diff options
Diffstat (limited to 'patternutils/commands/tmv.py')
| -rw-r--r-- | patternutils/commands/tmv.py | 101 |
1 files changed, 69 insertions, 32 deletions
diff --git a/patternutils/commands/tmv.py b/patternutils/commands/tmv.py index 25932bc..eb6dc99 100644 --- a/patternutils/commands/tmv.py +++ b/patternutils/commands/tmv.py @@ -18,30 +18,65 @@ def move_file(old_filename: str, new_filename: str) -> None: shutil.move(old_filename, new_filename) -@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 to allow manual changes') -@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', 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') -@click.option('--read-from', default='-', type=click.File('r')) -@click.option('--abort-on-path-exist', is_flag=True, default=False, - help='exit if a renamed path already exists') -@click.option('-n', '--dry-run', is_flag=True, default=False, - help='perform a trial run with no changes made') -@click.option('-v', '--verbose', is_flag=True, default=False, - help='explain what is being done') +@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 to allow manual changes", +) +@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", + 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", +) +@click.option("--read-from", default="-", type=click.File("r")) +@click.option( + "--abort-on-path-exist", + is_flag=True, + default=False, + help="exit if a renamed path already exists", +) +@click.option( + "-n", + "--dry-run", + is_flag=True, + default=False, + help="perform a trial run with no changes made", +) +@click.option( + "-v", "--verbose", is_flag=True, default=False, help="explain what is being done" +) @click.version_option() -@click.argument('output-pattern') +@click.argument("output-pattern") def tmv( output_pattern: str, interactive: bool, @@ -72,23 +107,25 @@ def tmv( else: data = [json.loads(line) for line in read_from] except json.JSONDecodeError: - click.secho('JSON not valid!', fg='red', err=True) + click.secho("JSON not valid!", fg="red", err=True) raise SystemExit if use_predefined_template: try: - output_pattern, template_engine = config.load_template(output_pattern, 'tmv') + output_pattern, template_engine = config.load_template( + output_pattern, "tmv" + ) except FileNotFoundError: - click.secho('File not found!', fg='red', err=True) + click.secho("File not found!", fg="red", err=True) raise SystemExit except RuntimeError: - click.secho('Multiple files found!', fg='red', err=True) + click.secho("Multiple files found!", fg="red", err=True) raise SystemExit try: render = template.get_render_function(output_pattern, engine=template_engine) except RuntimeError: - click.secho('jinja2 not installed!', fg='red', err=True) + click.secho("jinja2 not installed!", fg="red", err=True) raise SystemExit new_filenames = dict((entry[key], render(**entry)) for entry in data) @@ -97,14 +134,14 @@ def tmv( try: new_filenames = utils.edit(new_filenames) except RuntimeError: - click.secho('You edit is not parseable!', fg='red', err=True) + 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(): if os.path.exists(new): - click.secho('{} does exist!'.format(new), fg='red', err=True) + click.secho("{} does exist!".format(new), fg="red", err=True) has_existing = True else: non_existing[old] = new @@ -119,8 +156,8 @@ def tmv( for old, new in files_to_move: if verbose or interactive: - click.echo('{} -> {}'.format(old, new)) + click.echo("{} -> {}".format(old, new)) - if not (interactive and not click.confirm('Is the match correct?')): + if not (interactive and not click.confirm("Is the match correct?")): if not dry_run: move_file(old, new) |
