thirdparty/google_appengine/google/appengine/api/namespace_manager/__init__.py
changeset 2864 2e0b0af889be
parent 2273 e4cb9c53db3e
equal deleted inserted replaced
2862:27971a13089f 2864:2e0b0af889be
    25 
    25 
    26 
    26 
    27 import os
    27 import os
    28 
    28 
    29 ENV_DEFAULT_NAMESPACE = 'HTTP_X_APPENGINE_DEFAULT_NAMESPACE'
    29 ENV_DEFAULT_NAMESPACE = 'HTTP_X_APPENGINE_DEFAULT_NAMESPACE'
       
    30 ENV_CURRENT_NAMESPACE = '__INTERNAL_CURRENT_NAMESPACE'
    30 
    31 
    31 __default_namespace = None
       
    32 
    32 
    33 def set_request_namespace(namespace):
    33 def set_request_namespace(namespace):
    34   """Set the default namespace to use for future calls, for this request only.
    34   """Set the default namespace to use for future calls, for this request only.
    35 
    35 
    36   Args:
    36   Args:
    37     namespace: A string naming the new namespace to use. The empty
    37     namespace: A string naming the new namespace to use. The empty
    38       string specifies the root namespace for this app.
    38       string specifies the root namespace for this app.
    39   """
    39   """
    40   global __default_namespace
    40   os.environ[ENV_CURRENT_NAMESPACE] = namespace
    41   __default_namespace = namespace
       
    42 
    41 
    43 
    42 
    44 def get_request_namespace():
    43 def get_request_namespace():
    45   """Get the name of the current default namespace. The empty string
    44   """Get the name of the current default namespace.
    46   indicates that the root namespace is the default."""
    45 
    47   global __default_namespace
    46   The empty string indicates that the root namespace is the default.
    48   if __default_namespace is None:
    47   """
       
    48   return os.getenv(ENV_CURRENT_NAMESPACE, '')
       
    49 
       
    50 
       
    51 def _enable_request_namespace():
       
    52   """Automatically enable namespace to default for domain.
       
    53 
       
    54   Calling this function will automatically default the namespace to the
       
    55   chosen Google Apps domain for the current request.
       
    56   """
       
    57   if ENV_CURRENT_NAMESPACE not in os.environ:
    49     if ENV_DEFAULT_NAMESPACE in os.environ:
    58     if ENV_DEFAULT_NAMESPACE in os.environ:
    50       __default_namespace = os.environ[ENV_DEFAULT_NAMESPACE]
    59       os.environ[ENV_CURRENT_NAMESPACE] = os.environ[ENV_DEFAULT_NAMESPACE]
    51     else:
    60     else:
    52       __default_namespace = ''
    61       os.environ[ENV_CURRENT_NAMESPACE] = ''
    53   return __default_namespace
       
    54 
    62 
    55 
    63 
    56 def _add_name_space(request, namespace=None):
    64 def _add_name_space(request, namespace=None):
    57   """Add a name_space field to a request.
    65   """Add a name_space field to a request.
    58 
    66