summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Fink2019-08-19 23:36:53 +0200
committerDennis Fink2019-08-19 23:36:53 +0200
commitd79cc01a6ac22a0fded7df11f406ea7d71109a2f (patch)
treea8383f514ba1e103abcf271425d42eeaffdcf305
parent046465ab68796a7a89f68ef96da1cbfedc33060d (diff)
downloadpatternutils-d79cc01a6ac22a0fded7df11f406ea7d71109a2f.tar.gz
patternutils-d79cc01a6ac22a0fded7df11f406ea7d71109a2f.zip
Added stream option to tmv
-rw-r--r--patternutils/commands/tmv.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/patternutils/commands/tmv.py b/patternutils/commands/tmv.py
index 8147c11..b8d5d73 100644
--- a/patternutils/commands/tmv.py
+++ b/patternutils/commands/tmv.py
@@ -26,6 +26,8 @@ def move_file(old_filename: str, new_filename: str) -> None:
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,
+ 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'))
@@ -46,20 +48,27 @@ def tmv(
use_predefined_template: bool,
read_from: click.File,
key: str,
+ stream: bool,
dry_run: bool,
verbose: bool,
) -> None:
"""Rename files based on the OUTPUT_PATTERN.
- It read a JSON list containing objects. The object needs to have a
- key named as the option KEY (default: _abspath), which points to
- the file to move. The rest of the keys will be supplied to the
- templating engine to generate the new filename.
+ It read a JSON list containing objects or a stream of objects
+ (if --stream option is defined). The object needs to have a
+ key named as the option KEY (default: _abspath), which points
+ to the file to move. The rest of the keys will be supplied to
+ the templating engine to generate the new filename.
"""
try:
- data = json.load(read_from) # type: ignore
+ if not stream:
+ data = json.load(read_from) # type: ignore
+ else:
+ data = []
+ for line in read_from:
+ data.append(json.loads(line))
except json.JSONDecodeError:
click.secho('JSON not valid!', fg='red', err=True)
raise SystemExit