From 7466298a2ae4df0f8d0edb5071072cfb52a41c39 Mon Sep 17 00:00:00 2001 From: timoharboe Date: Tue, 14 Mar 2023 12:06:56 +0100 Subject: [PATCH] Converted from Python 2 to Python 3 --- CommonGraphDiffer.py | 47 ++++++++++++++++--------------- demo.py | 23 ++++++++------- diffgraph.py | 2 +- foo.dsx | 57 ++++++++++++++++++++++++++++--------- foo2.dsx | 67 +++++++++++++++++++++++++++++++++++--------- grapher.py | 10 +++---- 6 files changed, 140 insertions(+), 66 deletions(-) diff --git a/CommonGraphDiffer.py b/CommonGraphDiffer.py index dcd5946..ed0a293 100755 --- a/CommonGraphDiffer.py +++ b/CommonGraphDiffer.py @@ -5,6 +5,7 @@ import copy from xml.dom import minidom import codecs +import cmp def getMetaXML(metaData): @@ -120,7 +121,7 @@ def booleanObjectLists(objType, selfObjs, otherObjs): for selfObj in objsIntersection: myDict = recursive_dict(selfObj.MetaData) otherObj = [obj for obj in otherObjs if obj.InstanceGuid == selfObj.InstanceGuid][0] - if( cmp(recursive_dict(otherObj.MetaData), myDict) == 0): #it's the samE! + if recursive_dict(otherObj.MetaData) == myDict: # it's the same! objsSame.append(otherObj) else: objsChanged.append(otherObj) @@ -166,21 +167,21 @@ def add(self, objType, obj): # print "list of all NodeGUIs", [n.InstanceGuid for n in self.Nodes] parentNode = [n for n in self.Nodes if n.InstanceGuid == obj.ParentGuid][0] except Exception as e: - print e + print(e) raise ValueError("Invalid input! Apply of diff not possible") parentNode.addPort(obj) return True def removeObj(self, objType, objGuid, objParentGuid=None): - if(objType == "node"): + if objType == "node": objList = self.Nodes - if(objType == "edge"): + elif objType == "edge": objList = self.Edges - if(objType == "port"): + elif objType == "port": try: parentNode = [node for node in self.Nodes if node.InstanceGuid == objParentGuid][0] objList = parentNode.Ports - except Exception, e: + except Exception as e: # this sometimes doesn't work because parentNode was already deleted - and that's okay! return True try: @@ -189,25 +190,27 @@ def removeObj(self, objType, objGuid, objParentGuid=None): return False return True - def changeObj(self, objType, obj, objParentGuid = None): - if(objType == "node"): + def changeObj(self, objType, obj, objParentGuid=None): + if objType == "node": objList = self.Nodes - if(objType == "edge"): + elif objType == "edge": objList = self.Edges - if(objType == "port"): + elif objType == "port": try: parentNode = [node for node in self.Nodes if node.InstanceGuid == objParentGuid][0] objList = parentNode.Ports - except Exception, e: + except Exception as e: # if this doesn't work, this is bad return False try: for idx, thisN in enumerate(objList): if obj == thisN: objList[idx].MetaData = obj.MetaData - else: return False + break + else: + return False return True - except Exception, e: + except Exception as e: return False def getAllPorts(self): @@ -215,7 +218,7 @@ def getAllPorts(self): def diff(self, other): # If the metadata changed, include the other's metadata - if cmp(recursive_dict(self.MetaData), recursive_dict(other.MetaData)) !=0: + if recursive_dict(self.MetaData) != recursive_dict(other.MetaData): thisDiffSet = DiffSet(other.MetaData) else: thisDiffSet = DiffSet() @@ -266,7 +269,7 @@ def applyDiff(self, diffSet): newCG = copy.deepcopy(self) for thisNodeChange in [change for change in diffSet.Changes if change.__class__.__name__ == "NodeChange"]: - print thisNodeChange + print(thisNodeChange) if(thisNodeChange.Status == "added"): newCG.add("node", Node.addFromChange(thisNodeChange)) if(thisNodeChange.Status == "removed"): @@ -275,7 +278,7 @@ def applyDiff(self, diffSet): newCG.changeObj("node", Node.addFromChange(thisNodeChange)) for thisPortChange in [change for change in diffSet.Changes if change.__class__.__name__ == "PortChange"]: - print thisPortChange + print(thisPortChange) if(thisPortChange.Status == "added"): newCG.add("port", Port.addFromChange(thisPortChange)) if(thisPortChange.Status == "removed"): @@ -284,7 +287,7 @@ def applyDiff(self, diffSet): newCG.changeObj("port", Port.addFromChange(thisPortChange), objParentGuid = thisPortChange.ParentGuid) for thisEdgeChange in [change for change in diffSet.Changes if change.__class__.__name__ == "EdgeChange"]: - print thisEdgeChange + print(thisEdgeChange) if(thisEdgeChange.Status == "added"): newCG.add("edge", Edge.addFromChange(thisEdgeChange)) if(thisEdgeChange.Status == "removed"): @@ -339,9 +342,9 @@ def getGraphVizRep(node): def __repr__(self): s = '\n (#) Node (InstanceGuid: ' + self.InstanceGuid + ' Type: ' + self.Type if self.Position is not None: - s += ' Position:' + etree.tostring(self.Position) + s += ' Position:' + etree.tostring(self.Position).decode() if self.MetaData is not None: - s += ' MetaData: ' + etree.tostring(self.MetaData) + s += ' MetaData: ' + etree.tostring(self.MetaData).decode() s += ' ) \n' + '\n'.join([str(p) for p in self.Ports]) return s @@ -377,7 +380,7 @@ def __eq__(self, other): def __repr__(self): s = '\n * Port (InstanceGuid: ' + self.InstanceGuid + ' ParentGuid: ' + self.ParentGuid if self.MetaData is not None: - s += ' MetaData: ' + etree.tostring(self.MetaData) + s += ' MetaData: ' + etree.tostring(self.MetaData).decode() s += ' )' return s @classmethod @@ -462,7 +465,7 @@ def CGToXML(cg, fileName): def prettify(elem): # Return a pretty-printed XML string for the Element. - rough_string = etree.tostring(elem, 'utf-8') + rough_string = etree.tostring(elem, 'utf-8').decode() reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent="\t") @@ -485,6 +488,6 @@ def XMLToDS(fileName): elif xmlChange.tag == "EdgeChange": change = EdgeChange(status, xmlChange.get("InstanceGuid"), xmlChange.get("SrcGuid"), xmlChange.get("DstGuid")) else: - print "!!!!!unknown tag", xmlChange.tag + print("!!!!!unknown tag {}".format(xmlChange.tag)) thisDiffSet.addChange(change) return thisDiffSet diff --git a/demo.py b/demo.py index b5027ce..8439ab4 100644 --- a/demo.py +++ b/demo.py @@ -2,27 +2,26 @@ def main(): CGA = cgd.CgxToObject("examples/simple_multiply_example.cgx") - CGB = cgd.CgxToObject("examples/simple_multiply_example_b.cgx") ds = CGA.diff(CGB) cgd.DSToXML(ds, "foo.dsx") ds2 = cgd.XMLToDS("foo.dsx") cgd.DSToXML(ds2, "foo2.dsx") - print "=========================" + print("=========================") CGB2 = CGA.applyDiff(ds) - print "=========================" + print("=========================") CGB3 = CGA.applyDiff(ds2) - print "=CGA========================" - print CGA - print "=CGB========================" - print CGB - print "=CGB2========================" - print CGB2 - print "=CGB3========================" - print CGB3 - print "=========================" + print("=CGA========================") + print(CGA) + print("=CGB========================") + print(CGB) + print("=CGB2========================") + print(CGB2) + print("=CGB3========================") + print(CGB3) + print("=========================") if __name__ == "__main__": main() diff --git a/diffgraph.py b/diffgraph.py index 69bd8cc..693bf7c 100755 --- a/diffgraph.py +++ b/diffgraph.py @@ -13,7 +13,7 @@ def main(): CGA = cgd.CgxToObject(args.cg1) CGB = cgd.CgxToObject(args.cg2) ds = CGA.diff(CGB) - print ds + print(ds) cgd.DSToXML(ds, args.ds) if __name__ == "__main__": diff --git a/foo.dsx b/foo.dsx index dca44b8..7e51466 100644 --- a/foo.dsx +++ b/foo.dsx @@ -1,13 +1,44 @@ -"I'm a Horse" - "I'm a Cloud" - "I'm a Cloud" - "I'm a Cloud" - "I'm a Cloud" - "I'm a Monkey" - "I'm a Frog" - "I'm a Frog" - "I'm a Frog" - "I'm a Frog" - "I'm a Frog" - "I'm a MAGIC UNICORN" - \ No newline at end of file + + + + + + "I'm a glowing crystal" + + + + + + + "I'm a Horse" + + + + + + + + + + + + "I'm a Monkey" + + + + + + + "I'm a cold bottle of beer" + + + + + + + "I'm a MAGIC UNICORN" + + + + + diff --git a/foo2.dsx b/foo2.dsx index dca44b8..e609c7d 100644 --- a/foo2.dsx +++ b/foo2.dsx @@ -1,13 +1,54 @@ -"I'm a Horse" - "I'm a Cloud" - "I'm a Cloud" - "I'm a Cloud" - "I'm a Cloud" - "I'm a Monkey" - "I'm a Frog" - "I'm a Frog" - "I'm a Frog" - "I'm a Frog" - "I'm a Frog" - "I'm a MAGIC UNICORN" - \ No newline at end of file + + + + + + "I'm a glowing crystal" + + + + + + + + + "I'm a Horse" + + + + + + + + + + + + + + "I'm a Monkey" + + + + + + + + + "I'm a cold bottle of beer" + + + + + + + + + "I'm a MAGIC UNICORN" + + + + + + + diff --git a/grapher.py b/grapher.py index ca54216..a729126 100755 --- a/grapher.py +++ b/grapher.py @@ -31,10 +31,10 @@ def graphAddCGX(G, CGX, sameColor ='gray', sameEdgeColor = 'gray', borderColor=' def graphApplyDS(G, CGX, ds, borderColor, addedColor, removedColor, changedColor): G.node_attr['style'] = 'filled' for thisNodeChange in [change for change in ds.Changes if change.__class__.__name__ == "NodeChange"]: - print thisNodeChange + print(thisNodeChange) if(thisNodeChange.Status == "added"): (nodeid, name, position) = cgd.Node.getGraphVizRep(thisNodeChange) - G.node_attr['style'] = 'filled' + G.node_attr['style'] = 'filled' G.add_node(nodeid, color=borderColor, fillcolor=addedColor, label=name, pin="true", pos=position + "!") if(thisNodeChange.Status == "removed"): G.get_node(thisNodeChange.InstanceGuid).attr['fillcolor']=removedColor @@ -42,7 +42,7 @@ def graphApplyDS(G, CGX, ds, borderColor, addedColor, removedColor, changedColo G.get_node(thisNodeChange.InstanceGuid).attr['fillcolor']=changedColor for thisEdgeChange in [change for change in ds.Changes if change.__class__.__name__ == "EdgeChange"]: - print thisEdgeChange + print(thisEdgeChange) if(thisEdgeChange.Status == "added"): # try to find parentEdge from CGX parentEdge = cgd.Edge.parentEdge(CGX, thisEdgeChange, ds) @@ -64,7 +64,7 @@ def main(): G.node_attr['fontsize'] = 10.0 G.node_attr['shape'] = 'rectangle' - print "## generating graph from", args.cg1, "and", args.ds + print("## generating graph from {} and {}".format(args.cg1, args.ds)) borderColor = 'black' sameColor = '#DDDDDD' @@ -77,7 +77,7 @@ def main(): G.layout(prog='neato') # layout with default (neato) G.draw(args.png) - print "## wrote", args.png + print("## wrote {}".format(args.png)) if __name__ == "__main__": main()