Skip to content
Open
Show file tree
Hide file tree
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
50 changes: 34 additions & 16 deletions json_include.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# coding: utf-8

import os
Expand All @@ -23,32 +23,54 @@ def read_file(filepath):


def get_include_name(value):
if isinstance(value, basestring):
if isinstance(value, str):
rv = INCLUDE_VALUE_PATTERN.search(value)
if rv:
return rv.groups()[0]
return None


def is_include(o):
return set(o) == set([INCLUDE_KEY])


def cache_include(include_name, dirpath):
if include_name:
if include_name not in _included_cache:
_included_cache[include_name] = parse_json_include(dirpath, include_name, True)


def walk_through_to_include(o, dirpath):
if isinstance(o, dict):
is_include_exp = False
if set(o) == set([INCLUDE_KEY]):
include_name = get_include_name(o.values()[0])
if include_name:
is_include_exp = True
o.clear()
if include_name not in _included_cache:
_included_cache[include_name] = parse_json_include(dirpath, include_name, True)
o.update(_included_cache[include_name])
if is_include(o):
include_name = get_include_name(list(o.values())[0])
cache_include(include_name, dirpath)
o.clear()
o.update(_included_cache[include_name])
is_include_exp = True

if is_include_exp:
return

for k, v in o.iteritems():
for k, v in o.items():
if isinstance(v, OBJECT_TYPES):
walk_through_to_include(v, dirpath)
elif isinstance(o, list):
is_include_exp = False
if len(o) == 1 and isinstance(o[0], dict):
i = o[0]
if is_include(i):
include_name = get_include_name(list(i.values())[0])
cache_include(include_name, dirpath)
if isinstance(_included_cache[include_name], list):
is_include_exp = True
o.clear()
o.extend(_included_cache[include_name])

if is_include_exp:
return

for i in o:
if isinstance(i, OBJECT_TYPES):
walk_through_to_include(i, dirpath)
Expand All @@ -59,10 +81,6 @@ def parse_json_include(dirpath, filename, is_include=False):
json_str = read_file(filepath)
d = json.loads(json_str, object_pairs_hook=OrderedDict)

if is_include:
assert isinstance(d, dict),\
'The JSON file being included should always be a dict rather than a list'

walk_through_to_include(d, dirpath)

return d
Expand Down Expand Up @@ -113,7 +131,7 @@ def main():

args = parser.parse_args()

print build_json_include(args.dirpath, args.filename)
print (build_json_include(args.dirpath, args.filename))


if __name__ == '__main__':
Expand Down
10 changes: 10 additions & 0 deletions test/expect/d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"aa": {
"a0": 1,
"a1": "one"
},
"b0": {
"value": "a b0 value"
},
"b1": true
}
25 changes: 25 additions & 0 deletions test/expect/e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"c0": {
"value": "a c0 value",
"aaa": {
"a0": 1,
"a1": "one"
}
},
"bb": [
{
"aa": {
"a0": 1,
"a1": "one"
}
},
{
"b0": {
"value": "a b0 value"
}
},
{
"b1": true
}
]
}
4 changes: 2 additions & 2 deletions test/json_include_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# coding: utf-8

import sys
Expand Down Expand Up @@ -28,5 +28,5 @@ def test_build_json_include():
def run_build_json_include(dirpath, i):
rv = build_json_include(dirpath, i)
expect = read_file(os.path.join(_path('expect'), i))
print rv
print(rv)
assert rv.strip() == expect.strip()
15 changes: 15 additions & 0 deletions test/source/d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"aa": {
"...": "<a-0.json>"
}
},
{
"b0": {
"value": "a b0 value"
}
},
{
"b1": true
}
]
13 changes: 13 additions & 0 deletions test/source/e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"c0": {
"value": "a c0 value",
"aaa": {
"...": "<a-0.json>"
}
},
"bb": [
{
"...": "<d.json>"
}
]
}