Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for JSON Lines format in 'target' #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions glom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
import sys
import json

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.
Expand Down Expand Up @@ -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)
except Exception as e:
try:
target = json.loads(target_text)
except ValueError:
# intention: handle JSON Lines (.jsonl) format
target = [json.loads(line) for line in target_text.splitlines()]
except ValueError 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)