2 """Call this to start the application."""
3 from sys import exit as sys_exit
5 from plomtask.exceptions import HandledException, NotFoundException
6 from plomtask.http import TaskHandler, TaskServer
7 from plomtask.db import DatabaseFile, UnmigratedDbException
9 PLOMTASK_DB_PATH = environ.get('PLOMTASK_DB_PATH')
11 DB_CREATION_ASK = 'Database file not found. Create? Y/n\n'
12 DB_MIGRATE_ASK = 'Database file needs migration. Migrate? Y/n\n'
15 def yes_or_fail(question: str, fail_msg: str) -> None:
16 """Ask question, raise HandledException(fail_msg) if reply not yes."""
17 reply = input(question)
18 if not reply.lower() in {'y', 'yes', 'yes.', 'yes!'}:
19 print('Not recognizing reply as "yes".')
20 raise HandledException(fail_msg)
23 if __name__ == '__main__':
25 if not PLOMTASK_DB_PATH:
26 raise HandledException('PLOMTASK_DB_PATH not set.')
28 db_file = DatabaseFile(PLOMTASK_DB_PATH)
29 except NotFoundException:
30 yes_or_fail(DB_CREATION_ASK, 'Cannot run without DB.')
31 db_file = DatabaseFile.create_at(PLOMTASK_DB_PATH)
32 except UnmigratedDbException:
33 yes_or_fail(DB_MIGRATE_ASK, 'Cannot run with unmigrated DB.')
34 db_file = DatabaseFile.migrate(PLOMTASK_DB_PATH)
35 server = TaskServer(db_file, ('localhost', HTTP_PORT), TaskHandler)
36 print(f'running at port {HTTP_PORT}')
38 server.serve_forever()
39 except KeyboardInterrupt:
40 print('aborting due to keyboard interrupt')
42 except HandledException as e:
43 print(f'Aborting because: {e}')