this repo has no description
at develop 1.6 kB view raw
1import json, copy 2 3 ######################## Methods to store program config in JSON ######## 4 ######################## Config can be incrementable, see mergeJSON ##### 5 ########################### JSON CONFIG & STUFF ######################### 6n_JSON_Indent = 4 7s_AddKey = "ADD_KEY::" ## The prefix for keys to be added in new backend definitions 8 ## The prefix itself is not added 9 ## TODO: remove in lvalue 10s_CommentKey = "///COMMENT" ## The key for comments in JSON config dictionaries 11 12## Returns a merge of j2 into j1. 13def mergeJSON( j1, j2 ): 14 jNew = None 15 if dict==type(j1) and dict==type(j2): 16 jNew = copy.deepcopy( j1, {} ) 17 for key in j2: 18 if key in j1: 19 jNew[key] = mergeJSON( j1[key], j2[key] ) 20 else: 21 assert key.startswith( s_AddKey ), "Adding key %s: does not start with the prefix %s, j1: %s" % (key, s_AddKey, j1.__str__()) 22 jNew[key[len(s_AddKey):]] = j2[key] 23 elif list==type(j1) and list==type(j2): 24 jNew = [] 25 for i in range( 0, len(j2) ): ## Can assign a shorter list this way 26 if i<len(j1): 27 jNew.append( mergeJSON( j1[i], j2[i] ) ) 28 else: 29 jNew.append( j2[i] ) 30 elif type(j1)==type(j2): 31 jNew = j2 32 else: 33 print( "\n\n ERROR: Could not merge JSON objects:" ); 34 print( json.dumps( j1 ) ) 35 print( "\n -- TO BE MERGED BY --\n" ) 36 print( json.dumps( j2 ) ) 37 sys.quit() 38 return jNew 39