From aa9aaeae9e7e3324f392784dda0d60fe1d5abf32 Mon Sep 17 00:00:00 2001 From: digitalec <31838395+digitalec@users.noreply.github.com> Date: Mon, 28 Aug 2023 22:14:11 -0400 Subject: [PATCH] Added --init argument --- deemon/__init__.py | 2 +- deemon/cli.py | 8 +++++++- deemon/utils/startup.py | 15 +++++++++++++++ docs/docs/commands/commands.md | 20 +++++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/deemon/__init__.py b/deemon/__init__.py index 0134ea9..c62bc32 100644 --- a/deemon/__init__.py +++ b/deemon/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from deemon.utils import startup -__version__ = '2.19.3' +__version__ = '2.20' __dbversion__ = '3.7' appdata = startup.get_appdata_dir() diff --git a/deemon/cli.py b/deemon/cli.py index 1001f1f..cdb63b7 100644 --- a/deemon/cli.py +++ b/deemon/cli.py @@ -30,10 +30,12 @@ @click.group(context_settings=CONTEXT_SETTINGS, invoke_without_command=True, no_args_is_help=True) @click.option('--whats-new', is_flag=True, help="Show release notes from this version") +@click.option('--init', is_flag=True, help="""Initialize deemon application data + directory. Warning: if directory exists, this will delete existing config and database.""") @click.option('-P', '--profile', help="Specify profile to run deemon as") @click.version_option(__version__, '-V', '--version', message='deemon %(version)s') @click.option('-v', '--verbose', is_flag=True, help="Show debug output") -def run(whats_new, verbose, profile): +def run(whats_new, init, verbose, profile): """Monitoring and alerting tool for new music releases using the Deezer API. deemon is a free and open source tool. To report issues or to contribute, @@ -54,6 +56,10 @@ def run(whats_new, verbose, profile): if whats_new: return startup.get_changelog(__version__) + if init: + app_data_path = startup.get_appdata_dir() + startup.reinit_appdata_dir(app_data_path) + config = Config() db = Database() diff --git a/deemon/utils/startup.py b/deemon/utils/startup.py index 7c812b7..8437b8a 100644 --- a/deemon/utils/startup.py +++ b/deemon/utils/startup.py @@ -41,6 +41,21 @@ def init_appdata_dir(appdata): Path(appdata / 'backups').mkdir(exist_ok=True) +def delete_appdata(appdata): + import shutil + try: + shutil.rmtree(appdata) + except OSError as e: + logger.info(f"Error while deleting path: {e}") + +def reinit_appdata_dir(appdata): + if appdata.exists(): + logger.info("Deleting existing application data directory (config, database, etc.)") + delete_appdata(appdata) + logger.info("Initializing new application data directory...") + init_appdata_dir(appdata) + + def get_config(): return get_appdata_dir() / 'config.json' diff --git a/docs/docs/commands/commands.md b/docs/docs/commands/commands.md index 02072b0..4a8ca8d 100644 --- a/docs/docs/commands/commands.md +++ b/docs/docs/commands/commands.md @@ -4,4 +4,22 @@ title: Commands nav_order: 4 has_children: true permalink: /docs/commands ---- \ No newline at end of file +--- + +# arguments +{: .no_toc } + +## Table of contents +{: .no_toc .text-delta } + +1. TOC +{:toc} + +--- +The following arguments may be run directly after `deemon`: + +`--whats-new` - prints current version release notes from GitHub +`--init` - Initializes deemon's application data directory. (*If directory exists, it will be deleted!*) +`-P ID`, `--profile ID` - Uses specified profile ID +`-V` - Prints current version and exits +`-v`, `--verbose` - Show all verbose log messages