home · contact · privacy
Add todo tracker.
[misc] / unite.py
1 from plomlib import PlomException, run_server, PlomHandler 
2 from urllib.parse import urlparse
3 from income_progress_bars import IncomeProgressHandler 
4 from calories import ConsumptionsHandler 
5 from ledger import LedgerHandler 
6 from todo import TodoHandler 
7 server_port = 8081
8
9
10 class UnitedRequestHandler(PlomHandler):
11     routes = {'GET':{}, 'POST':{}}
12     apps = {}
13
14     @classmethod
15     def register_app(cls, app):
16         name, data = app.app_init(app, cls)
17         cls.apps[name] = data
18
19     @classmethod
20     def add_route(cls, method, path, service):
21         if not method in cls.routes.keys():
22             cls.routes[method] = {}
23         cls.routes[method][path] = service 
24
25     def do_POST(self):
26         self.try_do(self._do_posts)
27
28     def _do_posts(self):
29         parsed_url = urlparse(self.path)
30         path_toks = parsed_url.path.split('/')
31         while len(path_toks) > 0:
32             target_path = '/'.join(path_toks)
33             if target_path in self.routes['POST'].keys(): 
34                 self.routes['POST'][target_path](self)
35                 return 
36             path_toks.pop()
37         page = 'nothing to post?'
38         self.send_HTML(page)
39
40     def do_GET(self):
41         self.try_do(self._do_gets)
42
43     def _do_gets(self):
44         parsed_url = urlparse(self.path)
45         path_toks = parsed_url.path.split('/')
46         while len(path_toks) > 0:
47             target_path = '/'.join(path_toks)
48             print(target_path)
49             if target_path in self.routes['GET'].keys():                
50                 self.routes['GET'][target_path](self)
51                 return 
52             path_toks.pop()
53         page = 'hi there!<br />'
54         for route in self.routes['GET']:
55             page += f'<a href="{route}"/>{route}</a><br />'
56         self.send_HTML(page)
57
58
59 if __name__ == "__main__":  
60     UnitedRequestHandler.register_app(IncomeProgressHandler)
61     UnitedRequestHandler.register_app(ConsumptionsHandler)
62     UnitedRequestHandler.register_app(LedgerHandler)
63     UnitedRequestHandler.register_app(TodoHandler)
64     run_server(server_port, UnitedRequestHandler)