Consolidated common options into custom decorators
This commit is contained in:
parent
f68ce34351
commit
d9f0fb1667
3 changed files with 68 additions and 47 deletions
|
|
@ -8,10 +8,8 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms
|
|||
import click
|
||||
import octoprint
|
||||
|
||||
|
||||
#~~ click context
|
||||
|
||||
|
||||
class OctoPrintContext(object):
|
||||
def __init__(self, configfile=None, basedir=None, debug=False, verbosity=0):
|
||||
self.configfile = configfile
|
||||
|
|
@ -20,10 +18,8 @@ class OctoPrintContext(object):
|
|||
self.verbosity = verbosity
|
||||
pass_octoprint_ctx = click.make_pass_decorator(OctoPrintContext, ensure=True)
|
||||
|
||||
|
||||
#~~ Custom click option to hide from help
|
||||
|
||||
|
||||
class HiddenOption(click.Option):
|
||||
def get_help_record(self, ctx):
|
||||
pass
|
||||
|
|
@ -46,11 +42,7 @@ def hidden_option(*param_decls, **attrs):
|
|||
return f
|
||||
return decorator
|
||||
|
||||
#~~ "octoprint" command, merges server_commands and plugin_commands groups
|
||||
|
||||
from .server import server_commands
|
||||
from .plugins import plugin_commands
|
||||
from .devel import devel_commands
|
||||
#~~ helper for settings context options
|
||||
|
||||
def set_ctx_obj_option(ctx, param, value):
|
||||
"""Helper for setting eager options on the context."""
|
||||
|
|
@ -60,22 +52,56 @@ def set_ctx_obj_option(ctx, param, value):
|
|||
if hasattr(ctx.obj, param.name):
|
||||
setattr(ctx.obj, param.name, value)
|
||||
|
||||
#~~ helper for setting a lot of bulk options
|
||||
|
||||
def bulk_options(options):
|
||||
def decorator(f):
|
||||
options.reverse()
|
||||
for option in options:
|
||||
option(f)
|
||||
return f
|
||||
return decorator
|
||||
|
||||
#~~ helper for setting --basedir, --config and --verbose options
|
||||
|
||||
def standard_options(hidden=False):
|
||||
factory = click.option
|
||||
if hidden:
|
||||
factory = hidden_option
|
||||
|
||||
options = [
|
||||
factory("--basedir", "-b", type=click.Path(), callback=set_ctx_obj_option, is_eager=True, expose_value=False,
|
||||
help="Specify the basedir to use for uploads, timelapses etc."),
|
||||
factory("--config", "-c", "configfile", type=click.Path(), callback=set_ctx_obj_option, is_eager=True, expose_value=False,
|
||||
help="Specify the config file to use."),
|
||||
factory("--verbose", "-v", "verbosity", count=True, callback=set_ctx_obj_option, is_eager=True, expose_value=False,
|
||||
help="Increase logging verbosity"),
|
||||
]
|
||||
|
||||
return bulk_options(options)
|
||||
|
||||
#~~ helper for settings legacy options we still have to support on "octoprint"
|
||||
|
||||
legacy_options = bulk_options([
|
||||
hidden_option("--host", type=click.STRING),
|
||||
hidden_option("--port", type=click.INT),
|
||||
hidden_option("--logging", type=click.Path()),
|
||||
hidden_option("--debug", "-d", is_flag=True),
|
||||
hidden_option("--daemon", type=click.Choice(["start", "stop", "restart"])),
|
||||
hidden_option("--pid", type=click.Path(), default="/tmp/octoprint.pid"),
|
||||
hidden_option("--iknowwhatimdoing", "allow_root", is_flag=True),
|
||||
])
|
||||
|
||||
#~~ "octoprint" command, merges server_commands and plugin_commands groups
|
||||
|
||||
from .server import server_commands
|
||||
from .plugins import plugin_commands
|
||||
from .devel import devel_commands
|
||||
|
||||
@click.group(name="octoprint", invoke_without_command=True, cls=click.CommandCollection,
|
||||
sources=[server_commands, plugin_commands, devel_commands])
|
||||
@click.option("--basedir", "-b", type=click.Path(), callback=set_ctx_obj_option, is_eager=True, expose_value=False,
|
||||
help="Specify the basedir to use for uploads, timelapses etc.")
|
||||
@click.option("--config", "-c", "configfile", type=click.Path(), callback=set_ctx_obj_option, is_eager=True, expose_value=False,
|
||||
help="Specify the config file to use.")
|
||||
@click.option("--verbose", "-v", "verbosity", count=True, callback=set_ctx_obj_option, is_eager=True, expose_value=False,
|
||||
help="Increase logging verbosity")
|
||||
@hidden_option("--debug", "-d", is_flag=True)
|
||||
@hidden_option("--host", type=click.STRING)
|
||||
@hidden_option("--port", type=click.INT)
|
||||
@hidden_option("--logging", type=click.Path())
|
||||
@hidden_option("--daemon", type=click.Choice(["start", "stop", "restart"]))
|
||||
@hidden_option("--pid", type=click.Path(), default="/tmp/octoprint.pid")
|
||||
@hidden_option("--iknowwhatimdoing", "allow_root", is_flag=True)
|
||||
@standard_options()
|
||||
@legacy_options
|
||||
@click.version_option(version=octoprint.__version__)
|
||||
@click.pass_context
|
||||
def octo(ctx, debug, host, port, logging, daemon, pid, allow_root):
|
||||
|
|
|
|||
|
|
@ -10,10 +10,8 @@ import logging
|
|||
|
||||
from octoprint.cli import pass_octoprint_ctx, OctoPrintContext
|
||||
|
||||
|
||||
#~~ "octoprint plugin:command" commands
|
||||
|
||||
|
||||
class OctoPrintPluginCommands(click.MultiCommand):
|
||||
"""
|
||||
Custom `click.MultiCommand` implementation that collects commands from
|
||||
|
|
@ -75,7 +73,6 @@ class OctoPrintPluginCommands(click.MultiCommand):
|
|||
|
||||
return result
|
||||
|
||||
|
||||
@click.group(cls=OctoPrintPluginCommands)
|
||||
@pass_octoprint_ctx
|
||||
def plugin_commands(obj):
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import click
|
|||
import logging
|
||||
import sys
|
||||
|
||||
from octoprint.cli import pass_octoprint_ctx
|
||||
from octoprint.cli import pass_octoprint_ctx, bulk_options, standard_options
|
||||
|
||||
def run_server(basedir, configfile, host, port, debug, allow_root, logging_config, verbosity):
|
||||
from octoprint import init_platform, __display_version__
|
||||
|
|
@ -40,9 +40,23 @@ def run_server(basedir, configfile, host, port, debug, allow_root, logging_confi
|
|||
octoprint_server.run()
|
||||
|
||||
|
||||
#~~ "octoprint serve" and "octoprint daemon" commands
|
||||
#~~ server options
|
||||
|
||||
|
||||
server_options = bulk_options([
|
||||
click.option("--host", type=click.STRING,
|
||||
help="Specify the host on which to bind the server."),
|
||||
click.option("--port", type=click.INT,
|
||||
help="Specify the port on which to bind the server."),
|
||||
click.option("--logging", type=click.Path(),
|
||||
help="Specify the config file to use for configuring logging."),
|
||||
click.option("--iknowwhatimdoing", "allow_root", is_flag=True,
|
||||
help="Allow OctoPrint to run as user root."),
|
||||
click.option("--debug", is_flag=True, help="Enable debug mode"),
|
||||
])
|
||||
|
||||
#~~ "octoprint serve" and "octoprint daemon" commands
|
||||
|
||||
@click.group()
|
||||
@pass_octoprint_ctx
|
||||
def server_commands(obj):
|
||||
|
|
@ -50,16 +64,8 @@ def server_commands(obj):
|
|||
|
||||
|
||||
@server_commands.command(name="serve")
|
||||
@click.option("--host", type=click.STRING,
|
||||
help="Specify the host on which to bind the server.")
|
||||
@click.option("--port", type=click.INT,
|
||||
help="Specify the port on which to bind the server.")
|
||||
@click.option("--logging", type=click.Path(),
|
||||
help="Specify the config file to use for configuring logging.")
|
||||
@click.option("--iknowwhatimdoing", "allow_root", is_flag=True,
|
||||
help="Allow OctoPrint to run as user root.")
|
||||
@click.option("--debug", is_flag=True,
|
||||
help="Enable debug mode")
|
||||
@server_options
|
||||
@standard_options(hidden=True)
|
||||
@pass_octoprint_ctx
|
||||
def serve_command(obj, host, port, logging, allow_root, debug):
|
||||
"""Starts the OctoPrint server."""
|
||||
|
|
@ -70,16 +76,8 @@ def serve_command(obj, host, port, logging, allow_root, debug):
|
|||
@server_commands.command(name="daemon")
|
||||
@click.option("--pid", type=click.Path(), default="/tmp/octoprint.pid",
|
||||
help="Pidfile to use for daemonizing.")
|
||||
@click.option("--host", type=click.STRING,
|
||||
help="Specify the host on which to bind the server.")
|
||||
@click.option("--port", type=click.INT,
|
||||
help="Specify the port on which to bind the server.")
|
||||
@click.option("--logging", type=click.Path(),
|
||||
help="Specify the config file to use for configuring logging.")
|
||||
@click.option("--iknowwhatimdoing", "allow_root", is_flag=True,
|
||||
help="Allow OctoPrint to run as user root.")
|
||||
@click.option("--debug", is_flag=True,
|
||||
help="Enable debug mode")
|
||||
@server_options
|
||||
@standard_options(hidden=True)
|
||||
@click.argument("command", type=click.Choice(["start", "stop", "restart", "status"]),
|
||||
metavar="start|stop|restart|status")
|
||||
@pass_octoprint_ctx
|
||||
|
|
|
|||
Loading…
Reference in a new issue