comparison roundup/web/router.py @ 4999:e9077def7678 routing

router: Add interactive mode and iterator over urlmap
author anatoly techtonik <techtonik@gmail.com>
date Thu, 10 Sep 2015 12:33:12 +0300
parents d8e0af01543b
children ce06c665932a
comparison
equal deleted inserted replaced
4998:d8e0af01543b 4999:e9077def7678
28 EXAMPLE_URL_MAP = ( 28 EXAMPLE_URL_MAP = (
29 'static/(.*)', ExampleFileHandler, 29 'static/(.*)', ExampleFileHandler,
30 'index', ExampleHandler 30 'index', ExampleHandler
31 ) 31 )
32 32
33 # --- Helper functions
34
35 def entry(prompt='> '):
36 """Just get text for interactive mode"""
37 import sys
38 if sys.version_info[0] < 3:
39 return raw_input(prompt)
40 else:
41 return input(prompt)
33 42
34 # --- Regexp based router 43 # --- Regexp based router
35 44
36 class Router(object): 45 class Router(object):
46
47 urlmap = []
37 48
38 def __init__(self, urlmap=[]): 49 def __init__(self, urlmap=[]):
39 """ 50 """
40 `urlmap` is a list (pattern, handler, pattern, ...) 51 `urlmap` is a list (pattern, handler, pattern, ...)
41 leading slash in pattern is stripped 52 leading slash in pattern is stripped
49 60
50 returns tuple (handler, arguments) or (None, ()) 61 returns tuple (handler, arguments) or (None, ())
51 """ 62 """
52 # strip leading slashes before matching 63 # strip leading slashes before matching
53 path = urlpath.lstrip('/') 64 path = urlpath.lstrip('/')
54 for i in range(0, len(self.urlmap), 2): 65 for pattern, handler in self.iter_urlmap():
55 pattern, handler = self.urlmap[i], self.urlmap[i+1]
56 pattern = pattern.lstrip('/') 66 pattern = pattern.lstrip('/')
57 if DEBUG: 67 if DEBUG:
58 print('router: matching %s' % pattern) 68 print('router: matching %s' % pattern)
59 match = re.match(pattern, path) 69 match = re.match(pattern, path)
60 if match: 70 if match:
61 return handler, match.groups() 71 return handler, match.groups()
62 return (None, ()) 72 return (None, ())
63 73
74 def iter_urlmap(self):
75 """
76 iterate over self.urlmap returning (pattern, handler) pairs
77 """
78 for i in range(0, len(self.urlmap), 2):
79 yield self.urlmap[i], self.urlmap[i+1]
80
81 def interactive(self):
82 print('enter url to test, [l] to list rules, empty line exits')
83 url = entry('url: ')
84 while url != '':
85 if url == 'l':
86 for i in range(0, len(self.urlmap), 2):
87 pattern, handler = self.urlmap[i], self.urlmap[i+1]
88 print(self.urlmap[i:i+2])
89 print('matched ' + str(self.get_handler(url)))
90 url = entry('url: ')
64 91
65 92
66 # [ ] len(urlmap) should be even to avoid errors 93 # [ ] len(urlmap) should be even to avoid errors
67 # (find a way to explain this to users) 94 # (find a way to explain this to users)
68 95
69 if __name__ == '__main__': 96 if __name__ == '__main__':
97
98 import sys
99 if '-i' in sys.argv:
100 router = Router(EXAMPLE_URL_MAP)
101 router.interactive()
102 sys.exit()
70 103
71 import unittest 104 import unittest
72 class test_Router(unittest.TestCase): 105 class test_Router(unittest.TestCase):
73 def test_example_routes(self): 106 def test_example_routes(self):
74 router = Router(EXAMPLE_URL_MAP) 107 router = Router(EXAMPLE_URL_MAP)

Roundup Issue Tracker: http://roundup-tracker.org/