home · contact · privacy
Add PostvarsParser to isolate postvars parsing/checking.
[plomtask] / run.py
1 #!/usr/bin/env python3
2 """Call this to start the application."""
3 from sys import exit as sys_exit
4 from os import environ
5 from plomtask.exceptions import HandledException
6 from plomtask.http import TaskHandler, TaskServer
7 from plomtask.db import DatabaseFile
8
9 PLOMTASK_DB_PATH = environ.get('PLOMTASK_DB_PATH')
10 HTTP_PORT = 8082
11 DB_CREATION_ASK = 'Database file not found. Create? Y/n\n'
12
13
14 if __name__ == '__main__':
15     try:
16         if not PLOMTASK_DB_PATH:
17             raise HandledException('PLOMTASK_DB_PATH not set.')
18         db_file = DatabaseFile(PLOMTASK_DB_PATH)
19         if not db_file.exists:
20             legal_yesses = {'y', 'yes', 'yes.', 'yes!'}
21             reply = input(DB_CREATION_ASK)
22             if reply.lower() in legal_yesses:
23                 db_file.remake()
24             else:
25                 print('Not recognizing reply as "yes".')
26                 raise HandledException('Cannot run without database.')
27         server = TaskServer(db_file, ('localhost', HTTP_PORT), TaskHandler)
28         print(f'running at port {HTTP_PORT}')
29         try:
30             server.serve_forever()
31         except KeyboardInterrupt:
32             print('aborting due to keyboard interrupt')
33         server.server_close()
34     except HandledException as e:
35         print(f'Aborting because: {e}')
36         sys_exit(1)