11
11
# struct to store the build info for jobs from parsed commandline args
12
12
class BuildInfo :
13
13
inputs = [] # list of files
14
+ import_dirs = [] # lst of import directories to search
14
15
output_dir = "" # output directory
15
16
print_out = False # print out the resulting json from jsn to the console
16
17
@@ -27,9 +28,15 @@ def parse_args():
27
28
info .inputs .append (sys .argv [j ])
28
29
j = j + 1
29
30
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" :
31
38
info .output_dir = sys .argv [i + 1 ]
32
- if sys .argv [i ] == "-p" :
39
+ elif sys .argv [i ] == "-p" :
33
40
info .print_out = True
34
41
return info
35
42
@@ -40,6 +47,7 @@ def display_help():
40
47
print (" -help display this message" )
41
48
print (" -i list of input files or directories to process" )
42
49
print (" -o output file or directory " )
50
+ print (" -I list of import directories, to search for imports" )
43
51
print (" -p print output to console " )
44
52
45
53
@@ -504,7 +512,7 @@ def inherit_dict_recursive(d, d2):
504
512
505
513
506
514
# finds files to import (includes)
507
- def get_imports (jsn , filedir ):
515
+ def get_imports (jsn , import_dirs ):
508
516
imports = []
509
517
bp = jsn .find ("{" )
510
518
head = jsn [:bp ].split ("\n " )
@@ -514,14 +522,22 @@ def get_imports(jsn, filedir):
514
522
has_imports = True
515
523
if not has_imports :
516
524
return jsn [bp :], imports
517
- if not filedir :
525
+ if not import_dirs :
518
526
filedir = os .getcwd ()
519
527
print ("WARNING: jsn loads() import file paths will be relative to cwd " + filedir )
520
528
print ("\t use load_from_file() for import paths relative to the jsn file." )
521
529
for i in head :
522
530
if i .find ("import" ) != - 1 :
523
531
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 )
525
541
return jsn [bp :], imports
526
542
527
543
@@ -622,15 +638,16 @@ def resolve_platform_keys(d):
622
638
623
639
624
640
# load from file
625
- def load_from_file (filepath ):
641
+ def load_from_file (filepath , import_dirs ):
626
642
jsn_contents = open (filepath ).read ()
627
643
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 )
629
646
630
647
631
648
# 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 )
634
651
jsn = remove_comments (jsn )
635
652
jsn = change_quotes (jsn )
636
653
jsn = collapse_line_breaks (jsn )
@@ -651,7 +668,7 @@ def loads(jsn, filedir=None):
651
668
652
669
# import
653
670
for i in imports :
654
- include_dict = loads (open (i , "r" ).read ())
671
+ include_dict = loads (open (i , "r" ).read (), import_dirs )
655
672
inherit_dict (j , include_dict )
656
673
657
674
# resolve platform specific keys
@@ -670,15 +687,14 @@ def loads(jsn, filedir=None):
670
687
def convert_jsn (info , input_file , output_file ):
671
688
print ("converting: " + input_file + " to " + output_file )
672
689
output_file = open (output_file , "w+" )
673
- jdict = load_from_file (input_file )
690
+ jdict = load_from_file (input_file , info . import_dirs )
674
691
if info .print_out :
675
692
print (json .dumps (jdict , indent = 4 ))
676
693
output_file .write (json .dumps (jdict , indent = 4 ))
677
694
output_file .close ()
678
695
679
696
680
- # output .jsn files as json,
681
- if __name__ == "__main__" :
697
+ def main ():
682
698
info = parse_args ()
683
699
if len (info .inputs ) == 0 or not info .output_dir :
684
700
display_help ()
@@ -699,4 +715,10 @@ def convert_jsn(info, input_file, output_file):
699
715
output_file = os .path .join (info .output_dir , i )
700
716
output_file = change_ext (output_file , ".json" )
701
717
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