From 062bedbcf0bb26f9f64c7871aac13d7a3184021d Mon Sep 17 00:00:00 2001 From: apalala Date: Sun, 20 May 2018 08:33:44 -0400 Subject: [PATCH 1/3] add support for JSON Lines format in 'target' --- glom/cli.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/glom/cli.py b/glom/cli.py index dac867dd..0cef7e05 100644 --- a/glom/cli.py +++ b/glom/cli.py @@ -34,6 +34,7 @@ import ast import sys import json +from json.decoder import JSONDecodeError from face import Command, Flag, face_middleware, PosArgSpec, PosArgDisplay from face.command import CommandLineError @@ -146,12 +147,21 @@ def mw_get_target(next_, posargs_, target_file, target_format, spec_file, spec_f if not target_text: target = {} + elif target_format == 'python': + if target_text[0] not in ('"', "'", "[", "{", "("): + # intention: handle trivial path access, assume string + spec_text = repr(spec_text) + target = ast.literal_eval(target_text) elif target_format == 'json': try: - target = json.loads(target_text) + try: + target = json.loads(target_text) + except JSONDecodeError: + # intention: handle JSON Lines (.jsonl) format + target = [json.loads(line) for line in target_text.splitlines()] except Exception as e: _error('could not load target data, got: %s' % e) else: - _error('expected spec-format to be one of python or json') + _error('expected target format to be one of python or json') return next_(spec=spec, target=target) From fdf43548e0369b32da4105e9166574f86aa784cf Mon Sep 17 00:00:00 2001 From: apalala Date: Sun, 20 May 2018 08:40:29 -0400 Subject: [PATCH 2/3] fix JSONDecodeError not available in py27 --- glom/cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/glom/cli.py b/glom/cli.py index 0cef7e05..5d6f9a78 100644 --- a/glom/cli.py +++ b/glom/cli.py @@ -34,13 +34,13 @@ import ast import sys import json -from json.decoder import JSONDecodeError -from face import Command, Flag, face_middleware, PosArgSpec, PosArgDisplay +from face import Command, face_middleware, PosArgSpec from face.command import CommandLineError from glom import glom, Path, GlomError, Inspect + def glom_cli(target, spec, indent, debug, inspect): """Command-line interface to the glom library, providing nested data access and data restructuring with the power of Python. @@ -156,7 +156,7 @@ def mw_get_target(next_, posargs_, target_file, target_format, spec_file, spec_f try: try: target = json.loads(target_text) - except JSONDecodeError: + except Exception: # JSONDecodeError not available in 2.7 # intention: handle JSON Lines (.jsonl) format target = [json.loads(line) for line in target_text.splitlines()] except Exception as e: From 5e053fb0ae81239841abdc7754e95e7d66548690 Mon Sep 17 00:00:00 2001 From: apalala Date: Sun, 10 Jun 2018 08:57:07 -0400 Subject: [PATCH 3/3] catch json.loads() exceptions with ValueError --- glom/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glom/cli.py b/glom/cli.py index 5d6f9a78..0a8cdc19 100644 --- a/glom/cli.py +++ b/glom/cli.py @@ -156,10 +156,10 @@ def mw_get_target(next_, posargs_, target_file, target_format, spec_file, spec_f try: try: target = json.loads(target_text) - except Exception: # JSONDecodeError not available in 2.7 + except ValueError: # intention: handle JSON Lines (.jsonl) format target = [json.loads(line) for line in target_text.splitlines()] - except Exception as e: + except ValueError as e: _error('could not load target data, got: %s' % e) else: _error('expected target format to be one of python or json')