summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--patternutils/commands/patterncfg.py90
-rw-r--r--patternutils/utils.py12
-rw-r--r--setup.py1
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()
diff --git a/setup.py b/setup.py
index 2dfebe4..e1ee462 100644
--- a/setup.py
+++ b/setup.py
@@ -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,