Google App Engine provides a mechanism that allows users to specify their own
values for constants and "hook functions" for use by some of the Python
modules in the
google.appengine
packages. Specifying your own values can change the default behavior of the
related App Engine services based on the application's needs. The file where
you specify these constants is appengine_config.py. After
creating this file, you simply
deploy it with
your other code.
Configuring Python modules with appengine_config.py
Several Python modules in
App Engine are configurable using appengine_config.py.
To customize the Python modules of your App Engine services, create a new
appengine_config.py file in your application's root directory. To
use this file, you need to define only those constants or hook functions you
wish to override. You then run gcloud app deploy from the
directory where the app.yaml file is located to redeploy your app
along with the new appengine_config.py file. The constants and
hook functions that you defined will then be used by those App Engine services
internally.
To override a constant, prefix the constant's name with the Python module
name and an underscore, then assign a value. For example, to edit overrides in
appstats, you can
define the value of KEY_PREFIX
appstats_KEY_PREFIX = '__my_custom_prefix__'
Naming of overridden hook functions is similar in other Python modules. For
example, in
namespace_manager, you can override the hook function
default_namespace_for_request in appengine_config.py
as follows:
import os
def namespace_manager_default_namespace_for_request():
return os.environ.get('HTTP_HOST', '')
Configurable Python modules in App Engine
The Python modules listed below are configurable using
appengine_config.py. By convention, hook functions are lowercase
and constants are uppercase:
django_setup()
see_django_setup()in appengine/ext/webapp/__init__.py-
django_version(defaultNone. Example value '1.2') -
add_wsgi_middleware(app)(default returnsapp)
seerun_wsgi_app(app)in appengine/ext/webapp/util.py
-
default_namespace_for_request()(default returnsNone)
- see
class ConfigDefaultsin appengine/ext/appstats/recording.py - see example in appengine/ext/appstats/sample_appengine_config.py
BASE_PATH(default'/_ah/datastore_admin')MAPREDUCE_PATH(default'/_ah/mapreduce')CLEANUP_MAPREDUCE_STATE(defaultTrue)
CUSTOM_ENVIRONMENT_AUTHENTICATION- see
class ConfigDefaultsin appengine/ext/remote_api/handler.py
Configuring your own Python modules with lib_config
App Engine also allows you to configure your own Python modules with constants
and hook functions defined in appengine_config.py. The
lib_config.register() function allows you to both register the
names of the user-overridable constants and hooks, and to define sensible
defaults in case the users don't wish to override them. Internally,
lib_config.register() attempts to import
appengine_config. If successful, it replaces the defaults of the
specified Python modules with those defined in
appengine_config.py.
Example usage in my_module.py:
from google.appengine.api import lib_config
def _hook_function1_default():
return 'baz'
_config = lib_config.register('my_module', {'CONSTANT1': 'foo',
'CONSTANT2': 'bar',
'hook_function1': _hook_function1_default})
Now you can access a user's constants as:
_config.CONSTANT1 _config.CONSTANT2
and call their hook function as:
_config.hook_function1()
Some programmers like to group their defaults into a class:
class _ConfigDefaults(object):
CONSTANT1 = 'foo'
CONSTANT2 = 'bar'
def hook_function1():
return 'baz'
_config = lib_config.register('my_module', _ConfigDefaults.__dict__)
In order to override your defaults, a user could define in
appengine_config.py:
my_module_CONSTANT1 = 'foofoo' my_module_hook_function1 = lambda: 'bazbaz'
As a result, in my_module.py, the following will be true:
_config.CONSTANT1is now'foofoo'_config.CONSTANT2remains'bar'_config.hook_function1()returns'bazbaz'
The user overrides are available to my_module.py immediately after
lib_config.register() returns.