|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +""" |
| 4 | +Test data builders for text XML elements |
| 5 | +""" |
| 6 | + |
| 7 | +from ...unitdata import BaseBuilder |
| 8 | + |
| 9 | + |
| 10 | +class CT_PBuilder(BaseBuilder): |
| 11 | + """ |
| 12 | + Test data builder for a CT_P (<w:p>) XML element that appears within the |
| 13 | + body element of a document.xml file. |
| 14 | + """ |
| 15 | + def __init__(self): |
| 16 | + """Establish instance variables with default values""" |
| 17 | + super(CT_PBuilder, self).__init__() |
| 18 | + self._pPr = None |
| 19 | + self._r = [] |
| 20 | + |
| 21 | + @property |
| 22 | + def is_empty(self): |
| 23 | + return self._pPr is None and len(self._r) == 0 |
| 24 | + |
| 25 | + def with_pPr(self, pPr=None): |
| 26 | + """Add a <w:pPr> child element""" |
| 27 | + if pPr is None: |
| 28 | + pPr = a_pPr() |
| 29 | + self._pPr = pPr |
| 30 | + return self |
| 31 | + |
| 32 | + def with_r(self, count=1): |
| 33 | + """Add *count* empty run elements""" |
| 34 | + for i in range(count): |
| 35 | + self._r.append(an_r()) |
| 36 | + return self |
| 37 | + |
| 38 | + @property |
| 39 | + def xml(self): |
| 40 | + """Return element XML based on attribute settings""" |
| 41 | + indent = ' ' * self._indent |
| 42 | + if self.is_empty: |
| 43 | + xml = '%s<w:p%s/>\n' % (indent, self._nsdecls) |
| 44 | + else: |
| 45 | + xml = '%s<w:p%s>\n' % (indent, self._nsdecls) |
| 46 | + if self._pPr: |
| 47 | + xml += self._pPr.with_indent(self._indent+2).xml |
| 48 | + for r in self._r: |
| 49 | + xml += r.with_indent(self._indent+2).xml |
| 50 | + xml += '%s</w:p>\n' % indent |
| 51 | + return xml |
| 52 | + |
| 53 | + |
| 54 | +class CT_PPrBuilder(BaseBuilder): |
| 55 | + """ |
| 56 | + Test data builder for a CT_PPr (<w:pPr>) XML element that appears as a |
| 57 | + child of a <w:p> element in a document.xml file. |
| 58 | + """ |
| 59 | + def __init__(self): |
| 60 | + """Establish instance variables with default values""" |
| 61 | + super(CT_PPrBuilder, self).__init__() |
| 62 | + self._pStyle = None |
| 63 | + |
| 64 | + @property |
| 65 | + def is_empty(self): |
| 66 | + return self._pStyle is None |
| 67 | + |
| 68 | + def with_style(self, style='foobar'): |
| 69 | + """Add pStyle child with inner text *style*""" |
| 70 | + self._pStyle = '<w:pStyle w:val="%s"/>' % style |
| 71 | + return self |
| 72 | + |
| 73 | + @property |
| 74 | + def xml(self): |
| 75 | + """Return element XML based on attribute settings""" |
| 76 | + indent = ' ' * self._indent |
| 77 | + if self.is_empty: |
| 78 | + xml = '%s<w:pPr%s/>\n' % (indent, self._nsdecls) |
| 79 | + else: |
| 80 | + xml = '%s<w:pPr%s>\n' % (indent, self._nsdecls) |
| 81 | + if self._pStyle: |
| 82 | + xml += '%s%s\n' % (indent+' ', self._pStyle) |
| 83 | + xml += '%s</w:pPr>\n' % indent |
| 84 | + return xml |
| 85 | + |
| 86 | + |
| 87 | +class CT_RBuilder(BaseBuilder): |
| 88 | + """ |
| 89 | + Test data builder for a CT_R (<w:r>) XML element that appears within the |
| 90 | + body element of a document.xml file. |
| 91 | + """ |
| 92 | + def __init__(self): |
| 93 | + """Establish instance variables with default values""" |
| 94 | + super(CT_RBuilder, self).__init__() |
| 95 | + self._t = [] |
| 96 | + |
| 97 | + @property |
| 98 | + def is_empty(self): |
| 99 | + return len(self._t) == 0 |
| 100 | + |
| 101 | + def with_t(self, text): |
| 102 | + """Add an text element containing *text*""" |
| 103 | + self._t.append(a_t(text)) |
| 104 | + return self |
| 105 | + |
| 106 | + @property |
| 107 | + def xml(self): |
| 108 | + """Return element XML based on attribute settings""" |
| 109 | + indent = ' ' * self._indent |
| 110 | + if self.is_empty: |
| 111 | + xml = '%s<w:r%s/>\n' % (indent, self._nsdecls) |
| 112 | + else: |
| 113 | + xml = '%s<w:r%s>\n' % (indent, self._nsdecls) |
| 114 | + for t_builder in self._t: |
| 115 | + xml += t_builder.with_indent(self._indent+2).xml |
| 116 | + xml += '%s</w:r>\n' % indent |
| 117 | + return xml |
| 118 | + |
| 119 | + |
| 120 | +class CT_TextBuilder(BaseBuilder): |
| 121 | + """ |
| 122 | + Test data builder for a CT_Text (<w:t>) XML element that appears within a |
| 123 | + run element. |
| 124 | + """ |
| 125 | + def __init__(self, text): |
| 126 | + """Establish instance variables with default values""" |
| 127 | + super(CT_TextBuilder, self).__init__() |
| 128 | + self._text = text |
| 129 | + |
| 130 | + @property |
| 131 | + def xml(self): |
| 132 | + """Return element XML based on attribute settings""" |
| 133 | + indent = ' ' * self._indent |
| 134 | + return '%s<w:t%s>%s</w:t>\n' % (indent, self._nsdecls, self._text) |
| 135 | + |
| 136 | + |
| 137 | +class CT_SectPrBuilder(BaseBuilder): |
| 138 | + """ |
| 139 | + Test data builder for a CT_SectPr (<w:sectPr>) XML element that appears |
| 140 | + within the body element of a document.xml file. |
| 141 | + """ |
| 142 | + def __init__(self): |
| 143 | + """Establish instance variables with default values""" |
| 144 | + super(CT_SectPrBuilder, self).__init__() |
| 145 | + |
| 146 | + @property |
| 147 | + def xml(self): |
| 148 | + """Return element XML based on attribute settings""" |
| 149 | + tmpl = '%s<w:sectPr/>\n' |
| 150 | + indent = ' ' * self._indent |
| 151 | + return tmpl % (indent) |
| 152 | + |
| 153 | + |
| 154 | +def a_p(): |
| 155 | + """Return a CT_PBuilder instance""" |
| 156 | + return CT_PBuilder() |
| 157 | + |
| 158 | + |
| 159 | +def a_pPr(): |
| 160 | + """Return a CT_PPrBuilder instance""" |
| 161 | + return CT_PPrBuilder() |
| 162 | + |
| 163 | + |
| 164 | +def a_t(text): |
| 165 | + """Return a CT_TextBuilder instance""" |
| 166 | + return CT_TextBuilder(text) |
| 167 | + |
| 168 | + |
| 169 | +def a_sectPr(): |
| 170 | + """Return a CT_SectPr instance""" |
| 171 | + return CT_SectPrBuilder() |
| 172 | + |
| 173 | + |
| 174 | +def an_r(): |
| 175 | + """Return a CT_RBuilder instance""" |
| 176 | + return CT_RBuilder() |
0 commit comments