home · contact · privacy
Require explicit call to Day.save to have any Day committed to DB.
[plomtask] / run.py
diff --git a/run.py b/run.py
index d94905dc69bc97379567fb3f79d5f7288c7bbc57..ef6aeab6b7fd56a93ed3c9e76a7b5bbfe532b68c 100755 (executable)
--- a/run.py
+++ b/run.py
@@ -1,16 +1,31 @@
 #!/usr/bin/env python3
 """Call this to start the application."""
 from sys import exit as sys_exit
+from os import environ
 from plomtask.misc import HandledException
 from plomtask.http import TaskHandler, TaskServer
+from plomtask.db import DatabaseFile
 
+PATH_DB = environ.get('PATH_DB')
 HTTP_PORT = 8082
 TEMPLATES_DIR = 'templates'
+DB_CREATION_ASK = 'Database file not found. Create? Y/n\n'
 
 
 if __name__ == '__main__':
     try:
-        server = TaskServer(TEMPLATES_DIR,
+        if not PATH_DB:
+            raise HandledException('PATH_DB not set.')
+        db_file = DatabaseFile(PATH_DB)
+        if not db_file.exists:
+            legal_yesses = {'y', 'yes', 'yes.', 'yes!'}
+            reply = input(DB_CREATION_ASK)
+            if reply.lower() in legal_yesses:
+                db_file.remake()
+            else:
+                print('Not recognizing reply as "yes".')
+                raise HandledException('Cannot run without database.')
+        server = TaskServer(TEMPLATES_DIR, db_file,
                             ('localhost', HTTP_PORT), TaskHandler)
         print(f'running at port {HTTP_PORT}')
         try:
@@ -19,5 +34,5 @@ if __name__ == '__main__':
             print('aborting due to keyboard interrupt')
         server.server_close()
     except HandledException as e:
-        print(f'Aborting due to: {e}')
+        print(f'Aborting because: {e}')
         sys_exit(1)