thirdparty/google_appengine/google/appengine/api/namespace_manager/__init__.py
changeset 2273 e4cb9c53db3e
child 2864 2e0b0af889be
equal deleted inserted replaced
2272:26491ee91e33 2273:e4cb9c53db3e
       
     1 #!/usr/bin/env python
       
     2 #
       
     3 # Copyright 2007 Google Inc.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #     http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 #
       
    17 
       
    18 """Control the namespacing system used by various APIs.
       
    19 
       
    20 Each API call can specify an alternate namespace, but the functions
       
    21 here can be used to change the default namespace. The default is set
       
    22 before user code begins executing.
       
    23 """
       
    24 
       
    25 
       
    26 
       
    27 import os
       
    28 
       
    29 ENV_DEFAULT_NAMESPACE = 'HTTP_X_APPENGINE_DEFAULT_NAMESPACE'
       
    30 
       
    31 __default_namespace = None
       
    32 
       
    33 def set_request_namespace(namespace):
       
    34   """Set the default namespace to use for future calls, for this request only.
       
    35 
       
    36   Args:
       
    37     namespace: A string naming the new namespace to use. The empty
       
    38       string specifies the root namespace for this app.
       
    39   """
       
    40   global __default_namespace
       
    41   __default_namespace = namespace
       
    42 
       
    43 
       
    44 def get_request_namespace():
       
    45   """Get the name of the current default namespace. The empty string
       
    46   indicates that the root namespace is the default."""
       
    47   global __default_namespace
       
    48   if __default_namespace is None:
       
    49     if ENV_DEFAULT_NAMESPACE in os.environ:
       
    50       __default_namespace = os.environ[ENV_DEFAULT_NAMESPACE]
       
    51     else:
       
    52       __default_namespace = ''
       
    53   return __default_namespace
       
    54 
       
    55 
       
    56 def _add_name_space(request, namespace=None):
       
    57   """Add a name_space field to a request.
       
    58 
       
    59   Args:
       
    60     request: A protocol buffer supporting the set_name_space() operation.
       
    61     namespace: The name of the namespace part. If None, use the
       
    62       default namespace.
       
    63   """
       
    64   if namespace is None:
       
    65     request.set_name_space(get_request_namespace())
       
    66   else:
       
    67     request.set_name_space(namespace)