plcopen/plcopen.py
changeset 1749 d73b64672238
parent 1744 69dfdb26f600
child 1750 acf02488f37f
equal deleted inserted replaced
1748:ba5f64fe0e00 1749:d73b64672238
   149     return test_result
   149     return test_result
   150 
   150 
   151 
   151 
   152 def TextMatched(str1, str2):
   152 def TextMatched(str1, str2):
   153     return str1 and str2 and (str1.upper() == str2.upper())
   153     return str1 and str2 and (str1.upper() == str2.upper())
       
   154 
   154 
   155 
   155 PLCOpenParser = GenerateParserFromXSD(paths.AbsNeighbourFile(__file__, "tc6_xml_v201.xsd"))
   156 PLCOpenParser = GenerateParserFromXSD(paths.AbsNeighbourFile(__file__, "tc6_xml_v201.xsd"))
   156 PLCOpen_XPath = lambda xpath: etree.XPath(xpath, namespaces=PLCOpenParser.NSMAP)
   157 PLCOpen_XPath = lambda xpath: etree.XPath(xpath, namespaces=PLCOpenParser.NSMAP)
   157 
   158 
   158 LOAD_POU_PROJECT_TEMPLATE = """
   159 LOAD_POU_PROJECT_TEMPLATE = """
   185 <pou name="paste_pou" pouType="program">
   186 <pou name="paste_pou" pouType="program">
   186   <body>
   187   <body>
   187     <%(body_type)s>%%s</%(body_type)s>
   188     <%(body_type)s>%%s</%(body_type)s>
   188   </body>
   189   </body>
   189 </pou>""" % locals()
   190 </pou>""" % locals()
       
   191 
   190 
   192 
   191 PLCOpen_v1_file = open(paths.AbsNeighbourFile(__file__, "TC6_XML_V10_B.xsd"))
   193 PLCOpen_v1_file = open(paths.AbsNeighbourFile(__file__, "TC6_XML_V10_B.xsd"))
   192 PLCOpen_v1_xml = PLCOpen_v1_file.read()
   194 PLCOpen_v1_xml = PLCOpen_v1_file.read()
   193 PLCOpen_v1_file.close()
   195 PLCOpen_v1_file.close()
   194 PLCOpen_v1_xml = PLCOpen_v1_xml.replace(
   196 PLCOpen_v1_xml = PLCOpen_v1_xml.replace(
   287     project_file = open(filepath)
   289     project_file = open(filepath)
   288     project_xml = project_file.read()
   290     project_xml = project_file.read()
   289     project_file.close()
   291     project_file.close()
   290     return LoadProjectXML(project_xml)
   292     return LoadProjectXML(project_xml)
   291 
   293 
       
   294 
   292 project_pou_xpath = PLCOpen_XPath("/ppx:project/ppx:types/ppx:pous/ppx:pou")
   295 project_pou_xpath = PLCOpen_XPath("/ppx:project/ppx:types/ppx:pous/ppx:pou")
   293 
   296 
   294 
   297 
   295 def LoadPou(xml_string):
   298 def LoadPou(xml_string):
   296     root, error = LoadProjectXML(LOAD_POU_PROJECT_TEMPLATE % xml_string)
   299     root, error = LoadProjectXML(LOAD_POU_PROJECT_TEMPLATE % xml_string)
   297     return project_pou_xpath(root)[0], error
   300     return project_pou_xpath(root)[0], error
       
   301 
   298 
   302 
   299 project_pou_instances_xpath = {
   303 project_pou_instances_xpath = {
   300     body_type: PLCOpen_XPath(
   304     body_type: PLCOpen_XPath(
   301         "/ppx:project/ppx:types/ppx:pous/ppx:pou[@name='paste_pou']/ppx:body/ppx:%s/*" % body_type)
   305         "/ppx:project/ppx:types/ppx:pous/ppx:pou[@name='paste_pou']/ppx:body/ppx:%s/*" % body_type)
   302     for body_type in ["FBD", "LD", "SFC"]}
   306     for body_type in ["FBD", "LD", "SFC"]}
   314         project,
   318         project,
   315         pretty_print=True,
   319         pretty_print=True,
   316         xml_declaration=True,
   320         xml_declaration=True,
   317         encoding='utf-8'))
   321         encoding='utf-8'))
   318     project_file.close()
   322     project_file.close()
       
   323 
   319 
   324 
   320 cls = PLCOpenParser.GetElementClass("formattedText")
   325 cls = PLCOpenParser.GetElementClass("formattedText")
   321 if cls:
   326 if cls:
   322     def updateElementName(self, old_name, new_name):
   327     def updateElementName(self, old_name, new_name):
   323         text = self.getanyText()
   328         text = self.getanyText()
   709                 break
   714                 break
   710         for variable in variables:
   715         for variable in variables:
   711             search_result.extend(variable.Search(criteria, parent_infos + [variable_type, var_number]))
   716             search_result.extend(variable.Search(criteria, parent_infos + [variable_type, var_number]))
   712             var_number += 1
   717             var_number += 1
   713     return search_result
   718     return search_result
       
   719 
   714 
   720 
   715 cls = PLCOpenParser.GetElementClass("configuration", "configurations")
   721 cls = PLCOpenParser.GetElementClass("configuration", "configurations")
   716 if cls:
   722 if cls:
   717 
   723 
   718     def addglobalVar(self, var_type, name, location="", description=""):
   724     def addglobalVar(self, var_type, name, location="", description=""):
   966 
   972 
   967 
   973 
   968 def _updateBaseTypeElementName(self, old_name, new_name):
   974 def _updateBaseTypeElementName(self, old_name, new_name):
   969     self.baseType.updateElementName(old_name, new_name)
   975     self.baseType.updateElementName(old_name, new_name)
   970 
   976 
       
   977 
   971 cls = PLCOpenParser.GetElementClass("dataType", "dataTypes")
   978 cls = PLCOpenParser.GetElementClass("dataType", "dataTypes")
   972 if cls:
   979 if cls:
   973     setattr(cls, "updateElementName", _updateBaseTypeElementName)
   980     setattr(cls, "updateElementName", _updateBaseTypeElementName)
   974 
   981 
   975     def Search(self, criteria, parent_infos=[]):
   982     def Search(self, criteria, parent_infos=[]):
  1041     search_result.extend(_Search([("lower", self.range.getlower()),
  1048     search_result.extend(_Search([("lower", self.range.getlower()),
  1042                                   ("upper", self.range.getupper())],
  1049                                   ("upper", self.range.getupper())],
  1043                                  criteria, parent_infos))
  1050                                  criteria, parent_infos))
  1044     return search_result
  1051     return search_result
  1045 
  1052 
       
  1053 
  1046 cls = PLCOpenParser.GetElementClass("subrangeSigned", "dataType")
  1054 cls = PLCOpenParser.GetElementClass("subrangeSigned", "dataType")
  1047 if cls:
  1055 if cls:
  1048     setattr(cls, "updateElementName", _updateBaseTypeElementName)
  1056     setattr(cls, "updateElementName", _updateBaseTypeElementName)
  1049     setattr(cls, "Search", _SearchInSubrange)
  1057     setattr(cls, "Search", _SearchInSubrange)
  1050 
  1058 
  1074     type_content = variable_type.getcontent()
  1082     type_content = variable_type.getcontent()
  1075     type_content_type = type_content.getLocalTag()
  1083     type_content_type = type_content.getLocalTag()
  1076     if type_content_type == "derived":
  1084     if type_content_type == "derived":
  1077         return type_content.getname()
  1085         return type_content.getname()
  1078     return type_content_type.upper()
  1086     return type_content_type.upper()
       
  1087 
  1079 
  1088 
  1080 cls = PLCOpenParser.GetElementClass("pou", "pous")
  1089 cls = PLCOpenParser.GetElementClass("pou", "pous")
  1081 if cls:
  1090 if cls:
  1082 
  1091 
  1083     block_inputs_xpath = PLCOpen_XPath(
  1092     block_inputs_xpath = PLCOpen_XPath(
  1924 
  1933 
  1925 
  1934 
  1926 def _SearchInElement(self, criteria, parent_infos=[]):
  1935 def _SearchInElement(self, criteria, parent_infos=[]):
  1927     return []
  1936     return []
  1928 
  1937 
       
  1938 
  1929 _connectionsFunctions = {
  1939 _connectionsFunctions = {
  1930     "bbox": {"none": _getBoundingBox,
  1940     "bbox": {"none": _getBoundingBox,
  1931              "single": _getBoundingBoxSingle,
  1941              "single": _getBoundingBoxSingle,
  1932              "multiple": _getBoundingBoxMultiple},
  1942              "multiple": _getBoundingBoxMultiple},
  1933     "translate": {"none": _translate,
  1943     "translate": {"none": _translate,
  1956         setattr(cls, "filterConnections", _connectionsFunctions["filter"][connectionPointInType])
  1966         setattr(cls, "filterConnections", _connectionsFunctions["filter"][connectionPointInType])
  1957         setattr(cls, "updateConnectionsId", _connectionsFunctions["update"][connectionPointInType])
  1967         setattr(cls, "updateConnectionsId", _connectionsFunctions["update"][connectionPointInType])
  1958         setattr(cls, "Search", _SearchInElement)
  1968         setattr(cls, "Search", _SearchInElement)
  1959     return cls
  1969     return cls
  1960 
  1970 
       
  1971 
  1961 cls = _initElementClass("comment", "commonObjects")
  1972 cls = _initElementClass("comment", "commonObjects")
  1962 if cls:
  1973 if cls:
  1963     def setcontentText(self, text):
  1974     def setcontentText(self, text):
  1964         self.content.setanyText(text)
  1975         self.content.setanyText(text)
  1965     setattr(cls, "setcontentText", setcontentText)
  1976     setattr(cls, "setcontentText", setcontentText)
  2041 
  2052 
  2042 def _getSearchInLDElement(ld_element_type):
  2053 def _getSearchInLDElement(ld_element_type):
  2043     def SearchInLDElement(self, criteria, parent_infos=[]):
  2054     def SearchInLDElement(self, criteria, parent_infos=[]):
  2044         return _Search([("reference", self.variable)], criteria, parent_infos + [ld_element_type, self.getlocalId()])
  2055         return _Search([("reference", self.variable)], criteria, parent_infos + [ld_element_type, self.getlocalId()])
  2045     return SearchInLDElement
  2056     return SearchInLDElement
       
  2057 
  2046 
  2058 
  2047 cls = _initElementClass("contact", "ldObjects", "single")
  2059 cls = _initElementClass("contact", "ldObjects", "single")
  2048 if cls:
  2060 if cls:
  2049     setattr(cls, "updateElementName", _UpdateLDElementName)
  2061     setattr(cls, "updateElementName", _UpdateLDElementName)
  2050     setattr(cls, "updateElementAddress", _UpdateLDElementAddress)
  2062     setattr(cls, "updateElementAddress", _UpdateLDElementAddress)
  2308 
  2320 
  2309 
  2321 
  2310 def _UpdateIOElementAddress(self, address_model, new_leading):
  2322 def _UpdateIOElementAddress(self, address_model, new_leading):
  2311     self.expression = update_address(self.expression, address_model, new_leading)
  2323     self.expression = update_address(self.expression, address_model, new_leading)
  2312 
  2324 
       
  2325 
  2313 cls = _initElementClass("inVariable", "fbdObjects")
  2326 cls = _initElementClass("inVariable", "fbdObjects")
  2314 if cls:
  2327 if cls:
  2315     setattr(cls, "updateElementName", _UpdateIOElementName)
  2328     setattr(cls, "updateElementName", _UpdateIOElementName)
  2316     setattr(cls, "updateElementAddress", _UpdateIOElementAddress)
  2329     setattr(cls, "updateElementAddress", _UpdateIOElementAddress)
  2317     setattr(cls, "Search", _SearchInIOVariable)
  2330     setattr(cls, "Search", _SearchInIOVariable)
  2329     setattr(cls, "Search", _SearchInIOVariable)
  2342     setattr(cls, "Search", _SearchInIOVariable)
  2330 
  2343 
  2331 
  2344 
  2332 def _SearchInConnector(self, criteria, parent_infos=[]):
  2345 def _SearchInConnector(self, criteria, parent_infos=[]):
  2333     return _Search([("name", self.getname())], criteria, parent_infos + ["connector", self.getlocalId()])
  2346     return _Search([("name", self.getname())], criteria, parent_infos + ["connector", self.getlocalId()])
       
  2347 
  2334 
  2348 
  2335 cls = _initElementClass("continuation", "commonObjects")
  2349 cls = _initElementClass("continuation", "commonObjects")
  2336 if cls:
  2350 if cls:
  2337     setattr(cls, "Search", _SearchInConnector)
  2351     setattr(cls, "Search", _SearchInConnector)
  2338 
  2352 
  2492         elif opened == closed:
  2506         elif opened == closed:
  2493             i += 1
  2507             i += 1
  2494         else:
  2508         else:
  2495             raise ValueError, _("\"%s\" is an invalid value!") % value
  2509             raise ValueError, _("\"%s\" is an invalid value!") % value
  2496     return items
  2510     return items
       
  2511 
  2497 
  2512 
  2498 cls = PLCOpenParser.GetElementClass("arrayValue", "value")
  2513 cls = PLCOpenParser.GetElementClass("arrayValue", "value")
  2499 if cls:
  2514 if cls:
  2500     arrayValue_model = re.compile("([0-9]+)\((.*)\)$")
  2515     arrayValue_model = re.compile("([0-9]+)\((.*)\)$")
  2501 
  2516