xmlclass/xmlclass.py
branchwxPython4
changeset 3589 a0b645a934c9
parent 3288 8f3cb4b47cbb
child 3750 f62625418bff
equal deleted inserted replaced
3588:412090a6b3a7 3589:a0b645a934c9
   596             ind1, _ind2 = getIndent(indent, name)
   596             ind1, _ind2 = getIndent(indent, name)
   597             return ind1 + "<%s/>\n" % name
   597             return ind1 + "<%s/>\n" % name
   598         else:
   598         else:
   599             return ""
   599             return ""
   600 
   600 
       
   601     def Initial():
       
   602         p = etree.Element(infos["name"])
       
   603         return p
       
   604 
   601     return {
   605     return {
   602         "type": TAG,
   606         "type": TAG,
   603         "extract": ExtractTag,
   607         "extract": ExtractTag,
   604         "generate": GenerateTag,
   608         "generate": GenerateTag,
   605         "initial": lambda: None,
   609         "initial": Initial,
   606         "check": lambda x: x is None or infos["minOccurs"] == 0 and x
   610         "check": lambda x: x is None or infos["minOccurs"] == 0 and x
   607     }
   611     }
   608 
   612 
   609 
   613 
   610 def FindTypeInfos(factory, infos):
   614 def FindTypeInfos(factory, infos):
  1448         children = []
  1452         children = []
  1449         if path is not None:
  1453         if path is not None:
  1450             parts = path.split(".", 1)
  1454             parts = path.split(".", 1)
  1451             if parts[0] in attributes:
  1455             if parts[0] in attributes:
  1452                 if len(parts) != 1:
  1456                 if len(parts) != 1:
  1453                     raise ValueError("Wrong path!")
  1457                     raise ValueError("Wrong path: "+path)
  1454                 attr_type = gettypeinfos(attributes[parts[0]]["attr_type"]["basename"],
  1458                 attr_type = gettypeinfos(attributes[parts[0]]["attr_type"]["basename"],
  1455                                          attributes[parts[0]]["attr_type"]["facets"])
  1459                                          attributes[parts[0]]["attr_type"]["facets"])
  1456                 value = getattr(self, parts[0], "")
  1460                 value = getattr(self, parts[0], "")
  1457             elif parts[0] in elements:
  1461             elif parts[0] in elements:
  1458                 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
  1462                 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
  1459                     if len(parts) != 1:
  1463                     if len(parts) != 1:
  1460                         raise ValueError("Wrong path!")
  1464                         raise ValueError("Wrong path: "+path)
  1461                     attr_type = gettypeinfos(elements[parts[0]]["elmt_type"]["basename"],
  1465                     attr_type = gettypeinfos(elements[parts[0]]["elmt_type"]["basename"],
  1462                                              elements[parts[0]]["elmt_type"]["facets"])
  1466                                              elements[parts[0]]["elmt_type"]["facets"])
  1463                     value = getattr(self, parts[0], "")
  1467                     value = getattr(self, parts[0], "")
  1464                 elif parts[0] == "content":
  1468                 elif parts[0] == "content":
  1465                     return self.content.getElementInfos(self.content.getLocalTag(), path)
  1469                     return self.content.getElementInfos(self.content.getLocalTag(), path)
  1466                 else:
  1470                 else:
  1467                     attr = getattr(self, parts[0], None)
  1471                     attr = getattr(self, parts[0], None)
  1468                     if attr is None:
  1472                     if attr is None:
  1469                         raise ValueError("Wrong path!")
  1473                         raise ValueError("Wrong path: "+path)
  1470                     if len(parts) == 1:
  1474                     if len(parts) == 1:
  1471                         return attr.getElementInfos(parts[0])
  1475                         return attr.getElementInfos(parts[0])
  1472                     else:
  1476                     else:
  1473                         return attr.getElementInfos(parts[0], parts[1])
  1477                         return attr.getElementInfos(parts[0], parts[1])
  1474             elif "content" in elements:
  1478             elif "content" in elements:
  1475                 if len(parts) > 0:
  1479                 if len(parts) > 0:
  1476                     return self.content.getElementInfos(name, path)
  1480                     return self.content.getElementInfos(name, path)
  1477             elif "base" in classinfos:
  1481             elif "base" in classinfos:
  1478                 classinfos["base"].getElementInfos(name, path)
  1482                 classinfos["base"].getElementInfos(name, path)
  1479             else:
  1483             else:
  1480                 raise ValueError("Wrong path!")
  1484                 raise ValueError("Wrong path: "+path)
  1481         else:
  1485         else:
  1482             if not derived:
  1486             if not derived:
  1483                 children.extend(self.getElementAttributes())
  1487                 children.extend(self.getElementAttributes())
  1484             if "base" in classinfos:
  1488             if "base" in classinfos:
  1485                 children.extend(classinfos["base"].getElementInfos(self, name, derived=True)["children"])
  1489                 children.extend(classinfos["base"].getElementInfos(self, name, derived=True)["children"])
  1517     def setElementValue(self, path, value):
  1521     def setElementValue(self, path, value):
  1518         if path is not None:
  1522         if path is not None:
  1519             parts = path.split(".", 1)
  1523             parts = path.split(".", 1)
  1520             if parts[0] in attributes:
  1524             if parts[0] in attributes:
  1521                 if len(parts) != 1:
  1525                 if len(parts) != 1:
  1522                     raise ValueError("Wrong path!")
  1526                     raise ValueError("Wrong path: "+path)
  1523                 if attributes[parts[0]]["attr_type"]["basename"] == "boolean":
  1527                 if attributes[parts[0]]["attr_type"]["basename"] == "boolean":
  1524                     setattr(self, parts[0], value)
  1528                     setattr(self, parts[0], value)
  1525                 elif attributes[parts[0]]["use"] == "optional" and value == None:
  1529                 elif attributes[parts[0]]["use"] == "optional" and value == None:
  1526                     if "default" in attributes[parts[0]]:
  1530                     if "default" in attributes[parts[0]]:
  1527                         setattr(self, parts[0],
  1531                         setattr(self, parts[0],
  1532                 else:
  1536                 else:
  1533                     setattr(self, parts[0], attributes[parts[0]]["attr_type"]["extract"](value, False))
  1537                     setattr(self, parts[0], attributes[parts[0]]["attr_type"]["extract"](value, False))
  1534             elif parts[0] in elements:
  1538             elif parts[0] in elements:
  1535                 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
  1539                 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
  1536                     if len(parts) != 1:
  1540                     if len(parts) != 1:
  1537                         raise ValueError("Wrong path!")
  1541                         raise ValueError("Wrong path: "+path)
  1538                     if elements[parts[0]]["elmt_type"]["basename"] == "boolean":
  1542                     if elements[parts[0]]["elmt_type"]["basename"] == "boolean":
  1539                         setattr(self, parts[0], value)
  1543                         setattr(self, parts[0], value)
  1540                     elif attributes[parts[0]]["minOccurs"] == 0 and value == "":
  1544                     elif attributes[parts[0]]["minOccurs"] == 0 and value == "":
  1541                         setattr(self, parts[0], None)
  1545                         setattr(self, parts[0], None)
  1542                     else:
  1546                     else:
  1736         return etree.QName(self.tag).localname
  1740         return etree.QName(self.tag).localname
  1737 
  1741 
  1738     def tostring(self):
  1742     def tostring(self):
  1739         return NAMESPACE_PATTERN.sub("", etree.tostring(self, pretty_print=True, encoding='utf-8')).decode('utf-8')
  1743         return NAMESPACE_PATTERN.sub("", etree.tostring(self, pretty_print=True, encoding='utf-8')).decode('utf-8')
  1740 
  1744 
       
  1745     def getElementInfos(self, name, path=None, derived=False):
       
  1746         return {"name": name, "type": TAG, "value": None, "use": None, "children": []}
  1741 
  1747 
  1742 class XMLElementClassLookUp(etree.PythonElementClassLookup):
  1748 class XMLElementClassLookUp(etree.PythonElementClassLookup):
  1743 
  1749 
  1744     def __init__(self, classes, *args, **kwargs):
  1750     def __init__(self, classes, *args, **kwargs):
  1745         etree.PythonElementClassLookup.__init__(self, *args, **kwargs)
  1751         etree.PythonElementClassLookup.__init__(self, *args, **kwargs)