def flattenClassDict(cd, retDict=None): """ Given a dict containing a series of nested objects such as would be created by restoring from a cdxml file, returns a dict with all classIDs as keys, and a dict as the corresponding value. The dict value will have keys for the attributes and/or code, depending on what was in the original dict. The end result is to take a nested dict structure and return a flattened dict with all objects at the top level. """ if retDict is None: retDict = {} atts = cd.get("attributes", {}) props = cd.get("properties", {}) kids = cd.get("children", []) code = cd.get("code", {}) classID = atts.get("classID", "") classFile = resolvePath(atts.get("designerClass", "")) superclass = resolvePath(atts.get("superclass", "")) superclassID = atts.get("superclassID", "") if superclassID and os.path.exists(superclass): # Get the superclass info superCD = xmltodict(superclass, addCodeFile=True) flattenClassDict(superCD, retDict) if classID: if os.path.exists(classFile): # Get the class info classCD = xmltodict(classFile, addCodeFile=True) classAtts = classCD.get("attributes", {}) classProps = classCD.get("properties", {}) classCode = classCD.get("code", {}) classKids = classCD.get("children", []) currDict = retDict.get(classID, {}) retDict[classID] = { "attributes": classAtts, "code": classCode, "properties": classProps } retDict[classID].update(currDict) # Now update the child objects in the dict for kid in classKids: flattenClassDict(kid, retDict) else: # Not a file; most likely just a component in another class currDict = retDict.get(classID, {}) retDict[classID] = { "attributes": atts, "code": code, "properties": props } retDict[classID].update(currDict) if kids: for kid in kids: flattenClassDict(kid, retDict) return retDict
def flattenClassDict(cd, retDict=None): """ Given a dict containing a series of nested objects such as would be created by restoring from a cdxml file, returns a dict with all classIDs as keys, and a dict as the corresponding value. The dict value will have keys for the attributes and/or code, depending on what was in the original dict. The end result is to take a nested dict structure and return a flattened dict with all objects at the top level. """ if retDict is None: retDict = {} atts = cd.get("attributes", {}) props = cd.get("properties", {}) kids = cd.get("children", []) code = cd.get("code", {}) classID = atts.get("classID", "") classFile = resolvePath(atts.get("designerClass", "")) superclass = resolvePath(atts.get("superclass", "")) superclassID = atts.get("superclassID", "") if superclassID and os.path.exists(superclass): # Get the superclass info superCD = xmltodict(superclass, addCodeFile=True) flattenClassDict(superCD, retDict) if classID: if os.path.exists(classFile): # Get the class info classCD = xmltodict(classFile, addCodeFile=True) classAtts = classCD.get("attributes", {}) classProps = classCD.get("properties", {}) classCode = classCD.get("code", {}) classKids = classCD.get("children", []) currDict = retDict.get(classID, {}) retDict[classID] = {"attributes": classAtts, "code": classCode, "properties": classProps} retDict[classID].update(currDict) # Now update the child objects in the dict for kid in classKids: flattenClassDict(kid, retDict) else: # Not a file; most likely just a component in another class currDict = retDict.get(classID, {}) retDict[classID] = {"attributes": atts, "code": code, "properties": props} retDict[classID].update(currDict) if kids: for kid in kids: flattenClassDict(kid, retDict) return retDict
def importConnections(pth=None, useHomeDir=False): """Read the connection info in the file passed as 'pth', and return a dict containing connection names as keys and connection info dicts as the values. If 'useHomeDir' is True, any file-based database connections will have their pathing resolved based upon the app's current HomeDirectory. Otherwise, the path will be resolved relative to the connection file itself. """ if pth is None: return None f = fileRef(pth) ch = connHandler() xml.sax.parse(f, ch) ret = ch.getConnectionDict() basePath = pth if useHomeDir: basePath = dabo.dAppRef.HomeDirectory else: basePath = pth for cxn, data in ret.items(): dbtype = data.get("dbtype", "") if dbtype.lower() in FILE_DATABASES: for key, val in data.items(): if key == "database": osp = os.path relpath = utils.resolvePath(val, basePath, abspath=False) pth = pth.decode(dabo.fileSystemEncoding) abspath = osp.abspath(osp.join(osp.split(basePath)[0], relpath)) if osp.exists(abspath): ret[cxn][key] = abspath return ret
def importConnections(pth=None, useHomeDir=False): """Read the connection info in the file passed as 'pth', and return a dict containing connection names as keys and connection info dicts as the values. If 'useHomeDir' is True, any file-based database connections will have their pathing resolved based upon the app's current HomeDirectory. Otherwise, the path will be resolved relative to the connection file itself. """ if pth is None: return None f = fileRef(pth) ch = connHandler() xml.sax.parse(f, ch) ret = ch.getConnectionDict() basePath = pth if useHomeDir: basePath = dabo.dAppRef.HomeDirectory else: basePath = pth for cxn, data in ret.items(): dbtype = data.get("dbtype", "") if dbtype.lower() in FILE_DATABASES: for key, val in data.items(): if key == "database": osp = os.path relpath = utils.resolvePath(val, basePath, abspath=False) pth = pth.decode(dabo.fileSystemEncoding) abspath = osp.abspath( osp.join(osp.split(basePath)[0], relpath)) if osp.exists(abspath): ret[cxn][key] = abspath return ret
def test_Pathing(self): prfx = utils.getPathAttributePrefix() pth = "a/b/file2" self.assertEqual(utils.resolvePath(pth), "a/b/file2") pth2 = "../../file2" self.assertEqual(utils.resolvePath(pth2, "a1/b1"), "../../file2") self.assertEqual(utils.relativePath(pth), "a/b/file2") self.assertEqual(utils.relativePath(pth2), "../../file2") self.assertEqual(utils.relativePath(pth,pth2), "../tmp/relpath_tests_dir/a/b/file2") self.assertEqual(utils.relativePathList(pth,pth2), ["..", "tmp", "relpath_tests_dir", "a", "b", "file2"]) atts = {"Foo": "Bar", "ThePath": "%s../some/file.txt" % prfx} utils.resolveAttributePathing(atts, os.getcwd()) self.assertEqual(atts, {"Foo": "Bar", "ThePath": "../some/file.txt"}) atts = {"Foo": "Bar", "ThePath": "%sa/b/file2" % prfx} utils.resolveAttributePathing(atts, os.getcwd()) self.assertEqual(atts, {"Foo": "Bar", "ThePath": "a/b/file2"}) atts = {"Foo": "Bar", "ThePath": "%s../a/b/file2" % prfx} utils.resolveAttributePathing(atts, "a1/") self.assertEqual(atts, {"Foo": "Bar", "ThePath": "../a/b/file2"})
def test_Pathing(self): prfx = utils.getPathAttributePrefix() pth = "a/b/file2" self.assertEqual(utils.resolvePath(pth), "a/b/file2") pth2 = "../../file2" self.assertEqual(utils.resolvePath(pth2, "a1/b1"), "../../file2") self.assertEqual(utils.relativePath(pth), "a/b/file2") self.assertEqual(utils.relativePath(pth2), "../../file2") self.assertEqual(utils.relativePath(pth, pth2), "../tmp/relpath_tests_dir/a/b/file2") self.assertEqual(utils.relativePathList(pth, pth2), ["..", "tmp", "relpath_tests_dir", "a", "b", "file2"]) atts = {"Foo": "Bar", "ThePath": "%s../some/file.txt" % prfx} utils.resolveAttributePathing(atts, os.getcwd()) self.assertEqual(atts, {"Foo": "Bar", "ThePath": "../some/file.txt"}) atts = {"Foo": "Bar", "ThePath": "%sa/b/file2" % prfx} utils.resolveAttributePathing(atts, os.getcwd()) self.assertEqual(atts, {"Foo": "Bar", "ThePath": "a/b/file2"}) atts = {"Foo": "Bar", "ThePath": "%s../a/b/file2" % prfx} utils.resolveAttributePathing(atts, "a1/") self.assertEqual(atts, {"Foo": "Bar", "ThePath": "../a/b/file2"})