home · contact · privacy
Rename DB path environment variable to something more unique.
[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.misc 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 TEMPLATES_DIR = 'templates'
12 DB_CREATION_ASK = 'Database file not found. Create? Y/n\n'
13
14
15 if __name__ == '__main__':
16     try:
17         if not PLOMTASK_DB_PATH:
18             raise HandledException('PLOMTASK_DB_PATH not set.')
19         db_file = DatabaseFile(PLOMTASK_DB_PATH)
20         if not db_file.exists:
21             legal_yesses = {'y', 'yes', 'yes.', 'yes!'}
22             reply = input(DB_CREATION_ASK)
23             if reply.lower() in legal_yesses:
24                 db_file.remake()
25             else:
26                 print('Not recognizing reply as "yes".')
27                 raise HandledException('Cannot run without database.')
28         server = TaskServer(TEMPLATES_DIR, db_file,
29                             ('localhost', HTTP_PORT), TaskHandler)
30         print(f'running at port {HTTP_PORT}')
31         try:
32             server.serve_forever()
33         except KeyboardInterrupt:
34             print('aborting due to keyboard interrupt')
35         server.server_close()
36     except HandledException as e:
37         print(f'Aborting because: {e}')
38         sys_exit(1)