|
14 | 14 |
|
15 | 15 | from lxml import etree, objectify |
16 | 16 |
|
| 17 | +from opc.constants import NAMESPACE as NS, RELATIONSHIP_TARGET_MODE as RTM |
| 18 | + |
17 | 19 |
|
18 | 20 | # configure objectified XML parser |
19 | 21 | fallback_lookup = objectify.ObjectifyElementClassLookup() |
20 | 22 | element_class_lookup = etree.ElementNamespaceClassLookup(fallback_lookup) |
21 | 23 | oxml_parser = etree.XMLParser(remove_blank_text=True) |
22 | 24 | oxml_parser.set_element_class_lookup(element_class_lookup) |
23 | 25 |
|
| 26 | +nsmap = { |
| 27 | + 'ct': NS.OPC_CONTENT_TYPES, |
| 28 | + 'pr': NS.OPC_RELATIONSHIPS, |
| 29 | +} |
| 30 | + |
24 | 31 |
|
25 | 32 | # =========================================================================== |
26 | 33 | # functions |
|
29 | 36 | def oxml_fromstring(text): |
30 | 37 | """``etree.fromstring()`` replacement that uses oxml parser""" |
31 | 38 | return objectify.fromstring(text, oxml_parser) |
| 39 | + |
| 40 | + |
| 41 | +def oxml_tostring(elm, encoding=None, pretty_print=False, standalone=None): |
| 42 | + # if xsi parameter is not set to False, PowerPoint won't load without a |
| 43 | + # repair step; deannotate removes some original xsi:type tags in core.xml |
| 44 | + # if this parameter is left out (or set to True) |
| 45 | + objectify.deannotate(elm, xsi=False, cleanup_namespaces=True) |
| 46 | + return etree.tostring(elm, encoding=encoding, pretty_print=pretty_print, |
| 47 | + standalone=standalone) |
| 48 | + |
| 49 | + |
| 50 | +# =========================================================================== |
| 51 | +# Custom element classes |
| 52 | +# =========================================================================== |
| 53 | + |
| 54 | +class OxmlBaseElement(objectify.ObjectifiedElement): |
| 55 | + """ |
| 56 | + Base class for all custom element classes, to add standardized behavior |
| 57 | + to all classes in one place. |
| 58 | + """ |
| 59 | + @property |
| 60 | + def xml(self): |
| 61 | + """ |
| 62 | + Return XML string for this element, suitable for testing purposes. |
| 63 | + Pretty printed for readability and without an XML declaration at the |
| 64 | + top. |
| 65 | + """ |
| 66 | + return oxml_tostring(self, encoding='unicode', pretty_print=True) |
| 67 | + |
| 68 | + |
| 69 | +class CT_Relationship(OxmlBaseElement): |
| 70 | + """ |
| 71 | + ``<Relationship>`` element, representing a single relationship from a |
| 72 | + source to a target part. |
| 73 | + """ |
| 74 | + @property |
| 75 | + def rId(self): |
| 76 | + """ |
| 77 | + String held in the ``Id`` attribute of this ``<Relationship>`` |
| 78 | + element. |
| 79 | + """ |
| 80 | + return self.get('Id') |
| 81 | + |
| 82 | + @property |
| 83 | + def reltype(self): |
| 84 | + """ |
| 85 | + String held in the ``Type`` attribute of this ``<Relationship>`` |
| 86 | + element. |
| 87 | + """ |
| 88 | + return self.get('Type') |
| 89 | + |
| 90 | + @property |
| 91 | + def target_ref(self): |
| 92 | + """ |
| 93 | + String held in the ``Target`` attribute of this ``<Relationship>`` |
| 94 | + element. |
| 95 | + """ |
| 96 | + return self.get('Target') |
| 97 | + |
| 98 | + @property |
| 99 | + def target_mode(self): |
| 100 | + """ |
| 101 | + String held in the ``TargetMode`` attribute of this |
| 102 | + ``<Relationship>`` element, either ``Internal`` or ``External``. |
| 103 | + Defaults to ``Internal``. |
| 104 | + """ |
| 105 | + return self.get('TargetMode', RTM.INTERNAL) |
| 106 | + |
| 107 | + |
| 108 | +pr_namespace = element_class_lookup.get_namespace(nsmap['pr']) |
| 109 | +pr_namespace['Relationship'] = CT_Relationship |
0 commit comments