def makeSqgFromJsonSqg(jsonSqg): """Make sqg object from json-sqg object """ #Fn to parse out variables from top level json def fn(name, defaultObj): obj = defaultObj if name in jsonSqg: obj = jsonSqg[name] assert isinstance(obj, defaultObj.__class__) return obj name = None if "name" in jsonSqg: name = int(jsonSqg["name"]) sqg = Sqg(includes=fn("include", []), name=name, parents=fn("parents", []), sharedVariables=fn("sharedVariables", {})) arrayListTypes = [ arrayListType for arrayListType in jsonSqg.keys() if arrayListType not in \ ("name", "include", "parents", "sharedVariables") ] while len(arrayListTypes) > 0: arrayListType = arrayListTypes.pop() jsonArrayList = jsonSqg[arrayListType] assert isinstance(jsonArrayList, [].__class__) properties = jsonArrayList[0] def fn(name): if name in properties: return properties[name] return None inherits = fn("inherits") if inherits != None and inherits in arrayListTypes: arrayListTypes = [ arrayListType ] + arrayListTypes continue if len(jsonArrayList) not in (1, 2, 3): raise RuntimeError("Got an incorrect number of arguments for a json array list: %s %i" % (jsonArrayList, len(jsonArrayList))) if len(jsonArrayList) in (1, 2): #In memory arrayList = InMemoryArrayList(type=arrayListType, inherits=inherits, sharedVariables=fn("sharedVariables"), variables=fn("variables")) if len(jsonArrayList) == 2: arrays = jsonArrayList[1] arrayWidth = arrayList.getArrayWidth() #Now parse in the different arrays assert len(arrays) % arrayWidth == 0 #Otherwise the length of the array is not divisible by the length of each entry for j in xrange(0,len(arrays),arrayWidth): arrayList.addArray(arrays[j:j+arrayWidth]) else: #On disk arrayList = OnDiskArrayList(file=jsonArrayList[2], type=arrayListType, inherits=inherits, sharedVariables=fn("sharedVariables"), variables=fn("variables")) sqg.setArrayList(arrayList) return sqg