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

Allow passing literal JSON strings instead of file paths #110

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions singer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ def chunk(array, num):
yield array[i:i + num]


def load_json(path):
with open(path) as fil:
return json.load(fil)
def load_json(path_or_json):
try:
inline_config = json.loads(path_or_json)
except ValueError:
with open(path_or_json) as fil:
return json.load(fil)
return inline_config


def update_state(state, entity, dtime):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from datetime import datetime as dt
import pytz
import logging
import json
import tempfile
import singer.utils as u


Expand Down Expand Up @@ -33,3 +35,28 @@ def test_exception_fn(self):
def foo():
raise RuntimeError("foo")
self.assertRaises(RuntimeError, foo)

class TestLoadJson(unittest.TestCase):
def setUp(self):
self.expected_json = """
{
"key1": false,
"key2": [
{"field1": 366, "field2": "2018-01-01T00:00:00+00:00"}
]
}
"""

def test_inline(self):
inline = u.load_json(self.expected_json)
self.assertEqual(inline, json.loads(self.expected_json))

def test_path(self):
# from valid path
with tempfile.NamedTemporaryFile() as fil:
fil.write(self.expected_json.encode())
fil.seek(0)
from_path = u.load_json(fil.name)
self.assertEqual(from_path, json.loads(self.expected_json))
# from invalid path
self.assertRaises(FileNotFoundError, u.load_json, 'does_not_exist.json')