This file is indexed.

/usr/lib/python3/dist-packages/pypeg2/xmlast.py is in python3-pypeg2 2.15.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
XML AST generator

pyPEG parsing framework

Copyleft 2012, Volker Birk.
This program is under GNU General Public License 2.0.
"""


from __future__ import unicode_literals
try:
    str = unicode
except NameError:
    pass


__version__ = 2.15
__author__ = "Volker Birk"
__license__ = "This program is under GNU General Public License 2.0."
__url__ = "http://fdik.org/pyPEG"


try:
    import lxml
    from lxml import etree
except ImportError:
    import xml.etree.ElementTree as etree

if __debug__:
    import warnings
import pypeg2


def create_tree(thing, parent=None, object_names=False):
    """Create an XML etree from a thing.

    Arguments:
        thing           thing to interpret
        parent          etree.Element to put subtree into
                        default: create a new Element tree
        object_names    experimental feature: if True tag names are object
                        names instead of types

    Returns:
        etree.Element instance created
    """

    try:
        grammar = type(thing).grammar
    except AttributeError:
        if isinstance(thing, list):
            grammar = pypeg2.csl(pypeg2.name())
        else:
            grammar = pypeg2.word

    name = type(thing).__name__

    if object_names:
        try:
            name = str(thing.name)
            name = name.replace(" ", "_")
        except AttributeError:
            pass

    if parent is None:
        me = etree.Element(name)
    else:
        me = etree.SubElement(parent, name)

    for e in pypeg2.attributes(grammar):
        if object_names and e.name == "name":
            if name != type(thing).__name__:
                continue
        key, value = e.name, getattr(thing, e.name, None)
        if value is not None:
            if pypeg2._issubclass(e.thing, (str, int, pypeg2.Literal)) \
                    or type(e.thing) == pypeg2._RegEx:
                me.set(key, str(value))
            else:
                create_tree(value, me, object_names)

    if isinstance(thing, list):
        things = thing
    elif isinstance(thing, pypeg2.Namespace):
        things = thing.values()
    else:
        things = []

    last = None
    for t in things:
        if type(t) == str:
            if last is not None:
                last.tail = str(t)
            else:
                me.text = str(t)
        else:
            last = create_tree(t, me, object_names)

    if isinstance(thing, str):
        me.text = str(thing)

    return me


def thing2xml(thing, pretty=False, object_names=False):
    """Create XML text from a thing.

    Arguments:
        thing           thing to interpret
        pretty          True if XML should be indented
                        False if XML should be plain
        object_names    experimental feature: if True tag names are object
                        names instead of types

    Returns:
        bytes with encoded XML 
    """

    tree = create_tree(thing, None, object_names)
    try:
        if lxml:
            return etree.tostring(tree, pretty_print=pretty)
    except NameError:
        if __debug__:
            if pretty:
                warnings.warn("lxml is needed for pretty printing",
                        ImportWarning)
        return etree.tostring(tree)


def create_thing(element, symbol_table):
    """Create thing from an XML element.

    Arguments:
        element         etree.Element instance to read
        symbol_table    symbol table where the classes can be found

    Returns:
        thing created
    """

    C = symbol_table[element.tag]
    if element.text:
        thing = C(element.text)
    else:
        thing = C()
    
    subs = iter(element)
    iterated_already = False

    try:
        grammar = C.grammar
    except AttributeError:
        pass
    else:
        for e in pypeg2.attributes(grammar):
            key = e.name
            if pypeg2._issubclass(e.thing, (str, int, pypeg2.Literal)) \
                    or type(e.thing) == pypeg2._RegEx:
                try:
                    value = element.attrib[e.name]
                except KeyError:
                    pass
                else:
                    setattr(thing, key, e.thing(value))
            else:
                try:
                    if not iterated_already:
                        iterated_already = True
                        sub = next(subs)
                except StopIteration:
                    pass
                if sub.tag == e.thing.__name__:
                    iterated_already = False
                    t = create_thing(sub, symbol_table)
                    setattr(thing, key, t)

    if issubclass(C, list) or issubclass(C, pypeg2.Namespace):
        try:
            while True:
                if iterated_already:
                    iterated_alread = False
                else:
                    sub = next(subs)
                t = create_thing(sub, symbol_table)
                if isinstance(thing, pypeg2.List):
                    thing.append(t)
                else:
                    thing[t.name] = t
        except StopIteration:
            pass
    
    return thing


def xml2thing(xml, symbol_table):
    """Create thing from XML text.

    Arguments:
        xml             bytes with encoded XML
        symbol_table    symbol table where the classes can be found

    Returns:
        created thing
    """

    element = etree.fromstring(xml)
    return create_thing(element, symbol_table)