author | Tero Marttila <terom@fixme.fi> |
Mon, 09 Feb 2009 01:10:40 +0200 | |
changeset 47 | 99c45fc13edc |
parent 46 | 54c5f5f340de |
child 48 | 480adab03749 |
permissions | -rw-r--r-- |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
Tree-based URL mapping |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
import re |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
import os.path |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
|
46 | 8 |
from qmsk.web import map |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
class URLError (Exception) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
Error with an URL definition |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
pass |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
class LabelValue (object) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
Represents the value of a ValueLabel... love these names |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
def __init__ (self, label, value) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
Just store |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
self.label = label |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
self.value = value |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
def __str__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
return "%s=%r" % (self.label.key, self.value) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
33 |
def __repr__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
return "<%s>" % self |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
class Label (object) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
Base class for URL labels (i.e. the segments of the URL between /s) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
@staticmethod |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
42 |
def parse (mask, defaults, config) : |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
""" |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
44 |
Parse the given label-segment, and return a *Label instance. Config is the URLConfig to use |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
# empty? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
if not mask : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
return EmptyLabel() |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
# simple value? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
match = SimpleValueLabel.EXPR.match(mask) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
if match : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
# key |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
key = match.group('key') |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
# type |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
type = match.group("type") |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
|
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
61 |
# lookup type, None -> default |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
62 |
type = config.get_type(type) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
# defaults? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
default = defaults.get(key) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
if not default : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
default = match.group('default') |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
if default : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
# apply type to default |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
72 |
default = type.parse(default) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
# build |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
return SimpleValueLabel(key, type, default) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
# static? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
match = StaticLabel.EXPR.match(mask) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
if match : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
return StaticLabel(match.group('name')) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
# invalid |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
raise URLError("Invalid label: %r" % (mask, )) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
def match (self, value=None) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
Match this label against the given value, returning either True to match without a value, a LabelValue |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
object, or boolean false to not match. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
If value is None, this means that only a default value should be returned. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
abstract |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
95 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
96 |
def build (self, value_dict) : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
97 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
98 |
Return a string representing this label, using the values in the given value_dict if needed |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
99 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
100 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
101 |
abstract |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
103 |
class EmptyLabel (Label) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
An empty label, i.e. just a slash in the URL |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
def __eq__ (self, other) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
Just compares type |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
113 |
return isinstance(other, EmptyLabel) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
def match (self, value=None) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
116 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
117 |
Match empty string -> True |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
118 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
119 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
120 |
# no default |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
121 |
if value is None : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
122 |
return False |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
123 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
# only empty segments |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
125 |
if value == '' : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
return True |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
127 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
128 |
def build (self, values) : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
129 |
return str(self) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
130 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
131 |
def __str__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
132 |
return '' |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
133 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
134 |
class StaticLabel (Label) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
135 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
136 |
A simple literal Label, used for fixed terms in the URL |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
137 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
138 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
139 |
EXPR = re.compile(r'^(?P<name>[a-zA-Z_.-]+)$') |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
140 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
141 |
def __init__ (self, name) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
142 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
143 |
The given name is the literal name of this label |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
144 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
145 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
146 |
self.name = name |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
147 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
148 |
def __eq__ (self, other) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
149 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
150 |
Compares names |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
151 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
152 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
153 |
return isinstance(other, StaticLabel) and self.name == other.name |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
154 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
155 |
def match (self, value=None) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
156 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
157 |
Match exactly -> True |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
158 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
159 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
160 |
# no defaults |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
161 |
if value is None : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
162 |
return False |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
163 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
164 |
# match name |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
165 |
if value == self.name : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
166 |
return True |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
167 |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
168 |
def build (self, values) : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
169 |
return str(self) |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
170 |
|
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
171 |
def __str__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
172 |
return self.name |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
173 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
174 |
class ValueLabel (Label) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
175 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
176 |
A label with a key and a value |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
177 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
178 |
XXX: do we even need this? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
179 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
180 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
181 |
def __init__ (self, key, default) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
182 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
183 |
Set the key and default value. Default value may be None if there is no default value defined |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
184 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
185 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
186 |
self.key = key |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
187 |
self.default = default |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
188 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
189 |
def __eq__ (self, other) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
190 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
191 |
Compares keys |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
192 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
193 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
194 |
return isinstance(other, ValueLabel) and self.key == other.key |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
195 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
196 |
def build (self, values) : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
197 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
198 |
Return either the assigned value from values, our default value, or raise an error |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
199 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
200 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
201 |
value = values.get(self.key) |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
202 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
203 |
if not value and self.default : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
204 |
value = self.default |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
205 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
206 |
elif not value : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
207 |
raise URLError("No value given for label %r" % (self.key, )) |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
208 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
209 |
return value |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
210 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
211 |
class SimpleValueLabel (ValueLabel) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
212 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
213 |
A label that has a name and a simple string value |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
214 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
215 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
216 |
EXPR = re.compile(r'^\{(?P<key>[a-zA-Z_][a-zA-Z0-9_]*)(:(?P<type>[a-zA-Z_][a-zA-Z0-9_]*))?(=(?P<default>[^}]+))?\}$') |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
217 |
|
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
218 |
def __init__ (self, key, type, default) : |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
219 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
220 |
The given key is the name of this label's value |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
221 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
222 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
223 |
# type |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
224 |
self.type = type |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
225 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
226 |
# store |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
227 |
self.key = key |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
228 |
self.default = default |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
229 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
230 |
def match (self, value=None) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
231 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
232 |
Match -> LabelValue |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
233 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
234 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
235 |
# default? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
236 |
if value is None and self.default : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
237 |
return LabelValue(self, self.default) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
238 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
239 |
# only non-empty values! |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
240 |
elif value : |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
241 |
# test |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
242 |
if not self.type.test(value) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
243 |
return False |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
244 |
|
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
245 |
# convert with type |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
246 |
value = self.type.parse(value) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
247 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
248 |
return LabelValue(self, value) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
249 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
250 |
def __str__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
251 |
return '{%s%s%s}' % ( |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
252 |
self.key, |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
253 |
':%s' % (self.type, ), # XXX: omit if default |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
254 |
'=%s' % (self.default, ) if self.default else '', |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
255 |
) |
41 | 256 |
|
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
257 |
class URLType (object) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
258 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
259 |
Handles the type-ness of values in the URL |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
260 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
261 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
262 |
def _init_name (self, name) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
263 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
264 |
Initialize our .name attribute, called by URLConfig |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
265 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
266 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
267 |
self.name = name |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
268 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
269 |
def test (self, value) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
270 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
271 |
Tests if the given value is valid for this type. |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
272 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
273 |
Defaults to calling parse(), and returning False on errors, True otherwise |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
274 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
275 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
276 |
try : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
277 |
self.parse(value) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
278 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
279 |
except : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
280 |
return False |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
281 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
282 |
else : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
283 |
return True |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
284 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
285 |
def parse (self, value) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
286 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
287 |
Parse the given value, which was tested earlier with test(), and return the value object |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
288 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
289 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
290 |
abstract |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
291 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
292 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
293 |
def build (self, obj) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
294 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
295 |
Reverse of parse(), return an url-value built from the given object |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
296 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
297 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
298 |
abstract |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
299 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
300 |
def __str__ (self) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
301 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
302 |
Return a short string giving the name of this type, defaults to self.name |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
303 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
304 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
305 |
return self.name |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
306 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
307 |
class URLStringType (URLType) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
308 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
309 |
The default URLType, just plain strings. |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
310 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
311 |
Note that this does not accept empty strings as valid |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
312 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
313 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
314 |
def __init__ (self) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
315 |
super(URLStringType, self).__init__('str') |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
316 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
317 |
def parse (self, value) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
318 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
319 |
Identitiy |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
320 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
321 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
322 |
return value |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
323 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
324 |
def build (self, obj) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
325 |
if not obj : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
326 |
raise ValueError("String must not be empty") |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
327 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
328 |
return str(obj) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
329 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
330 |
class URLIntegerType (URLType) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
331 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
332 |
A URLType for simple integers |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
333 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
334 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
335 |
def __init__ (self, negative=True, zero=True, max=None) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
336 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
337 |
Pass in negative=False to disallow negative numbers, zero=False to disallow zero, or non-zero max |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
338 |
to specifiy maximum value |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
339 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
340 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
341 |
super(URLIntegerType, self).__init__('int') |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
342 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
343 |
self.negative = negative |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
344 |
self.zero = zero |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
345 |
self.max = max |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
346 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
347 |
def _validate (self, value) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
348 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
349 |
Test to make sure value fits our criteria |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
350 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
351 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
352 |
# negative? |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
353 |
if self.negative and value < 0 : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
354 |
raise ValueError("value is negative") |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
355 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
356 |
# zero? |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
357 |
if self.zero and value == 0 : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
358 |
raise ValueError("value is zero") |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
359 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
360 |
# max? |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
361 |
if self.max is not None and value > max : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
362 |
raise ValueError("value is too large: %d" % value) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
363 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
364 |
return value |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
365 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
366 |
def parse (self, value) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
367 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
368 |
Convert str -> int |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
369 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
370 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
371 |
return self._validate(int(value)) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
372 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
373 |
def build (self, obj) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
374 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
375 |
Convert int -> str |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
376 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
377 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
378 |
return unicode(self._validate(obj)) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
379 |
|
41 | 380 |
class URLConfig (object) : |
381 |
""" |
|
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
382 |
Global configuration relevant to all URLs. This can be used to construct a set of URLs and then create an |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
383 |
URLTree out of them. Simply call the url_config() instace with the normal URL arguments (except, of course, |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
384 |
config), and finally just pass the url_config to URLTree (it's iterable). |
41 | 385 |
""" |
386 |
||
387 |
# built-in type codes |
|
388 |
BUILTIN_TYPES = { |
|
43
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
389 |
# string |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
390 |
'str' : URLStringType(), |
43
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
391 |
|
41 | 392 |
# integer |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
393 |
'int' : URLIntegerType(), |
41 | 394 |
} |
395 |
||
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
396 |
# init names |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
397 |
for name, type in BUILTIN_TYPES.iteritems() : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
398 |
type._init_name(name) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
399 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
400 |
def __init__ (self, type_dict=None, default_type='str') : |
41 | 401 |
""" |
402 |
Create an URLConfig for use with URL |
|
403 |
||
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
404 |
If type_dict is given, it should be a dict of { type_names: URLType }, and they will be available for |
41 | 405 |
type specifications in addition to the defaults. |
406 |
""" |
|
407 |
||
408 |
# build our type_dict |
|
409 |
self.type_dict = self.BUILTIN_TYPES.copy() |
|
410 |
||
411 |
# apply the given type_dict |
|
412 |
if type_dict : |
|
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
413 |
# initialize names |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
414 |
for name, type in type_dict.iteritems() : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
415 |
type._init_name(name) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
416 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
417 |
# merge |
41 | 418 |
self.type_dict.update(type_dict) |
419 |
||
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
420 |
# init |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
421 |
self.default_type = default_type |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
422 |
self.urls = [] |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
423 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
424 |
def get_type (self, type_name=None) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
425 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
426 |
Lookup an URLType by type_name, None for default |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
427 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
428 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
429 |
# default type? |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
430 |
if not type_name : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
431 |
type_name = self.default_type |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
432 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
433 |
# lookup + return |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
434 |
return self.type_dict[type_name] |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
435 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
436 |
def __call__ (self, *args, **kwargs) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
437 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
438 |
Return new URL object with this config and the given args, adding it to our list of urls |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
439 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
440 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
441 |
# build |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
442 |
url = URL(self, *args, **kwargs) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
443 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
444 |
# store |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
445 |
self.urls.append(url) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
446 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
447 |
# return |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
448 |
return url |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
449 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
450 |
def __iter__ (self) : |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
451 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
452 |
Returns all defined URLs |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
453 |
""" |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
454 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
455 |
return iter(self.urls) |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
456 |
|
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
457 |
class URL (object) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
458 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
459 |
Represents a specific URL |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
460 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
461 |
|
41 | 462 |
|
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
463 |
def __init__ (self, config, url_mask, handler, **defaults) : |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
464 |
""" |
41 | 465 |
Create an URL using the given URLConfig, with the given url mask, handler, and default values. |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
466 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
467 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
468 |
# store |
41 | 469 |
self.config = config |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
470 |
self.url_mask = url_mask |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
471 |
self.handler = handler |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
472 |
self.defaults = defaults |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
473 |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
474 |
# query string |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
475 |
self.query_args = dict() |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
476 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
477 |
# parse any query string |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
478 |
# XXX: conflicts with regexp syntax |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
479 |
if '/?' in url_mask : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
480 |
url_mask, query_mask = url_mask.split('/?') |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
481 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
482 |
else : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
483 |
query_mask = None |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
484 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
485 |
# build our label path |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
486 |
self.label_path = [Label.parse(mask, defaults, config) for mask in url_mask.split('/')] |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
487 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
488 |
# build our query args list |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
489 |
if query_mask : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
490 |
# split into items |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
491 |
for query_item in query_mask.split('&') : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
492 |
# parse default |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
493 |
if '=' in query_item : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
494 |
query_item, default = query_item.split('=') |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
495 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
496 |
else : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
497 |
default = None |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
498 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
499 |
# parse type |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
500 |
if ':' in query_item : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
501 |
query_item, type = query_item.split(':') |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
502 |
else : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
503 |
type = None |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
504 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
505 |
# parse key |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
506 |
key = query_item |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
507 |
|
43
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
508 |
# type |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
509 |
type = self.config.get_type(type) |
43
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
510 |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
511 |
# add to query_args as (type, default) tuple |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
512 |
self.query_args[key] = (type, type.parse(default) if default else default) |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
513 |
|
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
514 |
def get_label_path (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
515 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
516 |
Returns a list containing the labels in this url |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
517 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
518 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
519 |
# copy self.label_path |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
520 |
return list(self.label_path) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
521 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
522 |
def execute (self, request, label_values) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
523 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
524 |
Invoke the handler, using the given label values |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
525 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
526 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
527 |
# start with the defaults |
40 | 528 |
kwargs = self.defaults.copy() |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
529 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
530 |
# then add all the values |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
531 |
for label_value in label_values : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
532 |
kwargs[label_value.label.key] = label_value.value |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
533 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
534 |
# then parse all query args |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
535 |
for key, value in request.get_args() : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
536 |
# lookup spec |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
537 |
type, default = self.query_args[key] |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
538 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
539 |
# normalize empty value to None |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
540 |
if not value : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
541 |
value = None |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
542 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
543 |
else : |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
544 |
# parse value |
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
545 |
value = type.parse(value) |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
546 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
547 |
# set default? |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
548 |
if not value : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
549 |
if default : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
550 |
value = default |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
551 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
552 |
if default == '' : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
553 |
# do not pass key at all |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
554 |
continue |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
555 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
556 |
# otherwise, fail |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
557 |
raise URLError("No value given for required argument: %r" % (key, )) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
558 |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
559 |
# set key |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
560 |
kwargs[key] = value |
43
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
561 |
|
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
562 |
# then check all query args |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
563 |
for key, (type, default) in self.query_args.iteritems() : |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
564 |
# skip those already present |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
565 |
if key in kwargs : |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
566 |
continue |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
567 |
|
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
568 |
# apply default? |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
569 |
if default is None : |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
570 |
raise URLError("Missing required argument: %r" % (key, )) |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
571 |
|
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
572 |
elif default == '' : |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
573 |
# skip empty default |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
574 |
continue |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
575 |
|
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
576 |
else : |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
577 |
# set default |
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
578 |
kwargs[key] = default |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
579 |
|
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
580 |
# execute the handler |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
581 |
return self.handler(request, **kwargs) |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
582 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
583 |
def build (self, request, **values) : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
584 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
585 |
Build an absolute URL pointing to this target, with the given values |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
586 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
587 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
588 |
# build URL from request page prefix and our labels |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
589 |
return request.page_prefix + '/'.join(label.build(values) for label in self.label_path) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
590 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
591 |
def __str__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
592 |
return '/'.join(str(label) for label in self.label_path) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
593 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
594 |
def __repr__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
595 |
return "URL(%r, %r)" % (str(self), self.handler) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
596 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
597 |
class URLNode (object) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
598 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
599 |
Represents a node in the URLTree |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
600 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
601 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
602 |
def __init__ (self, parent, label) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
603 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
604 |
Initialize with the given parent and label, empty children dict |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
605 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
606 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
607 |
# the parent URLNode |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
608 |
self.parent = parent |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
609 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
610 |
# this node's Label |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
611 |
self.label = label |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
612 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
613 |
# list of child URLNodes |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
614 |
self.children = [] |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
615 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
616 |
# this node's URL, set by add_url for an empty label_path |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
617 |
self.url = None |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
618 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
619 |
def _build_child (self, label) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
620 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
621 |
Build, insert and return a new child Node |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
622 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
623 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
624 |
# build new child |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
625 |
child = URLNode(self, label) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
626 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
627 |
# add to children |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
628 |
self.children.append(child) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
629 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
630 |
# return |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
631 |
return child |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
632 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
633 |
def add_url (self, url, label_path) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
634 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
635 |
Add a URL object to this node under the given path. Uses recursion to process the path. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
636 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
637 |
The label_path argument is a (partial) label path as returned by URL.get_label_path. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
638 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
639 |
If label_path is empty (len zero, or begins with EmptyLabel), then the given url is assigned to this node, if no |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
640 |
url was assigned before. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
641 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
642 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
643 |
# matches this node? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
644 |
if not label_path or isinstance(label_path[0], EmptyLabel) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
645 |
if self.url : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
646 |
raise URLError(url, "node already defined") |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
647 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
648 |
else : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
649 |
# set |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
650 |
self.url = url |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
651 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
652 |
else : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
653 |
# pop child label from label_path |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
654 |
child_label = label_path.pop(0) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
655 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
656 |
# look for the child to recurse into |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
657 |
child = None |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
658 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
659 |
# look for an existing child with that label |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
660 |
for child in self.children : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
661 |
if child.label == child_label : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
662 |
# found, use this |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
663 |
break |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
664 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
665 |
else : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
666 |
# build a new child |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
667 |
child = self._build_child(child_label) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
668 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
669 |
# recurse to handle the rest of the label_path |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
670 |
child.add_url(url, label_path) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
671 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
672 |
def match (self, label_path) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
673 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
674 |
Locate the URL object corresponding to the given label_path value under this node. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
675 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
676 |
Returns a (url, label_values) tuple |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
677 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
678 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
679 |
# determine value to use |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
680 |
value = None |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
681 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
682 |
# empty label_path? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
683 |
if not label_path or label_path[0] == '' : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
684 |
# the search ends at this node |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
685 |
if self.url : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
686 |
# this URL is the best match |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
687 |
return (self.url, []) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
688 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
689 |
elif not self.children : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
690 |
# incomplete URL |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
691 |
raise URLError("no URL handler defined for this Node") |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
692 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
693 |
else : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
694 |
# use default value, i.e. Label.match(None) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
695 |
label = None |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
696 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
697 |
else : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
698 |
# pop the next label from the label path |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
699 |
label = label_path.pop(0) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
700 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
701 |
# return one match... |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
702 |
match = value = None |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
703 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
704 |
# recurse through our children, DFS |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
705 |
for child in self.children : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
706 |
# match value |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
707 |
value = child.label.match(label) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
708 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
709 |
# skip those that don't match at all |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
710 |
if not value : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
711 |
continue; |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
712 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
713 |
# already found a match? :/ |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
714 |
if match : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
715 |
raise URLError("Ambiguous URL") |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
716 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
717 |
# ok, but continue looking to make sure there's no ambiguous URLs |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
718 |
match = child |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
719 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
720 |
# found something? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
721 |
if not match : |
40 | 722 |
raise URLError("No child found for label: %s + %s + %s" % (self.get_url(), label, '/'.join(str(l) for l in label_path))) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
723 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
724 |
# ok, recurse into the match |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
725 |
url, label_value = match.match(label_path) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
726 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
727 |
# add our value? |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
728 |
if isinstance(value, LabelValue) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
729 |
label_value.append(value) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
730 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
731 |
# return the match |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
732 |
return url, label_value |
40 | 733 |
|
734 |
def get_url (self) : |
|
735 |
""" |
|
736 |
Returns the URL for this node, by iterating over our parents |
|
737 |
""" |
|
738 |
||
739 |
# URL segments in reverse order |
|
740 |
segments = [''] |
|
741 |
||
742 |
# start with ourself |
|
743 |
node = self |
|
744 |
||
745 |
# iterate up to root |
|
746 |
while node : |
|
747 |
segments.append(str(node.label)) |
|
748 |
||
749 |
node = node.parent |
|
750 |
||
751 |
# reverse |
|
752 |
segments.reverse() |
|
753 |
||
754 |
# return |
|
755 |
return '/'.join(segments) |
|
756 |
||
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
757 |
def dump (self, indent=0) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
758 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
759 |
Returns a multi-line string representation of this Node |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
760 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
761 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
762 |
return '\n'.join([ |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
763 |
"%-45s%s" % ( |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
764 |
' '*indent + str(self.label) + ('/' if self.children else ''), |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
765 |
(' -> %r' % self.url) if self.url else '' |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
766 |
) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
767 |
] + [ |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
768 |
child.dump(indent + 4) for child in self.children |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
769 |
]) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
770 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
771 |
def __str__ (self) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
772 |
return "%s/[%s]" % (self.label, ','.join(str(child) for child in self.children)) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
773 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
774 |
class URLTree (map.Mapper) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
775 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
776 |
Map requests to handlers, using a defined tree of URLs |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
777 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
778 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
779 |
def __init__ (self, url_list) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
780 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
781 |
Initialize the tree using the given list of URLs |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
782 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
783 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
784 |
# root node |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
785 |
self.root = URLNode(None, EmptyLabel()) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
786 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
787 |
# just add each URL |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
788 |
for url in url_list : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
789 |
self.add_url(url) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
790 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
791 |
def add_url (self, url) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
792 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
793 |
Adds the given URL to the tree. The URL must begin with a root slash. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
794 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
795 |
# get url's label path |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
796 |
path = url.get_label_path() |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
797 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
798 |
# should begin with root |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
799 |
root_label = path.pop(0) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
800 |
assert root_label == self.root.label, "URL must begin with root" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
801 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
802 |
# add to root |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
803 |
self.root.add_url(url, path) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
804 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
805 |
def match (self, url) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
806 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
807 |
Find the URL object best corresponding to the given url, matching any ValueLabels. |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
808 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
809 |
Returns an (URL, [LabelValue]) tuple. |
47
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
810 |
|
99c45fc13edc
improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
811 |
XXX: handle unicode on URLs |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
812 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
813 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
814 |
# split it into labels |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
815 |
path = url.split('/') |
40 | 816 |
|
817 |
# empty URL is empty |
|
818 |
if url : |
|
819 |
# ensure that it doesn't start with a / |
|
820 |
assert not self.root.label.match(path[0]), "URL must not begin with root" |
|
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
821 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
822 |
# just match starting at root |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
823 |
return self.root.match(path) |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
824 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
825 |
def handle_request (self, request) : |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
826 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
827 |
Looks up the request's URL, and invokes its handler |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
828 |
""" |
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
829 |
|
40 | 830 |
# get the requested URL |
831 |
request_url = request.get_page_name() |
|
832 |
||
833 |
# find the URL+values to use |
|
834 |
url, label_values = self.match(request_url) |
|
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
835 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
836 |
# let the URL handle it |
40 | 837 |
return url.execute(request, label_values) |
39
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
838 |
|
82df0bb66ca7
split off to urltree.py, and add support for types
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
839 |