diff options
| -rw-r--r-- | patternutils/commands/tmv.py | 19 |
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 |
