Mercurial > hg > wsgintegrate
annotate wsgintegrate/match.py @ 18:d30118837c1a
dunno
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 30 Dec 2012 20:23:50 -0800 |
parents | 3a1fb496b47d |
children |
rev | line source |
---|---|
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 utilities for matching requests |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 these are just a sample; you can add arbitrary match objects if desired |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
13
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
6 from webob import Request |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 from webob import exc |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 class RequestMatch(object): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 """abstract base class for matching requests""" |
12 | 11 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 def __init__(self, app): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 self.app = app |
12 | 14 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 def __call__(self, environ, start_response): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 """WSGI app""" |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 if not self.condition(environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 raise exc.HTTPNotFound |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 return self.app(environ, start_response) |
12 | 20 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 def condition(self, environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 """match condition""" |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 return True |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 class ConditionMatch(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 """generic environment-based condition-checker""" |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 # XXX unused |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 def __init__(self, app, condition): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 RequestMatch.__init__(self, app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 self.condition = condition |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 ### aspect-based checkers |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 class MatchPath(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 """match based on PATH INFO""" |
12 | 36 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 def __init__(self, app, path): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 RequestMatch.__init__(self, app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 self.path = path |
12 | 40 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 def match(self, path): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 if path.startswith(self.path): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 # currently only matches with str.startswith; |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 # different types of matches could be considered |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 return self.path, path[len(self.path):] |
12 | 46 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 def condition(self, environ): # XXX unused |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 return self.match(environ['PATH_INFO']) is not None |
12 | 49 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 def __call__(self, environ, start_response): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
51 match = self.match(environ['PATH_INFO']) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
52 if match is None: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
53 raise exc.HTTPNotFound |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
54 script_name, path_info = match |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 # fix up the environment for downstream applications |
13
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
57 _script_name = environ.get('SCRIPT_NAME') |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
58 _path_info = environ.get('PATH_INFO') |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 environ['SCRIPT_NAME'] = script_name |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 environ['PATH_INFO'] = path_info |
12 | 61 |
13
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
62 request = Request(environ) |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
63 response = request.get_response(self.app) |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
64 |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
65 # unfutz the environ if the resource is not found |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
66 if response.status_int == 404: |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
67 if _script_name is not None: |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
68 environ['SCRIPT_NAME'] = script_name |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
69 environ['PATH_INFO'] = _path_info |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
70 |
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
71 return response(environ, start_response) |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
72 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
73 class MatchMethod(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
74 """match based on request method""" |
12 | 75 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 def __init__(self, app, methods): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 RequestMatch.__init__(self, app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 if isinstance(methods, basestring): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 methods = methods.split() |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 self.methods = set(methods) |
13
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
81 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
82 def condition(self, environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
83 return environ['REQUEST_METHOD'] in self.methods |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
84 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
85 class MatchHost(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
86 """match based on the host and port""" |
13
bb66c9a62be9
unfutz the environ if the resource is not found
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
87 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
88 def __init__(self, app, host, port=None): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
89 RequestMatch.__init__(self, app) |
18 | 90 self.wildcard = False |
91 if host.startswith('*.'): | |
92 self.wildcard = True | |
93 host = host[len('*.'):] | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
94 self.host = host |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
95 self.port = port |
16 | 96 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
97 def condition(self, environ): |
17
3a1fb496b47d
fix host matching if HTTP_HOST is not given
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
98 host = environ.get('HTTP_HOST') |
3a1fb496b47d
fix host matching if HTTP_HOST is not given
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
99 if host is None: |
3a1fb496b47d
fix host matching if HTTP_HOST is not given
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
100 return False |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
101 port = None |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
102 if ':' in host: |
16 | 103 host, port = host.rsplit(':', 1) |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
104 if self.port and port != self.port: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
105 return False |
18 | 106 if self.wildcard and host.endswith('.' + self.host): |
107 return True # wildcard | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
108 return host == self.host |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
109 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
110 class MatchAuthorized(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
111 """match if a user is authorized or not""" |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
112 def __init__(self, app, users=None): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
113 RequestMatch.__init__(self, app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
114 self.authorized_users = users |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
115 def condition(self, environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
116 raise NotImplementedError # TODO |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
117 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
118 class MatchProtocol(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
119 """match a given protocol, i.e. http vs https://""" |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
120 def __init__(self, app, protocol): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
121 self.protocol = protocol |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
122 RequestMatch.__init__(self, app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
123 def condition(self, environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
124 raise NotImplementedError # TODO |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
125 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
126 class MatchQueryString(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
127 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
128 match a request based on if the right query string parameters are given |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
129 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
130 def __init__(self, app, *tags, **kw): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
131 self.app = app |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
132 self.tags = tags |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
133 self.kw = kw |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
134 def condition(self, environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
135 raise NotImplementedError # TODO |
14 | 136 |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
137 ### logical checkers (currently unused) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
138 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
139 class AND(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
140 def __init__(self, app, condition1, condition2, *conditions): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
141 RequestMatch.__init__(self, app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
142 self.conditions = [condition1, condition2] |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
143 self.conditions.extend(conditions) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
144 def condition(self, environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
145 for condition in self.conditions: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
146 if isinstance(condition, RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
147 if not condition.condition(environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
148 return False |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
149 else: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
150 if not condition(): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
151 return False |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
152 return True |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
153 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
154 class OR(RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
155 def __init__(self, app, condition1, condition2, *conditions): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
156 RequestMatch.__init__(self, app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
157 self.conditions = [condition1, condition2] |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
158 self.conditions.extend(conditions) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
159 def condition(self, environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
160 for condition in self.conditions: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
161 if isinstance(condition, RequestMatch): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
162 if condition.condition(environ): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
163 return True |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
164 else: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
165 if condition(): |
14 | 166 return |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
167 return False |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
168 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
169 # string accessible list of conditions |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
170 conditions = {'host': MatchHost, |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
171 'method': MatchMethod, |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
172 'path': MatchPath } |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
173 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
174 class WrapApp(object): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
175 """match string-based conditions""" |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
176 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
177 def __init__(self, conditions=None): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
178 self.conditions = conditions or globals()['conditions'] |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
179 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
180 def __call__(self, app, *conditions, **kwargs): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
181 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
182 wrap an app in conditions |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
183 conditions should be a key, value 2-tuple of string, args; |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
184 kwargs should be a dictionary of unordered conditions, |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
185 likewise of the form string, args. |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
186 use *conditions if order is important, otherwise kwargs |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
187 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
188 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
189 # determine the condition |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
190 conditions = list(conditions) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
191 if kwargs: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
192 conditions.extend(kwargs.items()) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
193 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
194 # wrap the application |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
195 for condition, args in conditions: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
196 assert condition in self.conditions, 'Condition "%s" unknown' % condition |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
197 app = self.conditions[condition](app, args) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
198 return app |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
199 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
200 # convenience invocation |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
201 wrap = WrapApp(conditions) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
202 |