annotate roundup/web/router.py @ 4907:c37069a99cec routing

routing: Add self-test to router.py
author anatoly techtonik <techtonik@gmail.com>
date Tue, 15 Jul 2014 13:47:28 +0300
parents b860ede03056
children f31c93abedf6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4905
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
2 """
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
3 The purpose of router is to make Roundup URL scheme configurable
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
4 and allow extensions add their own handlers and URLs to tracker.
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
5
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
6 Public domain work by:
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
7 anatoly techtonik <techtonik@gmail.com>
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
8 """
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
9
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
10 import re
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
11
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
12
4906
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
13 # --- Example URL mapping
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
14
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
15 class NamedObject(object):
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
16 """Object that outputs given name when printed"""
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
17 def __init__(self, name):
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
18 self.name = name
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
19 def __repr__(self):
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
20 return self.name
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
21
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
22 ExampleHandler = NamedObject('ExampleHandler')
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
23 ExampleFileHandler = NamedObject('ExampleFileHandler')
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
24
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
25 EXAMPLE_URLMAP = (
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
26 '/static/(.*)', ExampleFileHandler,
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
27 '/', ExampleHandler
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
28 )
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
29
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
30
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
31 # --- Regexp based router
b860ede03056 routing: Add example URL map
anatoly techtonik <techtonik@gmail.com>
parents: 4905
diff changeset
32
4905
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
33 class Router(object):
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
34
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
35 def __init__(self, urlmap=[]):
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
36 """
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
37 `urlmap` is a list (pattern, handler, pattern, ...)
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
38 """
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
39 self.urlmap = urlmap
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
40
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
41 def get_handler(self, urlpath):
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
42 """
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
43 `urlpath` is a part of url /that/looks?like=this
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
44
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
45 returns tuple (handler, arguments) or (None, ())
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
46 """
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
47 for i in range(0, len(self.urlmap), 2):
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
48 pattern, handler = self.urlmap[i], self.urlmap[i+1]
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
49 match = re.match(pattern, urlpath)
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
50 if match:
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
51 return handler, match.groups()
6e313bdf6b69 routing: Add new roundup.web namespace with router component
anatoly techtonik <techtonik@gmail.com>
parents:
diff changeset
52 return (None, ())
4907
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
53
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
54
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
55
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
56 # [ ] len(urlmap) should be even to avoid errors
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
57 # (find a way to explain this to users)
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
58
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
59 if __name__ == '__main__':
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
60
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
61 import unittest
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
62 class test_Router(unittest.TestCase):
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
63 def test_example_routes(self):
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
64 router = Router(EXAMPLE_URLMAP)
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
65 self.assertEquals(router.get_handler(''), (None, ()))
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
66 handler, params = router.get_handler('/')
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
67 self.assertEquals(handler, ExampleHandler)
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
68 self.assertEquals(params, tuple())
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
69
c37069a99cec routing: Add self-test to router.py
anatoly techtonik <techtonik@gmail.com>
parents: 4906
diff changeset
70 unittest.main()

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