From e23b1999251f05d203d0505b53323804c7a27c4b Mon Sep 17 00:00:00 2001 From: Leo Schick Date: Wed, 6 Dec 2023 18:38:51 +0100 Subject: [PATCH] add click group command, add entry_point mara.commands --- mara_cron/__init__.py | 13 ++++---- mara_cron/cli.py | 75 +++++++++++++++++++++++++++++++++++++++---- setup.cfg | 4 +++ 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/mara_cron/__init__.py b/mara_cron/__init__.py index ba3558c..10327cc 100644 --- a/mara_cron/__init__.py +++ b/mara_cron/__init__.py @@ -20,12 +20,13 @@ def MARA_ACL_RESOURCES(): def MARA_CLICK_COMMANDS(): from . import cli return [ - cli.enable, - cli.disable, - cli.schedule_job, - cli.list_crontab, - cli.write_crontab, - cli.clear_crontab + cli.mara_cron, + cli._enable, + cli._disable, + cli._schedule_job, + cli._list_crontab, + cli._write_crontab, + cli._clear_crontab ] diff --git a/mara_cron/cli.py b/mara_cron/cli.py index 84b1719..60e2828 100644 --- a/mara_cron/cli.py +++ b/mara_cron/cli.py @@ -1,11 +1,18 @@ import sys +from warnings import warn import click from mara_cron import config from . import crontab, job -@click.command() +@click.group() +def mara_cron(): + """Let you sync. mara jobs with your crontab""" + pass + + +@mara_cron.command() @click.option('--job-id', required=True, help='The id of of the cron job.') @click.option('--module', default=None, @@ -25,7 +32,7 @@ def disable(job_id: str, module_name: str): sys.exit(0) -@click.command() +@mara_cron.command() @click.option('--job-id', required=True, help='The id of of the cron job.') @click.option('--module', default=None, @@ -49,7 +56,7 @@ def enable(job_id: str, module_name: str): sys.exit(0) -@click.command() +@mara_cron.command() @click.option('--job-id', required=True, help='The id of of the cron job.') def schedule_job(job_id: str): @@ -65,7 +72,7 @@ def schedule_job(job_id: str): sys.exit(0) -@click.command() +@mara_cron.command() @click.option('--with-changes', default=False, is_flag=True, help='Lists the current crontab including the not written changes.') def list_crontab(with_changes: bool): @@ -79,7 +86,7 @@ def list_crontab(with_changes: bool): print(cron) -@click.command() +@mara_cron.command() def write_crontab(): """ Updates the current crontab """ cron = crontab.generate() @@ -87,7 +94,7 @@ def write_crontab(): cron.write() -@click.command() +@mara_cron.command() def clear_crontab(): """ Removes all mara jobs from the crontab """ cron = crontab.current() @@ -98,3 +105,59 @@ def clear_crontab(): job.delete() cron.write() + + +# Old cli commands to be dropped in 5.0: + +@click.command() +@click.option('--job-id', required=True, + help='The id of of the cron job.') +@click.option('--module', default=None, + help='The module name the cron job.') +def _disable(job_id: str, module_name: str): + """ disables a specific cronjob """ + warn("CLI command ` mara_cron.disable` will be dropped in 5.0. Please use: ` mara-cron disable`") + disable.callback(job_id=job_id, module_name=module_name) + + +@click.command() +@click.option('--job-id', required=True, + help='The id of of the cron job.') +@click.option('--module', default=None, + help='The module name the cron job.') +def _enable(job_id: str, module_name: str): + """ enables a specific cronjob """ + warn("CLI command ` mara_cron.enable` will be dropped in 5.0. Please use: ` mara-cron enable`") + enable.callback(job_id=job_id, module_name=module_name) + + +@click.command() +@click.option('--job-id', required=True, + help='The id of of the cron job.') +def _schedule_job(job_id: str): + """ Schedules a job to run. """ + warn("CLI command ` mara_cron.schedule_job` will be dropped in 5.0. Please use: ` mara-cron schedule_job`") + schedule_job.callback(job_id=job_id) + + +@click.command() +@click.option('--with-changes', default=False, is_flag=True, + help='Lists the current crontab including the not written changes.') +def _list_crontab(with_changes: bool): + """ List the crontab content """ + warn("CLI command ` mara_cron.list_crontab` will be dropped in 5.0. Please use: ` mara-cron list_crontab`") + list_crontab.callback(with_changes=with_changes) + + +@click.command("write_crontab") +def _write_crontab(): + """ Updates the current crontab """ + warn("CLI command ` mara_cron.write_crontab` will be dropped in 5.0. Please use: ` mara-cron write_crontab`") + write_crontab.callback() + + +@click.command("clear_crontab") +def _clear_crontab(): + """ Removes all mara jobs from the crontab """ + warn("CLI command ` mara_cron.clear_crontab` will be dropped in 5.0. Please use: ` mara-cron clear_crontab`") + clear_crontab.callback() diff --git a/setup.cfg b/setup.cfg index 3335a7e..fee3128 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,3 +23,7 @@ test = [options.package_data] mara_cron = **/*.py + +[options.entry_points] +mara.commands = + cron = mara_cron.cli:mara_cron