Ejemplo n.º 1
0
class _RequestConfig(object):
    """
    RequestConfig thread-local singleton
    
    The Routes RequestConfig object is a thread-local singleton that should be initialized by
    the web framework that is utilizing Routes.
    """
    __shared_state = threadinglocal.local()

    def __getattr__(self, name):
        return getattr(self.__shared_state, name)

    def __setattr__(self, name, value):
        """
        If the name is environ, load the wsgi envion with load_wsgi_environ
        and set the environ
        """
        if name == 'environ':
            self.load_wsgi_environ(value)
            return self.__shared_state.__setattr__(name, value)
        return self.__shared_state.__setattr__(name, value)

    def __delattr__(self, name):
        delattr(self.__shared_state, name)

    def load_wsgi_environ(self, environ):
        """
        Load the protocol/server info from the environ and store it.
        Also, match the incoming URL if there's already a mapper, and
        store the resulting match dict in mapper_dict.
        """
        if environ.get('HTTPS') or environ.get('wsgi.url_scheme') == 'https':
            self.__shared_state.protocol = 'https'
        else:
            self.__shared_state.protocol = 'http'
        if hasattr(self, 'mapper'):
            self.mapper.environ = environ
        if 'PATH_INFO' in environ and hasattr(self, 'mapper'):
            mapper = self.mapper
            path = environ['PATH_INFO']
            result = mapper.routematch(path)
            if result is not None:
                self.__shared_state.mapper_dict = result[0]
                self.__shared_state.route = result[1]
            else:
                self.__shared_state.mapper_dict = None
                self.__shared_state.route = None

        if environ.get('HTTP_HOST'):
            self.__shared_state.host = environ['HTTP_HOST']
        else:
            self.__shared_state.host = environ['SERVER_NAME']
            if environ['wsgi.url_scheme'] == 'https':
                if environ['SERVER_PORT'] != '443':
                    self.__shared_state.host += ':' + environ['SERVER_PORT']
            else:
                if environ['SERVER_PORT'] != '80':
                    self.__shared_state.host += ':' + environ['SERVER_PORT']
Ejemplo n.º 2
0
 def __init__(self,
              controller_scan=controller_scan,
              directory=None,
              always_scan=False,
              register=True,
              explicit=False):
     """Create a new Mapper instance
     
     All keyword arguments are optional.
     
     ``controller_scan``
         Function reference that will be used to return a list of
         valid controllers used during URL matching. If
         ``directory`` keyword arg is present, it will be passed
         into the function during its call. This option defaults to
         a function that will scan a directory for controllers.
         
         Alternatively, a list of controllers or None can be passed
         in which are assumed to be the definitive list of
         controller names valid when matching 'controller'.
     
     ``directory``
         Passed into controller_scan for the directory to scan. It
         should be an absolute path if using the default 
         ``controller_scan`` function.
     
     ``always_scan``
         Whether or not the ``controller_scan`` function should be
         run during every URL match. This is typically a good idea
         during development so the server won't need to be restarted
         anytime a controller is added.
     
     ``register``
         Boolean used to determine if the Mapper should use 
         ``request_config`` to register itself as the mapper. Since
         it's done on a thread-local basis, this is typically best
         used during testing though it won't hurt in other cases.
     
     ``explicit``
         Boolean used to determine if routes should be connected
         with implicit defaults of::
             
             {'controller':'content','action':'index','id':None}
         
         When set to True, these defaults will not be added to route
         connections and ``url_for`` will not use Route memory.
             
     Additional attributes that may be set after mapper
     initialization (ie, map.ATTRIBUTE = 'something'):
     
     ``encoding``
         Used to indicate alternative encoding/decoding systems to
         use with both incoming URL's, and during Route generation
         when passed a Unicode string. Defaults to 'utf-8'.
     
     ``decode_errors``
         How to handle errors in the encoding, generally ignoring
         any chars that don't convert should be sufficient. Defaults
         to 'ignore'.
     
     ``minimization``
         Boolean used to indicate whether or not Routes should
         minimize URL's and the generated URL's, or require every
         part where it appears in the path. Defaults to True.
     
     ``hardcode_names``
         Whether or not Named Routes result in the default options
         for the route being used *or* if they actually force url
         generation to use the route. Defaults to False.
     
     """
     self.matchlist = []
     self.maxkeys = {}
     self.minkeys = {}
     self.urlcache = LRUCache(1600)
     self._created_regs = False
     self._created_gens = False
     self.prefix = None
     self.req_data = threadinglocal.local()
     self.directory = directory
     self.always_scan = always_scan
     self.controller_scan = controller_scan
     self._regprefix = None
     self._routenames = {}
     self.debug = False
     self.append_slash = False
     self.sub_domains = False
     self.sub_domains_ignore = []
     self.domain_match = '[^\.\/]+?\.[^\.\/]+'
     self.explicit = explicit
     self.encoding = 'utf-8'
     self.decode_errors = 'ignore'
     self.hardcode_names = True
     self.minimization = True
     self.create_regs_lock = threading.Lock()
     if register:
         config = request_config()
         config.mapper = self
Ejemplo n.º 3
0
 def __init__(self, controller_scan=controller_scan, directory=None, 
              always_scan=False, register=True, explicit=False):
     """Create a new Mapper instance
     
     All keyword arguments are optional.
     
     ``controller_scan``
         Function reference that will be used to return a list of
         valid controllers used during URL matching. If
         ``directory`` keyword arg is present, it will be passed
         into the function during its call. This option defaults to
         a function that will scan a directory for controllers.
     
     ``directory``
         Passed into controller_scan for the directory to scan. It
         should be an absolute path if using the default 
         ``controller_scan`` function.
     
     ``always_scan``
         Whether or not the ``controller_scan`` function should be
         run during every URL match. This is typically a good idea
         during development so the server won't need to be restarted
         anytime a controller is added.
     
     ``register``
         Boolean used to determine if the Mapper should use 
         ``request_config`` to register itself as the mapper. Since
         it's done on a thread-local basis, this is typically best
         used during testing though it won't hurt in other cases.
     
     ``explicit``
         Boolean used to determine if routes should be connected
         with implicit defaults of::
             
             {'controller':'content','action':'index','id':None}
         
         When set to True, these defaults will not be added to route
         connections and ``url_for`` will not use Route memory.
             
     Additional attributes that may be set after mapper
     initialization (ie, map.ATTRIBUTE = 'something'):
     
     ``encoding``
         Used to indicate alternative encoding/decoding systems to
         use with both incoming URL's, and during Route generation
         when passed a Unicode string. Defaults to 'utf-8'.
     
     ``decode_errors``
         How to handle errors in the encoding, generally ignoring
         any chars that don't convert should be sufficient. Defaults
         to 'ignore'.
     
     ``minimization``
         Boolean used to indicate whether or not Routes should
         minimize URL's and the generated URL's, or require every
         part where it appears in the path. Defaults to True.
     
     ``hardcode_names``
         Whether or not Named Routes result in the default options
         for the route being used *or* if they actually force url
         generation to use the route. Defaults to False.
     
     """
     self.matchlist = []
     self.maxkeys = {}
     self.minkeys = {}
     self.urlcache = {}
     self._created_regs = False
     self._created_gens = False
     self.prefix = None
     self.req_data = threadinglocal.local()
     self.directory = directory
     self.always_scan = always_scan
     self.controller_scan = controller_scan
     self._regprefix = None
     self._routenames = {}
     self.debug = False
     self.append_slash = False
     self.sub_domains = False
     self.sub_domains_ignore = []
     self.domain_match = '[^\.\/]+?\.[^\.\/]+'
     self.explicit = explicit
     self.encoding = 'utf-8'
     self.decode_errors = 'ignore'
     self.hardcode_names = True
     self.minimization = True
     self.create_regs_lock = threading.Lock()
     if register:
         config = request_config()
         config.mapper = self
Ejemplo n.º 4
0
 def __init__(self, default=_NoDefault):
     self.__dict__['_local'] = threadinglocal.local()
     self.__dict__['_default'] = default
Ejemplo n.º 5
0
 def __init__(self, default=_NoDefault):
     self.__dict__['_local'] = threadinglocal.local()
     self.__dict__['_default'] = default