From 822e512a01d3433b935d09c3a9ae721e7c6d3ed2 Mon Sep 17 00:00:00 2001 From: rrr-marble Date: Fri, 24 Sep 2021 02:47:48 +0300 Subject: [PATCH] ref: extract globals into common config.py --- config.py | 11 +++++++++++ main.py | 15 ++++++--------- util/import_photos.py | 19 +++++++++---------- 3 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..29e6f07 --- /dev/null +++ b/config.py @@ -0,0 +1,11 @@ +# use database residing here +DB_LOCATION = ( + "testbox/photovoter.dblite" # Q: any allowances for this being not OUR database? +) + +DATA_LOCATION = "/tmp/123" + +# place compressed images here (needs to exist) +DEST_SHRUNK = "image/" +# move originals here (needs to exist) +DEST_ORIGINAL = "original/" diff --git a/main.py b/main.py index 94365ed..ccd852f 100644 --- a/main.py +++ b/main.py @@ -9,12 +9,9 @@ from uuid import uuid4 import sqlite3 import zipfile - -# use database residing here -DB_LOCATION = ( - "../testbox/photovoter.dblite" # Q: any allowances for this being not OUR database? -) -DATA_LOCATION = "/tmp/123" +# Global settings of this program +# ./config.py +from config import DB_LOCATION, DATA_LOCATION app = FastAPI() security = HTTPBasic() @@ -219,9 +216,9 @@ async def upload_pictures( def unpack_pictures_zip(file: UploadFile, time): """ Unpack and process zip archived photo - Extract pictures in the DATA_LOCATION/processing + Extract pictures in the DATA_LOCATION/processing #TODO: and feed them to util/import_photos.py - #TODO: Walk the nested DATA_LOCATION/processing ourselves + #TODO: Walk the nested DATA_LOCATION/processing ourselves Uses: DATA_LOCATION """ # we only call this function sporadically, so import here @@ -229,7 +226,7 @@ def unpack_pictures_zip(file: UploadFile, time): print(f"Accepted {file.filename} at {time} into processing") os.chdir(DATA_LOCATION) - os.mkdir("processing") + os.makedirs("processing", exist_ok=True) os.chdir("processing") # using private ._file field is a dirty hack, but diff --git a/util/import_photos.py b/util/import_photos.py index e359d77..b165a1a 100644 --- a/util/import_photos.py +++ b/util/import_photos.py @@ -8,15 +8,6 @@ from sys import argv, stderr from shutil import move import sqlite3 -# update database residing here -DB_LOCATION = ( - "db/photovoter.dblite" # Q: any allowances for this being not OUR database? -) -# place compressed images here (needs to exist) -DEST_SHRUNK = "db/image/" -# move originals here (needs to exist) -DEST_ORIGINAL = "db/original/" - def process_pictures(source: str, dest_shrunk: str, dest_original: str): """Process images from the base directory in the first command line argument. @@ -187,7 +178,15 @@ def main(): usage() exit(1) - run(DB_LOCATION, argv[1], DEST_SHRUNK, DEST_ORIGINAL) + import sys + import os + + # append root directory to sys.path + # to allow import globals from ../config.py + sys.path.append(os.path.dirname(__file__) + "/..") + import config as cfg + + run(cfg.DB_LOCATION, argv[1], cfg.DEST_SHRUNK, cfg.DEST_ORIGINAL) if __name__ == "__main__":