From 97adc3f775a1f0133645d41bf0644d56de345853 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 13 Aug 2023 22:27:16 +0200 Subject: [PATCH] Moved application argument parser to an external `platypush.cli` module. --- platypush/app.py | 132 ++------------------------------------------- platypush/cli.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 127 deletions(-) create mode 100644 platypush/cli.py diff --git a/platypush/app.py b/platypush/app.py index bb162489d4..78fc8f4967 100644 --- a/platypush/app.py +++ b/platypush/app.py @@ -1,12 +1,12 @@ -import argparse import logging import os import subprocess import sys -from typing import Optional +from typing import Optional, Sequence from .bus import Bus from .bus.redis import RedisBus +from .cli import parse_cmdline from .config import Config from .context import register_backends, register_plugins from .cron.scheduler import CronScheduler @@ -184,133 +184,11 @@ class Application: self._redis_proc = None @classmethod - def build(cls, *args: str): + def from_cmdline(cls, args: Sequence[str]) -> "Application": """ Build the app from command line arguments. """ - from . import __version__ - - parser = argparse.ArgumentParser() - - parser.add_argument( - '--config', - '-c', - dest='config', - required=False, - default=None, - help='Custom location for the configuration file', - ) - - parser.add_argument( - '--workdir', - '-w', - dest='workdir', - required=False, - default=None, - help='Custom working directory to be used for the application', - ) - - parser.add_argument( - '--logsdir', - '-l', - dest='logsdir', - required=False, - default=None, - help='Store logs in the specified directory. By default, the ' - '`[logging.]filename` configuration option will be used. If not ' - 'set, logging will be sent to stdout and stderr.', - ) - - parser.add_argument( - '--version', - dest='version', - required=False, - action='store_true', - help="Print the current version and exit", - ) - - parser.add_argument( - '--verbose', - '-v', - dest='verbose', - required=False, - action='store_true', - help="Enable verbose/debug logging", - ) - - parser.add_argument( - '--pidfile', - '-P', - dest='pidfile', - required=False, - default=None, - help="File where platypush will " - + "store its PID, useful if you're planning to " - + "integrate it in a service", - ) - - parser.add_argument( - '--no-capture-stdout', - dest='no_capture_stdout', - required=False, - action='store_true', - help="Set this flag if you have max stack depth " - + "exceeded errors so stdout won't be captured by " - + "the logging system", - ) - - parser.add_argument( - '--no-capture-stderr', - dest='no_capture_stderr', - required=False, - action='store_true', - help="Set this flag if you have max stack depth " - + "exceeded errors so stderr won't be captured by " - + "the logging system", - ) - - parser.add_argument( - '--redis-queue', - dest='redis_queue', - required=False, - default=cls._default_redis_queue, - help="Name of the Redis queue to be used to internally deliver messages " - "(default: platypush/bus)", - ) - - parser.add_argument( - '--start-redis', - dest='start_redis', - required=False, - action='store_true', - help="Set this flag if you want to run and manage Redis internally " - "from the app rather than using an external server. It requires the " - "redis-server executable to be present in the path", - ) - - parser.add_argument( - '--redis-host', - dest='redis_host', - required=False, - default=None, - help="Overrides the host specified in the redis section of the " - "configuration file", - ) - - parser.add_argument( - '--redis-port', - dest='redis_port', - required=False, - default=None, - help="Overrides the port specified in the redis section of the " - "configuration file", - ) - - opts, _ = parser.parse_known_args(args) - if opts.version: - print(__version__) - sys.exit(0) - + opts = parse_cmdline(args) return cls( config_file=opts.config, workdir=opts.workdir, @@ -435,7 +313,7 @@ def main(*args: str): """ Application entry point. """ - app = Application.build(*args) + app = Application.from_cmdline(args) app.run() diff --git a/platypush/cli.py b/platypush/cli.py new file mode 100644 index 0000000000..81bc4c35f4 --- /dev/null +++ b/platypush/cli.py @@ -0,0 +1,138 @@ +import argparse +from typing import Sequence + +from platypush.bus.redis import RedisBus + + +def parse_cmdline(args: Sequence[str]) -> argparse.Namespace: + """ + Parse command-line arguments from a list of strings. + """ + parser = argparse.ArgumentParser() + + parser.add_argument( + '--config', + '-c', + dest='config', + required=False, + default=None, + help='Custom location for the configuration file', + ) + + parser.add_argument( + '--workdir', + '-w', + dest='workdir', + required=False, + default=None, + help='Custom working directory to be used for the application', + ) + + parser.add_argument( + '--logsdir', + '-l', + dest='logsdir', + required=False, + default=None, + help='Store logs in the specified directory. By default, the ' + '`[logging.]filename` configuration option will be used. If not ' + 'set, logging will be sent to stdout and stderr.', + ) + + parser.add_argument( + '--version', + dest='version', + required=False, + action='store_true', + help="Print the current version and exit", + ) + + parser.add_argument( + '--verbose', + '-v', + dest='verbose', + required=False, + action='store_true', + help="Enable verbose/debug logging", + ) + + parser.add_argument( + '--pidfile', + '-P', + dest='pidfile', + required=False, + default=None, + help="File where platypush will " + + "store its PID, useful if you're planning to " + + "integrate it in a service", + ) + + parser.add_argument( + '--no-capture-stdout', + dest='no_capture_stdout', + required=False, + action='store_true', + help="Set this flag if you have max stack depth " + + "exceeded errors so stdout won't be captured by " + + "the logging system", + ) + + parser.add_argument( + '--no-capture-stderr', + dest='no_capture_stderr', + required=False, + action='store_true', + help="Set this flag if you have max stack depth " + + "exceeded errors so stderr won't be captured by " + + "the logging system", + ) + + parser.add_argument( + '--redis-queue', + dest='redis_queue', + required=False, + default=RedisBus.DEFAULT_REDIS_QUEUE, + help="Name of the Redis queue to be used to internally deliver messages " + f"(default: {RedisBus.DEFAULT_REDIS_QUEUE})", + ) + + parser.add_argument( + '--start-redis', + dest='start_redis', + required=False, + action='store_true', + help="Set this flag if you want to run and manage Redis internally " + "from the app rather than using an external server. It requires the " + "redis-server executable to be present in the path", + ) + + parser.add_argument( + '--redis-host', + dest='redis_host', + required=False, + default=None, + help="Overrides the host specified in the redis section of the " + "configuration file", + ) + + parser.add_argument( + '--redis-port', + dest='redis_port', + required=False, + default=None, + help="Overrides the port specified in the redis section of the " + "configuration file", + ) + + parser.add_argument( + '--ctrl-sock', + dest='ctrl_sock', + required=False, + default=None, + help="If set, it identifies a path to a UNIX domain socket that " + "the application can use to send control messages (e.g. STOP and " + "RESTART) to its parent.", + ) + + opts, _ = parser.parse_known_args(args) + return opts