3 Commits

Author SHA1 Message Date
cd8117343b Merge branch 'develop' 2020-03-09 12:26:00 +00:00
306eb82237 Merge branch 'develop' 2020-03-09 12:22:43 +00:00
5aefcc2a2d Merge branch 'develop' 2020-03-09 12:12:28 +00:00
2 changed files with 22 additions and 31 deletions

View File

@@ -6,64 +6,55 @@ import click
from musicbrainzapi.__version__ import __version__ from musicbrainzapi.__version__ import __version__
from musicbrainzapi.__header__ import __header__ from musicbrainzapi.__header__ import __header__
# pylint:disable=invalid-name CONTEXT_SETTINGS = dict(auto_envvar_prefix='COMPLEX')
CONTEXT_SETTINGS = dict(auto_envvar_prefix="COMPLEX")
class Environment: class Environment(object):
"""Environment class to house shared parameters between all subcommands."""
def __init__(self): def __init__(self):
self.verbose = False self.verbose = False
self.home = os.getcwd() self.home = os.getcwd()
pass_environment = click.make_pass_decorator( pass_environment = click.make_pass_decorator(Environment, ensure=True)
Environment, ensure=True
)
cmd_folder = os.path.abspath( cmd_folder = os.path.abspath(
os.path.join(os.path.dirname(__file__), "commands") os.path.join(os.path.dirname(__file__), 'commands')
) )
class ComplexCLI(click.MultiCommand): class ComplexCLI(click.MultiCommand):
"""Access and run subcommands."""
def list_commands(self, ctx): def list_commands(self, ctx):
"""List all subcommands.""" rv = []
rv = [ for filename in os.listdir(cmd_folder):
filename[4:-3] if filename.endswith('.py') and filename.startswith('cmd_'):
for filename in os.listdir(cmd_folder) rv.append(filename[4:-3])
if filename.endswith(".py") and filename.startswith("cmd_")
]
rv.sort() rv.sort()
return rv return rv
def get_command(self, ctx, cmd_name): def get_command(self, ctx, cmd_name):
"""Get chosen subcummands.""" mod = import_module(f'musicbrainzapi.cli.commands.cmd_{cmd_name}')
mod = import_module(f"musicbrainzapi.cli.commands.cmd_{cmd_name}")
return getattr(mod, cmd_name) return getattr(mod, cmd_name)
@click.command(cls=ComplexCLI, context_settings=CONTEXT_SETTINGS) @click.command(cls=ComplexCLI, context_settings=CONTEXT_SETTINGS)
@click.option( @click.option(
"-p", '-p',
"--path", '--path',
type=click.Path(exists=True, file_okay=False, resolve_path=True, writable=True), type=click.Path(
help="Local path to save any output files.", exists=True, file_okay=False, resolve_path=True, writable=True
default=os.getcwd(), ),
help='Local path to save any output files.',
default=os.getcwd()
) )
@click.option("-v", "--verbose", is_flag=True, help="Enables verbose mode.") # @click.option('-v', '--verbose', is_flag=True, help='Enables verbose mode.')
@click.version_option( @click.version_option(
version=__version__, version=__version__,
prog_name=__header__, prog_name=__header__,
message=f"{__header__} version {__version__} 🎤", message=f'{__header__} version {__version__} 🎤',
) )
@pass_environment @pass_environment
def cli(ctx, verbose, path): def cli(ctx, path):
"""Display base command for the musicbrainzapi program.""" """Base command for the musicbrainzapi program."""
ctx.verbose = verbose # ctx.verbose = verbose
if path is not None: if path is not None:
click.echo(f"Path set to {os.path.expanduser(path)}") click.echo(f'Path set to {os.path.expanduser(path)}')
ctx.path = os.path.expanduser(path) ctx.path = os.path.expanduser(path)

Binary file not shown.