diff options
| -rw-r--r-- | patternutils/commands/tmv.py | 13 | ||||
| -rw-r--r-- | patternutils/utils.py | 22 |
2 files changed, 34 insertions, 1 deletions
diff --git a/patternutils/commands/tmv.py b/patternutils/commands/tmv.py index b8d5d73..5c2da98 100644 --- a/patternutils/commands/tmv.py +++ b/patternutils/commands/tmv.py @@ -6,6 +6,7 @@ import click from .. import template from .. import config +from .. import utils def move_file(old_filename: str, new_filename: str) -> None: @@ -20,13 +21,15 @@ def move_file(old_filename: str, new_filename: str) -> None: @click.command(context_settings={'help_option_names': ('-h', '--help', '-?')}) @click.option('-i', '--interactive', is_flag=True, default=False, help='prompt before renaming files') +@click.option('-e', '--editor', is_flag=True, default=False, + help='Open an editor before renaming files') @click.option('-f', '--force', is_flag=True, default=False, help='force rename') @click.option('-t', '--template-engine', default='python', type=click.Choice(['python', 'jinja2']), help='select the template engine to use') @click.option('-k', '--key', default='_abspath', help='select the key to use as the source file') -@click.option('-s', '--stream', if_flag=True, default=False, +@click.option('-s', '--stream', is_flag=True, default=False, help='Input is a stream of JSON objects') @click.option('-p', '--use-predefined-template', is_flag=True, default=False, help='use predefined template') @@ -42,6 +45,7 @@ def move_file(old_filename: str, new_filename: str) -> None: def tmv( output_pattern: str, interactive: bool, + editor: bool, force: bool, template_engine: str, abort_on_path_exist: bool, @@ -91,6 +95,13 @@ def tmv( new_filenames = dict((entry[key], render(**entry)) for entry in data) + if editor: + try: + new_filenames = utils.edit(new_filenames) + except RuntimeError: + click.secho('You edit is not parseable!', fg='red', err=True) + raise SystemExit + has_existing = False non_existing = {} for old, new in new_filenames.items(): diff --git a/patternutils/utils.py b/patternutils/utils.py index da64edb..0c514e3 100644 --- a/patternutils/utils.py +++ b/patternutils/utils.py @@ -1,6 +1,28 @@ import os +import click + def max_procs() -> int: cpu_count = os.cpu_count() return 1 if cpu_count is None else cpu_count * 2 + + +def edit(data: dict) -> dict: + + longest = len(max(data, key=len)) + temporary = ['{}\t{}\n'.format(key.ljust(longest), value) for key, value in data.items()] + edited = click.edit(''.join(temporary)) + + if edited is not None: + new_data = dict() + for line in edited.split('\n'): + if line and '\t' in line: + old, new = line.split('\t') + new_data[old.strip()] = new + if new_data: + return new_data + else: + raise RuntimeError + else: + return data |
