From 82dcf089d0a830bffd15e311c53ff00a1104d32c Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 9 Oct 2020 18:32:33 -0500 Subject: [PATCH] reformat and reorganize documentation and code more, slight edits to logging messages, add configuration defaults for image_count/buffer_size/single_override --- phototag/__init__.py | 67 +++++++++----------------------------------- phototag/cli.py | 67 ++++++++++++++++++++++++-------------------- phototag/config.py | 24 ++++++++++++++-- requirements.txt | 2 +- setup.py | 11 ++++++-- 5 files changed, 80 insertions(+), 91 deletions(-) diff --git a/phototag/__init__.py b/phototag/__init__.py index 8ea77fb..1f886ed 100644 --- a/phototag/__init__.py +++ b/phototag/__init__.py @@ -1,15 +1,17 @@ +""" +__init__.py + +The module's initialization file responsible for setting up loggers, holding a couple extension and path constants, +as well as setting up environment variables for the Google Cloud Vision API. +""" + import logging import os -import progressbar - from . import config -# Logging and Progressbar work -progressbar.streams.wrap_stderr() log = logging.getLogger("init") log.setLevel(logging.INFO) -log.info("Progressbar/Logging ready.") # Path Constants ROOT = os.getcwd() @@ -17,57 +19,14 @@ INPUT_PATH = ROOT SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__)) TEMP_PATH = os.path.join(ROOT, "temp") OUTPUT_PATH = os.path.join(ROOT, "output") -log.info("Path Constants Built.") +log.info("Path constants built successfully...") # Environment Variables -os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join( - SCRIPT_ROOT, "config", config.config["google"]["credentials"] -) +os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(SCRIPT_ROOT, "config", + config.config["google"]["credentials"]) # Extension Constants -RAW_EXTS = [ - "3fr", - "ari", - "arw", - "bay", - "braw", - "crw", - "cr2", - "cr3", - "cap", - "data", - "dcs", - "dcr", - "dng", - "drf", - "eip", - "erf", - "fff", - "gpr", - "iiq", - "k25", - "kdc", - "mdc", - "mef", - "mos", - "mrw", - "nef", - "nrw", - "obm", - "orf", - "pef", - "ptx", - "pxn", - "r3d", - "raf", - "raw", - "rwl", - "rw2", - "rwz", - "sr2", - "srf", - "srw", - "tif", - "x3f", -] +RAW_EXTS = ["3fr", "ari", "arw", "bay", "braw", "crw", "cr2", "cr3", "cap", "data", "dcs", "dcr", "dng", "drf", "eip", + "erf", "fff", "gpr", "iiq", "k25", "kdc", "mdc", "mef", "mos", "mrw", "nef", "nrw", "obm", "orf", "pef", + "ptx", "pxn", "r3d", "raf", "raw", "rwl", "rw2", "rwz", "sr2", "srf", "srw", "tif", "x3f", ] LOSSY_EXTS = ["jpeg", "jpg", "png"] diff --git a/phototag/cli.py b/phototag/cli.py index 3c3f946..507b7d2 100644 --- a/phototag/cli.py +++ b/phototag/cli.py @@ -1,3 +1,9 @@ +""" +cli.py + +The file responsible for providing commandline functionality to the user. +""" + import logging import os import shutil @@ -11,11 +17,13 @@ log = logging.getLogger("cli") @click.group() def cli(): + """Base CLI command group""" pass @cli.command() def run(): + """Run tagging of all valid files in the current directory.""" log.info(f"CLI started tagging at {os.getcwd()}") from .app import run @@ -24,39 +32,38 @@ def run(): @cli.command() @click.argument("path") -@click.option( - "-m", - "--move", - default=False, - show_default=True, - prompt=True, - help="Move instead of copying the credentials file", -) +@click.option("-m", "--move", default=False, show_default=True, prompt=True, + help="Move instead of copying the credentials file", ) def auth(path, move): + """ + A utility command for copying the Downloaded Google Vision API Credentials file to the configuration folder. + """ + + # Make sure relative path references are made absolute if not os.path.isabs(path): path = os.path.abspath(path) + # Verify that the file exists - if os.path.isfile(path): - log.info("Specified path is file and exists") - else: + if not os.path.isfile(path): if os.path.isdir(path): - log.warning("Specified path is directory, not file!") + log.warning("The specified path is a directory, not a file.") else: - log.warning("Specified path doesn't exist!") - log.warning("Please correct the path before trying again.") - click.exit() - # Identify the final location of the file in the config directory - _, head = os.path.split(path) - new_path = os.path.join(config.SCRIPT_ROOT, "config", head) - # MOVE the file - if move: - shutil.move(path, new_path) - log.info("Successfully moved file to configuration file.") - # COPY the file - elif not move: - # May be something to think about - should we copy metadata, permissions, etc? Probably not. - shutil.copy(path, new_path) - log.info("Successfully copied file to configuration folder.") - config.config["google"]["credentials"] = head - config.quicksave() - log.info(f"Key file configuration updated.") + log.warning("The specified path does not exist.") + else: + # Identify the final location of the file in the config directory + _, head = os.path.split(path) + new_path = os.path.join(config.SCRIPT_ROOT, "config", head) + + # Move or copy the file + if move: + shutil.move(path, new_path) + log.info("Successfully moved file to configuration file...") + elif not move: + shutil.copy(path, new_path) + log.info("Successfully copied file to configuration folder...") + + # Update the configuration file to point to the + config.config["google"]["credentials"] = head + config.quicksave() + + log.info(f"Configuration updated.") diff --git a/phototag/config.py b/phototag/config.py index 13b5684..2de1780 100644 --- a/phototag/config.py +++ b/phototag/config.py @@ -1,22 +1,40 @@ +""" +config.py + +Assist with creating, accessing and saving to a configuration file located in the script installation folder. +""" + import configparser import os -SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__)) -CONFIG_DIR = os.path.join(SCRIPT_ROOT, "config") -CONFIG_PATH = os.path.join(CONFIG_DIR, "config.ini") +SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__)) # Script installation folder +CONFIG_DIR = os.path.join(SCRIPT_ROOT, "config") # Configuration file folder +CONFIG_PATH = os.path.join(CONFIG_DIR, "config.ini") # Configuration file config = configparser.ConfigParser() def quicksave(): + """Simple function for saving current configuration state to file""" with open(CONFIG_PATH, "w+") as file: config.write(file) +# If file does not exist if not os.path.exists(CONFIG_PATH): + # If folder does not exist if not os.path.exists(CONFIG_DIR): os.makedirs(CONFIG_DIR) + + # Default configuration data config["google"] = {"credentials": ""} + config["limits"] = { + "image_count": 16, # 16 images in memory at any time + "buffer_size": "256 MB", # 256 MB of images in memory at any time, + "single_override": True # disregard previous filters to keep at least 1 image in rotation + } + quicksave() else: + # File exists, so just read with open(CONFIG_PATH, "r") as file: config.read_file(file) diff --git a/requirements.txt b/requirements.txt index b057897..48597aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,5 +5,5 @@ setuptools==40.8.0 Click==7.0 Pillow>=7.1.0 protobuf==3.11.3 -progressbar2==3.47.0 google-cloud-vision~=2.0 +google-api-python-client~=1.12.3 diff --git a/setup.py b/setup.py index 22994c6..e87b544 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,18 @@ -import sys +""" +setup.py + +The installation script responsible for setting up the CLI program on the user's system. +""" + import os -import io +import sys + from setuptools import find_packages, setup DEPENDENCIES = [ "Click", "rawpy", "imageio", - "progressbar2", "iptcinfo3", "google-api-python-client", "google-cloud",