home · contact · privacy
Replace ProcessChildren with more flexible ProcessStep infrastructure.
[plomtask] / run.py
diff --git a/run.py b/run.py
index 644fc4abbf5aa7fcc1e7cd410d931e0f1af8798f..e1bbe5dafa4a43664f3b6839fe2659dc80ff0f8b 100755 (executable)
--- a/run.py
+++ b/run.py
@@ -1,15 +1,30 @@
 #!/usr/bin/env python3
 """Call this to start the application."""
 from sys import exit as sys_exit
-from plomtask.misc import HandledException
+from os import environ
+from plomtask.exceptions import HandledException
 from plomtask.http import TaskHandler, TaskServer
+from plomtask.db import DatabaseFile
 
+PLOMTASK_DB_PATH = environ.get('PLOMTASK_DB_PATH')
 HTTP_PORT = 8082
+DB_CREATION_ASK = 'Database file not found. Create? Y/n\n'
 
 
 if __name__ == '__main__':
     try:
-        server = TaskServer(('localhost', HTTP_PORT), TaskHandler)
+        if not PLOMTASK_DB_PATH:
+            raise HandledException('PLOMTASK_DB_PATH not set.')
+        db_file = DatabaseFile(PLOMTASK_DB_PATH)
+        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(db_file, ('localhost', HTTP_PORT), TaskHandler)
         print(f'running at port {HTTP_PORT}')
         try:
             server.serve_forever()
@@ -17,5 +32,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)