comparison env/lib/python3.9/site-packages/routes/__init__.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Provides common classes and functions most users will want access to."""
2 import threading
3
4
5 class _RequestConfig(object):
6 """
7 RequestConfig thread-local singleton
8
9 The Routes RequestConfig object is a thread-local singleton that should
10 be initialized by the web framework that is utilizing Routes.
11 """
12 __shared_state = threading.local()
13
14 def __getattr__(self, name):
15 return getattr(self.__shared_state, name)
16
17 def __setattr__(self, name, value):
18 """
19 If the name is environ, load the wsgi envion with load_wsgi_environ
20 and set the environ
21 """
22 if name == 'environ':
23 self.load_wsgi_environ(value)
24 return self.__shared_state.__setattr__(name, value)
25 return self.__shared_state.__setattr__(name, value)
26
27 def __delattr__(self, name):
28 delattr(self.__shared_state, name)
29
30 def load_wsgi_environ(self, environ):
31 """
32 Load the protocol/server info from the environ and store it.
33 Also, match the incoming URL if there's already a mapper, and
34 store the resulting match dict in mapper_dict.
35 """
36 if 'HTTPS' in environ or environ.get('wsgi.url_scheme') == 'https' \
37 or environ.get('HTTP_X_FORWARDED_PROTO') == 'https':
38 self.__shared_state.protocol = 'https'
39 else:
40 self.__shared_state.protocol = 'http'
41 try:
42 self.mapper.environ = environ
43 except AttributeError:
44 pass
45
46 # Wrap in try/except as common case is that there is a mapper
47 # attached to self
48 try:
49 if 'PATH_INFO' in environ:
50 mapper = self.mapper
51 path = environ['PATH_INFO']
52 result = mapper.routematch(path)
53 if result is not None:
54 self.__shared_state.mapper_dict = result[0]
55 self.__shared_state.route = result[1]
56 else:
57 self.__shared_state.mapper_dict = None
58 self.__shared_state.route = None
59 except AttributeError:
60 pass
61
62 if 'HTTP_X_FORWARDED_HOST' in environ:
63 # Apache will add multiple comma separated values to
64 # X-Forwarded-Host if there are multiple reverse proxies
65 self.__shared_state.host = \
66 environ['HTTP_X_FORWARDED_HOST'].split(', ', 1)[0]
67 elif 'HTTP_HOST' in environ:
68 self.__shared_state.host = environ['HTTP_HOST']
69 else:
70 self.__shared_state.host = environ['SERVER_NAME']
71 if environ['wsgi.url_scheme'] == 'https':
72 if environ['SERVER_PORT'] != '443':
73 self.__shared_state.host += ':' + environ['SERVER_PORT']
74 else:
75 if environ['SERVER_PORT'] != '80':
76 self.__shared_state.host += ':' + environ['SERVER_PORT']
77
78
79 def request_config(original=False):
80 """
81 Returns the Routes RequestConfig object.
82
83 To get the Routes RequestConfig:
84
85 >>> from routes import *
86 >>> config = request_config()
87
88 The following attributes must be set on the config object every request:
89
90 mapper
91 mapper should be a Mapper instance thats ready for use
92 host
93 host is the hostname of the webapp
94 protocol
95 protocol is the protocol of the current request
96 mapper_dict
97 mapper_dict should be the dict returned by mapper.match()
98 redirect
99 redirect should be a function that issues a redirect,
100 and takes a url as the sole argument
101 prefix (optional)
102 Set if the application is moved under a URL prefix. Prefix
103 will be stripped before matching, and prepended on generation
104 environ (optional)
105 Set to the WSGI environ for automatic prefix support if the
106 webapp is underneath a 'SCRIPT_NAME'
107
108 Setting the environ will use information in environ to try and
109 populate the host/protocol/mapper_dict options if you've already
110 set a mapper.
111
112 **Using your own requst local**
113
114 If you have your own request local object that you'd like to use instead
115 of the default thread local provided by Routes, you can configure Routes
116 to use it::
117
118 from routes import request_config()
119 config = request_config()
120 if hasattr(config, 'using_request_local'):
121 config.request_local = YourLocalCallable
122 config = request_config()
123
124 Once you have configured request_config, its advisable you retrieve it
125 again to get the object you wanted. The variable you assign to
126 request_local is assumed to be a callable that will get the local config
127 object you wish.
128
129 This example tests for the presence of the 'using_request_local' attribute
130 which will be present if you haven't assigned it yet. This way you can
131 avoid repeat assignments of the request specific callable.
132
133 Should you want the original object, perhaps to change the callable its
134 using or stop this behavior, call request_config(original=True).
135 """
136 obj = _RequestConfig()
137 try:
138 if obj.request_local and original is False:
139 return getattr(obj, 'request_local')()
140 except AttributeError:
141 obj.request_local = False
142 obj.using_request_local = False
143 return _RequestConfig()
144
145 from routes.mapper import Mapper
146 from routes.util import redirect_to, url_for, URLGenerator
147
148 __all__ = ['Mapper', 'url_for', 'URLGenerator', 'redirect_to', 'request_config']