def parse_pattern_fill(self, node): fill = dict(node.attrib) for child in safe_iterator(node): if child is not node: tag = localname(child) fill[tag] = Color(**dict(child.attrib)) return PatternFill(**fill)
def __set__(self, instance, value): if hasattr(value, "tag"): tag = localname(value) if tag != self.name: raise ValueError("Tag does not match attribute") value = self.from_tree(value) super(Nested, self).__set__(instance, value)
def read_properties(xml_source): properties = DocumentProperties() root = fromstring(xml_source) for node in safe_iterator(root): tag = localname(node) setattr(properties, tag, node.text) return properties
def from_tree(cls, node): """ Create object from XML """ # strip known namespaces from attributes attrib = dict(node.attrib) for key, ns in cls.__namespaced__: if ns in attrib: attrib[key] = attrib[ns] del attrib[ns] # strip attributes with unknown namespaces for key in list(attrib): if key.startswith('{'): del attrib[key] elif key in KEYWORDS: attrib["_" + key] = attrib[key] del attrib[key] elif "-" in key: n = key.replace("-", "_") attrib[n] = attrib[key] del attrib[key] if node.text and "attr_text" in cls.__attrs__: attrib["attr_text"] = node.text for el in node: tag = localname(el) if tag in KEYWORDS: tag = "_" + tag desc = getattr(cls, tag, None) if desc is None or isinstance(desc, property): continue if hasattr(desc, 'from_tree'): #descriptor manages conversion obj = desc.from_tree(el) else: if hasattr(desc.expected_type, "from_tree"): #complex type obj = desc.expected_type.from_tree(el) else: #primitive obj = el.text if isinstance(desc, NestedSequence): attrib[tag] = obj elif isinstance(desc, Sequence): attrib.setdefault(tag, []) attrib[tag].append(obj) elif isinstance(desc, MultiSequencePart): attrib.setdefault(desc.store, []) attrib[desc.store].append(obj) else: attrib[tag] = obj return cls(**attrib)
def from_tree(cls, node): """ Create object from XML """ # strip known namespaces from attributes attrib = dict(node.attrib) for key, ns in cls.__namespaced__: if ns in attrib: attrib[key] = attrib[ns] del attrib[ns] # strip attributes with unknown namespaces for key in list(attrib): if key.startswith('{'): del attrib[key] elif key in KEYWORDS: attrib["_" + key] = attrib[key] del attrib[key] if node.text and "attr_text" in cls.__attrs__: attrib["attr_text"] = node.text for el in node: tag = localname(el) if tag in KEYWORDS: tag = "_" + tag desc = getattr(cls, tag, None) if desc is None or isinstance(desc, property): continue if hasattr(desc, 'from_tree'): #descriptor manages conversion obj = desc.from_tree(el) else: if hasattr(desc.expected_type, "from_tree"): #complex type obj = desc.expected_type.from_tree(el) else: #primitive obj = el.text if isinstance(desc, NestedSequence): attrib[tag] = obj elif isinstance(desc, Sequence): attrib.setdefault(tag, []) attrib[tag].append(obj) elif isinstance(desc, MultiSequencePart): attrib.setdefault(desc.store, []) attrib[desc.store].append(obj) else: attrib[tag] = obj return cls(**attrib)
def parse_font(self, font_node): """Read individual font""" font = {} for child in safe_iterator(font_node): if child is not font_node: tag = localname(child) font[tag] = child.get("val", True) underline = font_node.find('{%s}u' % SHEET_MAIN_NS) if underline is not None: font['u'] = underline.get('val', 'single') color = font_node.find('{%s}color' % SHEET_MAIN_NS) if color is not None: font['color'] = Color(**dict(color.attrib)) return Font(**font)
def from_tree(cls, node): """ Create object from XML """ attrib = dict(node.attrib) for key, ns in cls.__namespaced__: if ns in attrib: attrib[key] = attrib[ns] del attrib[ns] for el in node: tag = localname(el) if tag in KEYWORDS: tag = "_" + tag desc = getattr(cls, tag, None) if desc is None: continue if tag in cls.__nested__: if hasattr(desc, 'from_tree'): if isinstance(desc, Sequence): attrib.setdefault(tag, []) attrib[tag].append(desc.from_tree(el)) else: attrib[tag] = desc.from_tree(el) else: if isinstance(desc, property): continue elif hasattr(desc.expected_type, "from_tree"): obj = desc.expected_type.from_tree(el) else: obj = el.text if isinstance(desc, Sequence): if tag not in attrib: attrib[tag] = [] attrib[tag].append(obj) else: attrib[tag] = obj return cls(**attrib)
def from_tree(cls, node): """ Create object from XML """ attrib = dict(node.attrib) for el in node: tag = localname(el) desc = getattr(cls, tag, None) if desc is None: continue if tag in cls.__nested__: attrib[tag] = cls._create_nested(el, tag) else: if hasattr(desc.expected_type, "from_tree"): obj = desc.expected_type.from_tree(el) else: obj = el.text if isinstance(desc, Sequence): if tag not in attrib: attrib[tag] = [] attrib[tag].append(obj) else: attrib[tag] = obj return cls(**attrib)
def _from_tree(cls, el): attrib = dict(el.attrib) for child in el: desc = localname(child) attrib[desc] = Color.from_tree(child) return cls(**attrib)