Skip to content

Commit d79d64c

Browse files
committed
- add -I import dir option for improved imports
1 parent a9851d7 commit d79d64c

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ install:
55
- pip install codecov
66
- pip install coverage
77
script:
8-
- coverage run jsn.py -i example.jsn -o test.json
8+
- coverage run jsn.py -i example.jsn -o test.json -I import_dir
99
after_success:
1010
- codecov

example.jsn

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import import.jsn
2+
import test.jsn
23
{
34
// sytax highlights quite nicely in most editors with c or c++ syntax
45

import_dir/test.jsn

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import import.jsn
2+
{
3+
test_jsn: "included from sub directory"
4+
}

jsn.py

+36-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# struct to store the build info for jobs from parsed commandline args
1212
class BuildInfo:
1313
inputs = [] # list of files
14+
import_dirs = [] # lst of import directories to search
1415
output_dir = "" # output directory
1516
print_out = False # print out the resulting json from jsn to the console
1617

@@ -27,9 +28,15 @@ def parse_args():
2728
info.inputs.append(sys.argv[j])
2829
j = j + 1
2930
i = j
30-
if sys.argv[i] == "-o":
31+
elif sys.argv[i] == "-I":
32+
j = i + 1
33+
while j < len(sys.argv) and sys.argv[j][0] != '-':
34+
info.import_dirs.append(sys.argv[j])
35+
j = j + 1
36+
i = j
37+
elif sys.argv[i] == "-o":
3138
info.output_dir = sys.argv[i + 1]
32-
if sys.argv[i] == "-p":
39+
elif sys.argv[i] == "-p":
3340
info.print_out = True
3441
return info
3542

@@ -40,6 +47,7 @@ def display_help():
4047
print(" -help display this message")
4148
print(" -i list of input files or directories to process")
4249
print(" -o output file or directory ")
50+
print(" -I list of import directories, to search for imports")
4351
print(" -p print output to console ")
4452

4553

@@ -504,7 +512,7 @@ def inherit_dict_recursive(d, d2):
504512

505513

506514
# finds files to import (includes)
507-
def get_imports(jsn, filedir):
515+
def get_imports(jsn, import_dirs):
508516
imports = []
509517
bp = jsn.find("{")
510518
head = jsn[:bp].split("\n")
@@ -514,14 +522,22 @@ def get_imports(jsn, filedir):
514522
has_imports = True
515523
if not has_imports:
516524
return jsn[bp:], imports
517-
if not filedir:
525+
if not import_dirs:
518526
filedir = os.getcwd()
519527
print("WARNING: jsn loads() import file paths will be relative to cwd " + filedir)
520528
print("\t use load_from_file() for import paths relative to the jsn file.")
521529
for i in head:
522530
if i.find("import") != -1:
523531
stripped = i[len("import"):].strip().strip("\"").strip()
524-
imports.append(os.path.join(filedir, stripped))
532+
found = False
533+
for dir in import_dirs:
534+
import_path_dir = os.path.join(dir, stripped)
535+
if os.path.exists(import_path_dir):
536+
imports.append(import_path_dir)
537+
found = True
538+
break
539+
if not found:
540+
print("ERROR: cannot find import file " + stripped)
525541
return jsn[bp:], imports
526542

527543

@@ -622,15 +638,16 @@ def resolve_platform_keys(d):
622638

623639

624640
# load from file
625-
def load_from_file(filepath):
641+
def load_from_file(filepath, import_dirs):
626642
jsn_contents = open(filepath).read()
627643
filepath = os.path.join(os.getcwd(), filepath)
628-
return loads(jsn_contents, os.path.dirname(filepath))
644+
import_dirs.append(os.path.dirname(filepath))
645+
return loads(jsn_contents, import_dirs)
629646

630647

631648
# convert jsn to json
632-
def loads(jsn, filedir=None):
633-
jsn, imports = get_imports(jsn, filedir)
649+
def loads(jsn, import_dirs=None):
650+
jsn, imports = get_imports(jsn, import_dirs)
634651
jsn = remove_comments(jsn)
635652
jsn = change_quotes(jsn)
636653
jsn = collapse_line_breaks(jsn)
@@ -651,7 +668,7 @@ def loads(jsn, filedir=None):
651668

652669
# import
653670
for i in imports:
654-
include_dict = loads(open(i, "r").read())
671+
include_dict = loads(open(i, "r").read(), import_dirs)
655672
inherit_dict(j, include_dict)
656673

657674
# resolve platform specific keys
@@ -670,15 +687,14 @@ def loads(jsn, filedir=None):
670687
def convert_jsn(info, input_file, output_file):
671688
print("converting: " + input_file + " to " + output_file)
672689
output_file = open(output_file, "w+")
673-
jdict = load_from_file(input_file)
690+
jdict = load_from_file(input_file, info.import_dirs)
674691
if info.print_out:
675692
print(json.dumps(jdict, indent=4))
676693
output_file.write(json.dumps(jdict, indent=4))
677694
output_file.close()
678695

679696

680-
# output .jsn files as json,
681-
if __name__ == "__main__":
697+
def main():
682698
info = parse_args()
683699
if len(info.inputs) == 0 or not info.output_dir:
684700
display_help()
@@ -699,4 +715,10 @@ def convert_jsn(info, input_file, output_file):
699715
output_file = os.path.join(info.output_dir, i)
700716
output_file = change_ext(output_file, ".json")
701717
create_dir(output_file)
702-
convert_jsn(info, i, output_file)
718+
convert_jsn(info, i, output_file)
719+
720+
721+
# output .jsn files as json,
722+
if __name__ == "__main__":
723+
main()
724+

0 commit comments

Comments
 (0)