diff options
| -rw-r--r-- | patternutils/commands/pmatch.py | 7 | ||||
| -rw-r--r-- | patternutils/commands/targs.py | 8 | ||||
| -rw-r--r-- | patternutils/config.py | 45 |
3 files changed, 59 insertions, 1 deletions
diff --git a/patternutils/commands/pmatch.py b/patternutils/commands/pmatch.py index da32f9c..5633c29 100644 --- a/patternutils/commands/pmatch.py +++ b/patternutils/commands/pmatch.py @@ -10,6 +10,7 @@ from typing import Generator, List, Iterator, Pattern, Any, Tuple, Dict, Match import click from .. import match as mat +from .. import config def walk(dir: str, recursive: bool=False, match_directories: bool=False) -> Generator[os.DirEntry, None, None]: @@ -58,6 +59,8 @@ def apply_regex( help='exit if a name does not match the INPUT_PATTERN') @click.option('--match-full-path', is_flag=True, default=False, help='match on full path instead of only filename') +@click.option('-p', '--use-predefined-pattern', is_flag=True, default=False, + help='use predefinged reges pattern') @click.option('-v', '--verbose', is_flag=True, default=False, help='explain what is being done') @click.version_option() @@ -69,9 +72,13 @@ def pmatch( recursive: bool, abort_on_no_match: bool, match_full_path: bool, + use_predefined_pattern: bool, verbose: bool, ) -> None: + if use_predefined_pattern: + regex_pattern, _ = config.load_template(regex_pattern, 'pmatch') + try: regex_pattern_c = re.compile(regex_pattern) except re.error as e: diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index 59a8ab3..c1db5e4 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -15,6 +15,7 @@ from typing import ( import click from .. import template +from .. import config def run_command( @@ -41,6 +42,8 @@ def run_command( @click.option('-v', '--verbose', is_flag=True, default=False, help='explain what is being done') @click.option('-P', '--max-procs', default=1, type=click.IntRange(min=0, clamp=True)) +@click.option('-p', '--use-predefined-template', is_flag=True, default=False, + help='use predefined template') @click.option('--redirect-stdout', is_flag=True, default=False, help='Redirect stdout to /dev/null') @click.option('--redirect-stderr', is_flag=True, default=False, @@ -55,6 +58,7 @@ def targs( max_procs: int, redirect_stdout: bool, redirect_stderr: bool, + use_predefined_template: bool, verbose: bool, ) -> None: @@ -64,7 +68,9 @@ def targs( click.secho('JSON not valid!', fg='red', err=True) raise SystemExit - use_jinja = True if template_engine == 'jinja2' else False + if use_predefined_template: + template_engine, comand = config.load_template(command, 'targs') + try: render = template.get_render_function(command, engine=template_engine) except RuntimeError: diff --git a/patternutils/config.py b/patternutils/config.py new file mode 100644 index 0000000..a8feb36 --- /dev/null +++ b/patternutils/config.py @@ -0,0 +1,45 @@ +import os.path +import glob + +import click + +from typing import Tuple, List, Union + +CONFIG_PATH = click.get_app_dir('patternutils') + + +def load_template(template_name: str, command_name: str) -> Tuple[str, str]: + try: + files = find_templates(template_name, command_name) + if len(files) == 1: + with open(files[0], encoding='utf-8') as f: + template_lines = f.read() + elif len(files) == 0: + raise FileNotFoundError + elif len(files) > 1: + click.secho('More then one file found!', fg='red', err=True) + raise SystemExit + except FileNotFoundError: + raise + else: + output_pattern = template_lines.replace('\n', '') + template_file = files[0] + if template_file.endswith('regex'): + template_engine = 'regex' + elif template_file.endswith('jinja2'): + template_engine = 'jinja' + else: + template_engine = 'python' + + return output_pattern, template_engine + + +def find_templates(template_name: str, command_name: Union[str, None]) -> List[str]: + template_glob = '.'.join((template_name, '*')) + + if command_name is None: + command_glob = '*' + else: + command_glob = os.path.join(CONFIG_PATH, command_name) + + return glob.glob(os.path.join(command_glob, template_glob)) |
