Code rewritten, replacing all list containing tests by dict key defining tests
authorlaurent
Thu, 23 Jul 2009 17:50:26 +0200
changeset 379 e4c26ee9c998
parent 377 c41acb1465ee
child 380 9ca678ee827f
Code rewritten, replacing all list containing tests by dict key defining tests
DataTypeEditor.py
graphics/FBD_Objects.py
graphics/LD_Objects.py
graphics/SFC_Objects.py
plcopen/plcopen.py
--- a/DataTypeEditor.py	Fri Jul 10 11:41:07 2009 +0200
+++ b/DataTypeEditor.py	Thu Jul 23 17:50:26 2009 +0200
@@ -163,7 +163,7 @@
                 grid.SetCellEditor(row, col, editor)
                 grid.SetCellRenderer(row, col, renderer)
                 
-                if row in self.Errors and self.Errors[row][0] == colname.lower():
+                if self.Errors.has_key(row) and self.Errors[row][0] == colname.lower():
                     grid.SetCellBackgroundColour(row, col, wx.Colour(255, 255, 0))
                     grid.SetCellTextColour(row, col, wx.RED)
                     grid.MakeCellVisible(row, col)
@@ -204,7 +204,7 @@
         self.Errors = {}
 
 #-------------------------------------------------------------------------------
-#                          Configuration Editor class
+#                          Datatype Editor class
 #-------------------------------------------------------------------------------
 
 [ID_DATATYPEEDITOR, ID_DATATYPEEDITORSTATICBOX,
--- a/graphics/FBD_Objects.py	Fri Jul 10 11:41:07 2009 +0200
+++ b/graphics/FBD_Objects.py	Thu Jul 23 17:50:26 2009 +0200
@@ -240,14 +240,8 @@
                         inputs.append(("IN%d"%start, inputs[-1][1], inputs[-1][2]))
             else:
                 self.Colour = wx.RED
-                if "inputs" in connectors:
-                    inputs = connectors["inputs"]
-                else:
-                    inputs = []
-                if "outputs" in connectors:
-                    outputs = connectors["outputs"]
-                else:
-                    outputs = []
+                inputs = connectors.get("inputs", [])
+                outputs = connectors.get("outputs", [])
             if self.ExecutionControl:
                 inputs.insert(0, ("EN","BOOL","none"))
                 outputs.insert(0, ("ENO","BOOL","none"))
@@ -421,9 +415,9 @@
             # Draw block execution order
             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
                     self.Pos.y + self.Size[1] + 2)
-        if "name" in self.Errors:
+        if self.Errors.has_key("name"):
             HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1])
-        if "type" in self.Errors:
+        if self.Errors.has_key("type"):
             HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1])    
         dc.SetTextForeground(wx.BLACK)
         
@@ -856,7 +850,7 @@
         else:
             name_size = self.NameSize
         
-        # Draw a rectangle with the connection size with arrows in 
+        # Draw a rectangle with the connection size with arrows inside
         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
         arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2)
         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, 
--- a/graphics/LD_Objects.py	Fri Jul 10 11:41:07 2009 +0200
+++ b/graphics/LD_Objects.py	Thu Jul 23 17:50:26 2009 +0200
@@ -669,9 +669,9 @@
         # Draw input and output connectors
         self.Input.Draw(dc)
         self.Output.Draw(dc)
-        if "reference" in self.Errors:
+        if self.Errors.has_key("reference"):
             HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1])
-        if typetext != "" and ("negated" in self.Errors or "rising" in self.Errors or "falling" in self.Errors):
+        if typetext != "" and (self.Errors.has_key("negated") or self.Errors.has_key("rising") or self.Errors.has_key("falling")):
             HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1])
         
 
@@ -967,9 +967,9 @@
         # Draw input and output connectors
         self.Input.Draw(dc)
         self.Output.Draw(dc)
-        if "reference" in self.Errors:
+        if self.Errors.has_key("reference"):
             HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1])
-        if typetext != "" and ("negated" in self.Errors or "rising" in self.Errors or "falling" in self.Errors):
+        if typetext != "" and (self.Errors.has_key("negated") or self.Errors.has_key("rising") or self.Errors.has_key("falling")):
             HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1])
             
         
--- a/graphics/SFC_Objects.py	Fri Jul 10 11:41:07 2009 +0200
+++ b/graphics/SFC_Objects.py	Thu Jul 23 17:50:26 2009 +0200
@@ -913,7 +913,7 @@
         if infos[0] == "priority" and start[0] == 0 and start[1] == 0:
             self.Errors[infos[0]] = (start[1], end[1])
         elif infos[0] == "inline":
-            if infos[0] not in self.Errors:
+            if not self.Errors.has_key(infos[0]):
                 self.Errors[infos[0]] = []
             self.Errors[infos[0]].append((start[1], end[1]))
         else:
@@ -965,9 +965,9 @@
         self.Output.Draw(dc)
         if self.Type == "connection":
             self.Condition.Draw(dc)
-        if "priority" in self.Errors:
+        if self.Errors.has_key("priority"):
             HighlightErrorZone(dc, priority_pos[0], priority_pos[1], priority_size[0], priority_size[1])
-        if "inline" in self.Errors:
+        if self.Errors.has_key("inline"):
             for start, end in self.Errors["inline"]:
                 offset = dc.GetTextExtent(self.Condition[:start])
                 size = dc.GetTextExtent(self.Condition[start:end + 1])
@@ -1681,7 +1681,7 @@
         # Draw input connector
         if self.Input:
             self.Input.Draw(dc)
-        if "target" in self.Errors:
+        if self.Errors.has_key("target"):
             HighlightErrorZone(dc, target_pos[0], target_pos[1], target_size[0], target_size[1])
         
 
@@ -1807,14 +1807,14 @@
             width, height = self.Parent.GetTextExtent(action["qualifier"])
             self.ColSize[0] = max(self.ColSize[0], width + 10)
             row_height = height
-            if "duration" in action:
+            if action.has_key("duration"):
                 width, height = self.Parent.GetTextExtent(action["duration"])
                 row_height = max(row_height, height)
                 self.ColSize[0] = max(self.ColSize[0], width + 10)
             width, height = self.Parent.GetTextExtent(action["value"])
             row_height = max(row_height, height)
             self.ColSize[1] = max(self.ColSize[1], width + 10)
-            if "indicator" in action and action["indicator"] != "":
+            if action.get("indicator", "") != "":
                 width, height = self.Parent.GetTextExtent(action["indicator"])
                 row_height = max(row_height, height)
                 self.ColSize[2] = max(self.ColSize[2], width + 10)
@@ -1882,10 +1882,10 @@
     
     def AddError(self, infos, start, end):
         if infos[0] == "action" and infos[1] < len(self.Actions):
-            if infos[1] not in self.Errors:
+            if not self.Errors.has_key(infos[1]):
                 self.Errors[infos[1]] = {}
             if infos[2] == "inline":
-                if infos[2] not in self.Errors[infos[1]]:
+                if not self.Errors[infos[1]].has_key(infos[2]):
                     self.Errors[infos[1]][infos[2]] = []
                 self.Errors[infos[1]][infos[2]].append((start[1], end[1]))
             else:
@@ -1912,7 +1912,7 @@
                 dc.DrawLine(self.Pos.x, self.Pos.y + i * line_size, 
                     self.Pos.x + self.Size[0], self.Pos.y + i * line_size)
             qualifier_size = dc.GetTextExtent(action["qualifier"])
-            if "duration" in action:
+            if action.has_key("duration"):
                 qualifier_pos = (self.Pos.x + (colsize[0] - qualifier_size[0]) / 2,
                                  self.Pos.y + i * line_size + line_size / 2 - qualifier_size[1])
                 duration_size = dc.GetTextExtent(action["duration"])
@@ -1927,24 +1927,24 @@
             content_pos = (self.Pos.x + colsize[0] + (colsize[1] - content_size[0]) / 2,
                            self.Pos.y + i * line_size + (line_size - content_size[1]) / 2)
             dc.DrawText(action["value"], content_pos[0], content_pos[1])
-            if "indicator" in action:
+            if action.has_key("indicator"):
                 indicator_size = dc.GetTextExtent(action["indicator"])
                 indicator_pos = (self.Pos.x + colsize[0] + colsize[1] + (colsize[2] - indicator_size[0]) / 2,
                                  self.Pos.y + i * line_size + (line_size - indicator_size[1]) / 2)
                 dc.DrawText(action["indicator"], indicator_pos[0], indicator_pos[1])
             if i in self.Errors:
-                if "duration" in self.Errors[i] and "duration" in action:
+                if self.Errors[i].has_key("duration") and action.has_key("duration"):
                     HighlightErrorZone(dc, duration_pos[0], duration_pos[1], duration_size[0], duration_size[1])
-                if "qualifier" in self.Errors[i]:
+                if self.Errors[i].has_key("qualifier"):
                     HighlightErrorZone(dc, qualifier_pos[0], qualifier_pos[1], qualifier_size[0], qualifier_size[1])
-                if "reference" in self.Errors[i]:
+                if self.Errors[i].has_key("reference"):
                     HighlightErrorZone(dc, content_pos[0], content_pos[1], content_size[0], content_size[1])
-                elif "inline" in self.Errors[i]:
+                elif self.Errors[i].has_key("inline"):
                     for start, end in self.Errors[i]["inline"]:
                         offset = dc.GetTextExtent(action["value"][:start])
                         size = dc.GetTextExtent(action["value"][start:end + 1])
                         HighlightErrorZone(dc, content_pos[0] + offset[0], content_pos[1], size[0], size[1])
-                if "indicator" in self.Errors[i]:
+                if self.Errors[i].has_key("indicator"):
                     HighlightErrorZone(dc, indicator_pos[0], indicator_pos[1], indicator_size[0], indicator_size[1])
         # Draw input connector
         self.Input.Draw(dc)
--- a/plcopen/plcopen.py	Fri Jul 10 11:41:07 2009 +0200
+++ b/plcopen/plcopen.py	Thu Jul 23 17:50:26 2009 +0200
@@ -95,14 +95,14 @@
     
     def setfileHeader(self, fileheader):
         self.fileHeader.setcompanyName(fileheader["companyName"])
-        if "companyURL" in fileheader:
+        if fileheader.has_key("companyURL"):
             self.fileHeader.setcompanyURL(fileheader["companyURL"])
         self.fileHeader.setproductName(fileheader["productName"])
         self.fileHeader.setproductVersion(fileheader["productVersion"])
-        if "productRelease" in fileheader:
+        if fileheader.has_key("productRelease"):
             self.fileHeader.setproductRelease(fileheader["productRelease"])
         self.fileHeader.setcreationDateTime(fileheader["creationDateTime"])
-        if "contentDescription" in fileheader:
+        if fileheader.has_key("contentDescription"):
             self.fileHeader.setcontentDescription(fileheader["contentDescription"])
     setattr(cls, "setfileHeader", setfileHeader)
     
@@ -126,15 +126,15 @@
     
     def setcontentHeader(self, contentheader):
         self.contentHeader.setname(contentheader["projectName"])
-        if "projectVersion" in contentheader:
+        if contentheader.has_key("projectVersion"):
             self.contentHeader.setversion(contentheader["projectVersion"])
-        if "modificationDateTime" in contentheader:
+        if contentheader.has_key("modificationDateTime"):
             self.contentHeader.setmodificationDateTime(contentheader["modificationDateTime"])
-        if "organization" in contentheader:
+        if contentheader.has_key("organization"):
             self.contentHeader.setorganization(contentheader["organization"])
-        if "authorName" in contentheader:
+        if contentheader.has_key("authorName"):
             self.contentHeader.setauthor(contentheader["authorName"])
-        if "language" in contentheader:
+        if contentheader.has_key("language"):
             self.contentHeader.setlanguage(contentheader["language"])
         self.contentHeader.setpageSize(*contentheader["pageSize"])
         self.contentHeader.setscaling(contentheader["scaling"])
@@ -149,7 +149,7 @@
     setattr(cls, "getdataType", getdataType)
     
     def appenddataType(self, name):
-        if name in self.CustomTypeHierarchy:
+        if self.CustomTypeHierarchy.has_key(name):
             raise ValueError, "\"%s\" Data Type already exists !!!"%name
         self.types.appenddataTypeElement(name)
         self.AddCustomDataType(self.getdataType(name))
@@ -391,20 +391,20 @@
             basetype_content = datatype.baseType.getcontent()
             if basetype_content["name"] == "derived":
                 typename = basetype_content["value"].getname()
-                if typename in elementnames and name not in self.ElementUsingTree[typename]:
+                if name in self.ElementUsingTree[typename]:
                     self.ElementUsingTree[typename].append(name)
             elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned", "array"]:
                 base_type = basetype_content["value"].baseType.getcontent()
                 if base_type["name"] == "derived":
                     typename = base_type["value"].getname()
-                    if typename in elementnames and name not in self.ElementUsingTree[typename]:
+                    if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]:
                         self.ElementUsingTree[typename].append(name)
             elif basetype_content["name"] == "struct":
                 for element in basetype_content["value"].getvariable():
                     type_content = element.type.getcontent()
                     if type_content["name"] == "derived":
                         typename = type_content["value"].getname()
-                        if typename in elementnames and name not in self.ElementUsingTree[typename]:
+                        if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]:
                             self.ElementUsingTree[typename].append(name)
         # Analyze each pou
         for pou in pous:
@@ -416,14 +416,14 @@
                         vartype_content = var.gettype().getcontent()
                         if vartype_content["name"] == "derived":
                             typename = vartype_content["value"].getname()
-                            if typename in elementnames and name not in self.ElementUsingTree[typename]:
+                            if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]:
                                 self.ElementUsingTree[typename].append(name)
     setattr(cls, "RefreshElementUsingTree", RefreshElementUsingTree)
 
     def GetParentType(self, type):
-        if type in self.CustomTypeHierarchy:
+        if self.CustomTypeHierarchy.has_key(type):
             return self.CustomTypeHierarchy[type]
-        elif type in TypeHierarchy:
+        elif TypeHierarchy.has_key(type):
             return TypeHierarchy[type]
         return None
     setattr(cls, "GetParentType", GetParentType)
@@ -465,7 +465,7 @@
 
     # Return if pou given by name is used by another pou
     def ElementIsUsed(self, name):
-        if name in self.ElementUsingTree:
+        if self.ElementUsingTree.has_key(name):
             return len(self.ElementUsingTree[name]) > 0
         return False
     setattr(cls, "ElementIsUsed", ElementIsUsed)
@@ -476,7 +476,7 @@
 
     # Return if pou given by name is directly or undirectly used by the reference pou
     def ElementIsUsedBy(self, name, reference):
-        if name in self.ElementUsingTree:
+        if self.ElementUsingTree.has_key(name):
             list = self.ElementUsingTree[name]
             # Test if pou is directly used by reference
             if reference in list:
@@ -492,9 +492,9 @@
     setattr(cls, "ElementIsUsedBy", ElementIsUsedBy)
 
     def GetDataTypeRange(self, type):
-        if type in self.CustomDataTypeRange:
+        if self.CustomDataTypeRange.has_key(type):
             return self.CustomDataTypeRange[type]
-        elif type in DataTypeRange:
+        elif DataTypeRange.has_key(type):
             return DataTypeRange[type]
         else:
             parent_type = self.GetParentType(type)
@@ -509,8 +509,8 @@
             for values in self.EnumeratedDataTypeValues.values():
                 all_values.extend(values)
             return all_values
-        elif type in self.EnumeratedDataTypeValues:
-            return self.EnumeratedDataTypeValues(type)
+        elif self.EnumeratedDataTypeValues.has_key(type):
+            return self.EnumeratedDataTypeValues[type]
         return []
     setattr(cls, "GetEnumeratedDataTypeValues", GetEnumeratedDataTypeValues)
 
@@ -1536,9 +1536,9 @@
             else:
                 action.addinline()
                 action.setinlineContent(params["value"])
-            if "duration" in params:
+            if params.has_key("duration"):
                 action.setduration(params["duration"])
-            if "indicator" in params:
+            if params.has_key("indicator"):
                 action.setindicator(params["indicator"])
             self.action.append(action)
     setattr(cls, "setactions", setactions)