<commit_before>"""
Provides common classes and functions most users will want access to.

"""

import threadinglocal, sys

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()
    __shared_glob = {}
    
    def __getattr__(self, name):
        if name == 'mapper':
            return self.__shared_glob[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)
        elif name == 'mapper':
            self.__shared_glob[name] = value
            return
        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 'wsgi.scheme' in environ:
            self.__shared_state.protocol = environ['wsgi.scheme']
        if 'PATH_INFO' in environ and hasattr(self, 'mapper'):
            mapper = self.mapper
            path = environ['PATH_INFO']
            self.__shared_state.mapper_dict = mapper.match(path)
        if 'SERVER_NAME' in environ:
            host = environ['SERVER_NAME']
            if environ.get('SERVER_PORT', 80) != 80:
                host += ':' + environ['SERVER_PORT']
    
def request_config(original=False):
    """
    Returns the Routes RequestConfig object.
    
    To get the Routes RequestConfig:
    
    >>> from routes import *
    >>> config = routes.request_config()
    
    The following attributes must be set on the config object every request:
    
    mapper
        mapper should be a Mapper instance thats ready for use
    host
        host is the hostname of the webapp
    protocol
        protocol is the protocol of the current request
    mapper_dict
        mapper_dict should be the dict returned by mapper.match()
    redirect<commit_msg>[svn] Change our environ populating to be a bit more accurate and use the host name when possible<commit_after>"""
Provides common classes and functions most users will want access to.

"""

import threadinglocal, sys

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()
    __shared_glob = {}
    
    def __getattr__(self, name):
        if name == 'mapper':
            return self.__shared_glob[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)
        elif name == 'mapper':
            self.__shared_glob[name] = value
            return
        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'):
            self.__shared_state.protocol = 'https'
        else:
            self.__shared_state.protocol = 'http'
        if 'PATH_INFO' in environ and hasattr(self, 'mapper'):
            mapper = self.mapper
            path = environ['PATH_INFO']
            self.__shared_state.mapper_dict = mapper.match(path)
        host = environ.get('HTTP_HOST') or environ.get('SERVER_NAME')
        self.__shared_state.host = host.split(':')[0]
        if environ.get('SERVER_PORT', 80) != 80:
            self.__shared_state.host += ':' + environ['SERVER_PORT']
    
def request_config(original=False):
    """
    Returns the Routes RequestConfig object.
    
    To get the Routes RequestConfig:
    
    >>> from routes import *
    >>> config = routes.request_config()
    
    The following attributes must be set on the config object every request:
    
    mapper
        mapper should be a Mapper instance thats ready for use
    host
        host is the hostname of the webapp
    protocol
        protocol is the protocol of the current request
    mapper_dict
        mapper_dict should be the dict returned by mapper.match()
    redirect
{'fraction_non_alphanumeric': 0.07938664596273291, 'fraction_numerical': 0.0017468944099378882, 'mean_word_length': 2.379016393442623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3173453', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1404, 'n_words': 463}