Mercurial > piecrust2
comparison piecrust/uriutil.py @ 710:e85f29b28b84
internal: Remove unused piece of code.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 26 May 2016 19:46:28 -0700 |
parents | 4284c673bb91 |
children | 4850f8c21b6e |
comparison
equal
deleted
inserted
replaced
709:4285b2c9b872 | 710:e85f29b28b84 |
---|---|
1 import re | 1 import re |
2 import os.path | 2 import os.path |
3 import string | 3 import string |
4 import logging | 4 import logging |
5 import functools | |
6 | 5 |
7 | 6 |
8 logger = logging.getLogger(__name__) | 7 logger = logging.getLogger(__name__) |
9 | |
10 | |
11 class UriError(Exception): | |
12 def __init__(self, uri): | |
13 super(UriError, self).__init__("Invalid URI: %s" % uri) | |
14 | |
15 | |
16 @functools.total_ordering | |
17 class UriInfo(object): | |
18 def __init__(self, uri, source, args, taxonomy=None, page_num=1): | |
19 self.uri = uri | |
20 self.source = source | |
21 self.args = args | |
22 self.taxonomy = taxonomy | |
23 self.page_num = page_num | |
24 | |
25 def __eq__(self, other): | |
26 return ((self.uri, self.source, self.args, self.taxonomy, | |
27 self.page_num) == | |
28 (other.uri, other.source, other.args, other.taxonomy, | |
29 other.page_num)) | |
30 | |
31 def __lt__(self, other): | |
32 return ((self.uri, self.source, self.args, self.taxonomy, | |
33 self.page_num) < | |
34 (other.uri, other.source, other.args, other.taxonomy, | |
35 other.page_num)) | |
36 | |
37 | |
38 pagenum_pattern = re.compile(r'/(\d+)/?$') | |
39 | |
40 | |
41 def parse_uri(routes, uri): | |
42 if uri.find('..') >= 0: | |
43 raise UriError(uri) | |
44 | |
45 page_num = 1 | |
46 match = pagenum_pattern.search(uri) | |
47 if match is not None: | |
48 uri = uri[:match.start()] | |
49 page_num = int(match.group(1)) | |
50 | |
51 uri = '/' + uri.strip('/') | |
52 | |
53 for rn, rc in routes.items(): | |
54 pattern = route_to_pattern(rn) | |
55 m = re.match(pattern, uri) | |
56 if m is not None: | |
57 args = m.groupdict() | |
58 return UriInfo(uri, rc['source'], args, rc.get('taxonomy'), | |
59 page_num) | |
60 | |
61 return None | |
62 | |
63 | |
64 r2p_pattern = re.compile(r'%(\w+)%') | |
65 | |
66 | |
67 def route_to_pattern(route): | |
68 return r2p_pattern.sub(r'(?P<\1>[\w\-]+)', route) | |
69 | 8 |
70 | 9 |
71 def multi_replace(text, replacements): | 10 def multi_replace(text, replacements): |
72 reps = dict((re.escape(k), v) for k, v in replacements.items()) | 11 reps = dict((re.escape(k), v) for k, v in replacements.items()) |
73 pattern = re.compile("|".join(list(reps.keys()))) | 12 pattern = re.compile("|".join(list(reps.keys()))) |