diff options
| author | Dennis Fink | 2018-04-07 10:32:00 +0200 |
|---|---|---|
| committer | Dennis Fink | 2018-04-07 10:32:00 +0200 |
| commit | d1bccafa9399c74fd6814dff8d11c383078550f0 (patch) | |
| tree | 8a24a441fd1ed67faf843290232f4116e9e88758 | |
| parent | 38d0ad688232ad5dc826d813e0e56edb3d372820 (diff) | |
| download | patternutils-d1bccafa9399c74fd6814dff8d11c383078550f0.tar.gz patternutils-d1bccafa9399c74fd6814dff8d11c383078550f0.zip | |
Added the patterncfg command
Diffstat (limited to '')
| -rw-r--r-- | patternutils/commands/patterncfg.py | 90 | ||||
| -rw-r--r-- | patternutils/utils.py | 12 | ||||
| -rw-r--r-- | setup.py | 1 |
3 files changed, 103 insertions, 0 deletions
diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py new file mode 100644 index 0000000..da90ead --- /dev/null +++ b/patternutils/commands/patterncfg.py @@ -0,0 +1,90 @@ +import os +import os.path + +import click + +from .. import config +from .. import utils + + +@click.group(context_settings={'help_option_names': ('-h', '--help', '-?')}) +def patterncfg(): + pass + + +@patterncfg.command(name='list', help='list all patternutils template files') +def list(): + for d in os.listdir(config.CONFIG_PATH): + click.secho(d, fg='blue') + for f in os.listdir(os.path.join(config.CONFIG_PATH, d)): + click.echo('└── {}'.format(f)) + + +@patterncfg.command(name='add', help='add new template file') +@click.option('-t', '--file-type', default='regex', type=click.Choice(['regex', 'python', 'jinja2']), + help='select the file type to use') +@click.argument('command', type=click.Choice(['pmatch', 'tmv', 'targs'])) +@click.argument('name') +def add( + file_type, + command, + name +) -> None: + if command == 'pmatch': + fileext = 'regex' + else: + if file_type not in ('python', 'jinja2'): + fileext = 'python' + else: + fileext = file_type + + click.edit(filename=os.path.join(config.CONFIG_PATH, command, '.'.join((name, fileext)))) + + +@patterncfg.command(name='remove', help='remove template file') +@click.argument('command', type=click.Choice(['pmatch', 'tmv', 'targs'])) +@click.argument('name') +def remove(command, name): + files = config.find_templates(name, command) + try: + os.remove(files[0]) + except IndexError: + click.secho('Cannot remove {}. It doesn\'t exist!'.format(name), fg='red', err=True) + raise SystemExit + + +@patterncfg.command(name='view', help='view template file') +@click.option('-p', '--program', default=None, type=click.Choice(['pmatch', 'tmv', 'targs'])) +@click.argument('name') +def view(name, program): + + if program is not None: + files = config.find_templates(name, program) + try: + filename = files[0] + except IndexError: + click.secho('File not found!', fg='red', err=True) + raise SystemExit + else: + with utils.chdir(config.CONFIG_PATH): + files = config.find_templates(name) + + if not files: + click.secho('No files found!', fg='red', err=True) + raise SystemExit + elif len(files) != 1: + click.secho('Multiple files found!', fg='red', err=True) + for f in files: + click.secho(f, fg='red', err=True) + raise SystemExit + else: + filename = os.path.join(config.CONFIG_PATH, files[0]) + + try: + with open(filename, encoding='utf-8') as f: + text = f.read() + except FileNotFoundError: + click.secho('File not found!', fg='red', err=True) + raise SystemExit + else: + click.echo_via_pager(text) diff --git a/patternutils/utils.py b/patternutils/utils.py index da64edb..30b05ac 100644 --- a/patternutils/utils.py +++ b/patternutils/utils.py @@ -1,5 +1,17 @@ import os +from contextlib import contextmanager + + +@contextmanager +def chdir(dir): + curdir = os.getcwd() + try: + os.chdir(dir) + yield + finally: + os.chdir(curdir) + def max_procs() -> int: cpu_count = os.cpu_count() @@ -30,6 +30,7 @@ setup( 'pmatch=patternutils.commands.pmatch:pmatch', 'targs=patternutils.commands.targs:targs', 'tmv=patternutils.commands.tmv:tmv', + 'patterncfg=patternutils.commands.patterncfg:patterncfg', ], }, install_requires=REQUIRED, |
