updating latest from mac
This commit is contained in:
16
weather-cli/__dev/complex/README
Normal file
16
weather-cli/__dev/complex/README
Normal file
@@ -0,0 +1,16 @@
|
||||
$ complex_
|
||||
|
||||
complex is an example of building very complex cli
|
||||
applications that load subcommands dynamically from
|
||||
a plugin folder and other things.
|
||||
|
||||
All the commands are implemented as plugins in the
|
||||
`complex.commands` package. If a python module is
|
||||
placed named "cmd_foo" it will show up as "foo"
|
||||
command and the `cli` object within it will be
|
||||
loaded as nested Click command.
|
||||
|
||||
Usage:
|
||||
|
||||
$ pip install --editable .
|
||||
$ complex --help
|
||||
0
weather-cli/__dev/complex/complex/__init__.py
Normal file
0
weather-cli/__dev/complex/complex/__init__.py
Normal file
68
weather-cli/__dev/complex/complex/cli.py
Normal file
68
weather-cli/__dev/complex/complex/cli.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import os
|
||||
import sys
|
||||
import click
|
||||
|
||||
CONTEXT_SETTINGS = dict(auto_envvar_prefix='COMPLEX')
|
||||
|
||||
|
||||
class Environment(object):
|
||||
|
||||
def __init__(self):
|
||||
self.verbose = False
|
||||
self.home = os.getcwd()
|
||||
|
||||
def log(self, msg, *args):
|
||||
"""Logs a message to stderr."""
|
||||
if args:
|
||||
msg %= args
|
||||
click.echo(msg, file=sys.stderr)
|
||||
|
||||
def vlog(self, msg, *args):
|
||||
"""Logs a message to stderr only if verbose is enabled."""
|
||||
if self.verbose:
|
||||
self.log(msg, *args)
|
||||
|
||||
|
||||
pass_environment = click.make_pass_decorator(Environment, ensure=True)
|
||||
cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),
|
||||
'commands'))
|
||||
|
||||
|
||||
class ComplexCLI(click.MultiCommand):
|
||||
|
||||
def list_commands(self, ctx):
|
||||
rv = []
|
||||
for filename in os.listdir(cmd_folder):
|
||||
if filename.endswith('.py') and \
|
||||
filename.startswith('cmd_'):
|
||||
rv.append(filename[4:-3])
|
||||
rv.sort()
|
||||
return rv
|
||||
|
||||
def get_command(self, ctx, name):
|
||||
try:
|
||||
if sys.version_info[0] == 2:
|
||||
name = name.encode('ascii', 'replace')
|
||||
mod = __import__('complex.commands.cmd_' + name,
|
||||
None, None, ['cli'])
|
||||
except ImportError:
|
||||
return
|
||||
return mod.cli
|
||||
|
||||
|
||||
@click.command(cls=ComplexCLI, context_settings=CONTEXT_SETTINGS)
|
||||
@click.option('--home', type=click.Path(exists=True, file_okay=False,
|
||||
resolve_path=True),
|
||||
help='Changes the folder to operate on.')
|
||||
@click.option('-v', '--verbose', is_flag=True,
|
||||
help='Enables verbose mode.')
|
||||
@pass_environment
|
||||
def cli(ctx, verbose, home):
|
||||
"""A complex command line interface."""
|
||||
ctx.verbose = verbose
|
||||
if home is not None:
|
||||
ctx.home = home
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
15
weather-cli/__dev/complex/complex/commands/cmd_init.py
Normal file
15
weather-cli/__dev/complex/complex/commands/cmd_init.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import click
|
||||
from complex.cli import pass_environment
|
||||
|
||||
|
||||
@click.command('init', short_help='Initializes a repo.')
|
||||
@click.argument('path', required=False, type=click.Path(resolve_path=True))
|
||||
@pass_environment
|
||||
def cli(ctx, path):
|
||||
"""Initializes a repository."""
|
||||
print(f'{ctx=}')
|
||||
print(f'{dir(ctx)=}')
|
||||
if path is None:
|
||||
path = ctx.home
|
||||
ctx.log('Initialized the repository in %s',
|
||||
click.format_filename(path))
|
||||
10
weather-cli/__dev/complex/complex/commands/cmd_status.py
Normal file
10
weather-cli/__dev/complex/complex/commands/cmd_status.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import click
|
||||
from complex.cli import pass_environment
|
||||
|
||||
|
||||
@click.command('status', short_help='Shows file changes.')
|
||||
@pass_environment
|
||||
def cli(ctx):
|
||||
"""Shows file changes in the current working directory."""
|
||||
ctx.log('Changed files: none')
|
||||
ctx.vlog('bla bla bla, debug info')
|
||||
16
weather-cli/__dev/complex/complex/text
Normal file
16
weather-cli/__dev/complex/complex/text
Normal file
@@ -0,0 +1,16 @@
|
||||
.
|
||||
├── __init__.py
|
||||
├── __pycache__
|
||||
│ ├── __init__.cpython-38.pyc
|
||||
│ └── cli.cpython-38.pyc
|
||||
├── cli.py
|
||||
├── commands
|
||||
│ ├── __init__.py
|
||||
│ ├── __pycache__
|
||||
│ │ ├── __init__.cpython-38.pyc
|
||||
│ │ ├── cmd_init.cpython-38.pyc
|
||||
│ │ └── cmd_status.cpython-38.pyc
|
||||
│ ├── cmd_init.py
|
||||
│ ├── cmd_status.py
|
||||
│ └── test
|
||||
└── text
|
||||
15
weather-cli/__dev/complex/setup.py
Normal file
15
weather-cli/__dev/complex/setup.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='click-example-complex',
|
||||
version='1.0',
|
||||
packages=['complex', 'complex.commands'],
|
||||
include_package_data=True,
|
||||
install_requires=[
|
||||
'click',
|
||||
],
|
||||
entry_points='''
|
||||
[console_scripts]
|
||||
complex=complex.cli:cli
|
||||
''',
|
||||
)
|
||||
Reference in New Issue
Block a user