This file is indexed.

/usr/lib/python3/dist-packages/suds/xsd/sxbase.py is in python3-suds 0.7~git20150727.94664dd-3.

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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# This program is free software; you can redistribute it and/or modify it under
# the terms of the (LGPL) GNU Lesser General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Library Lesser General Public License
# for more details at ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jeff Ortel ( jortel@redhat.com )

"""I{Base} classes representing XSD schema objects."""

from suds import *
from suds.xsd import *
from suds.sax.element import Element
from suds.sax import Namespace

from logging import getLogger
log = getLogger(__name__)


class SchemaObject(UnicodeMixin):
    """
    A schema object is an extension to object with schema awareness.

    @ivar root: The XML root element.
    @type root: L{Element}
    @ivar schema: The schema containing this object.
    @type schema: L{schema.Schema}
    @ivar form_qualified: A flag indicating that @elementFormDefault has a
        value of I{qualified}.
    @type form_qualified: boolean
    @ivar nillable: A flag indicating that @nillable has a value of I{true}.
    @type nillable: boolean
    @ivar default: The default value.
    @type default: object
    @ivar rawchildren: A list raw of all children.
    @type rawchildren: [L{SchemaObject},...]

    """

    @classmethod
    def prepend(cls, d, s, filter=Filter()):
        """
        Prepend B{s}ource XSD schema objects to the B{d}estination list.

        B{filter} is used to decide which objects to prepend and which to skip.

        @param d: The destination list.
        @type d: list
        @param s: The source list.
        @type s: list
        @param filter: A filter allowing items to be prepended.
        @type filter: L{Filter}

        """
        i = 0
        for x in s:
            if x in filter:
                d.insert(i, x)
                i += 1

    @classmethod
    def append(cls, d, s, filter=Filter()):
        """
        Append B{s}ource XSD schema objects to the B{d}estination list.

        B{filter} is used to decide which objects to append and which to skip.

        @param d: The destination list.
        @type d: list
        @param s: The source list.
        @type s: list
        @param filter: A filter that allows items to be appended.
        @type filter: L{Filter}

        """
        for item in s:
            if item in filter:
                d.append(item)

    def __init__(self, schema, root):
        """
        @param schema: The containing schema.
        @type schema: L{schema.Schema}
        @param root: The XML root node.
        @type root: L{Element}

        """
        self.schema = schema
        self.root = root
        self.id = objid(self)
        self.name = root.get("name")
        self.qname = (self.name, schema.tns[1])
        self.min = root.get("minOccurs")
        self.max = root.get("maxOccurs")
        self.type = root.get("type")
        self.ref = root.get("ref")
        self.form_qualified = schema.form_qualified
        self.nillable = False
        self.default = root.get("default")
        self.rawchildren = []

    def attributes(self, filter=Filter()):
        """
        Get only the attribute content.

        @param filter: A filter to constrain the result.
        @type filter: L{Filter}
        @return: A list of (attr, ancestry) tuples.
        @rtype: [(L{SchemaObject}, [L{SchemaObject},..]),..]

        """
        result = []
        for child, ancestry in self:
            if child.isattr() and child in filter:
                result.append((child, ancestry))
        return result

    def children(self, filter=Filter()):
        """
        Get only the I{direct} or non-attribute content.

        @param filter: A filter to constrain the result.
        @type filter: L{Filter}
        @return: A list tuples: (child, ancestry)
        @rtype: [(L{SchemaObject}, [L{SchemaObject},..]),..]

        """
        result = []
        for child, ancestry in self:
            if not child.isattr() and child in filter:
                result.append((child, ancestry))
        return result

    def get_attribute(self, name):
        """
        Get (find) an attribute by name.

        @param name: A attribute name.
        @type name: str
        @return: A tuple: the requested (attribute, ancestry).
        @rtype: (L{SchemaObject}, [L{SchemaObject},..])

        """
        for child, ancestry in self.attributes():
            if child.name == name:
                return child, ancestry
        return None, []

    def get_child(self, name):
        """
        Get (find) a I{non-attribute} child by name.

        @param name: A child name.
        @type name: str
        @return: A tuple: the requested (child, ancestry).
        @rtype: (L{SchemaObject}, [L{SchemaObject},..])

        """
        for child, ancestry in self.children():
            if child.any() or child.name == name:
                return child, ancestry
        return None, []

    def namespace(self, prefix=None):
        """
        Get this property's namespace.

        @param prefix: The default prefix.
        @type prefix: str
        @return: The schema's target namespace.
        @rtype: (I{prefix}, I{URI})

        """
        ns = self.schema.tns
        if ns[0] is None:
            ns = (prefix, ns[1])
        return ns

    def default_namespace(self):
        return self.root.defaultNamespace()

    def multi_occurrence(self):
        """
        Get whether the node has multiple occurrences, i.e. is a I{collection}.

        @return: True if it has, False if it has at most 1 occurrence.
        @rtype: boolean

        """
        max = self.max
        if max is None:
            return False
        if max.isdigit():
            return int(max) > 1
        return max == "unbounded"

    def optional(self):
        """
        Get whether this type is optional.

        @return: True if optional, else False.
        @rtype: boolean

        """
        return self.min == "0"

    def required(self):
        """
        Get whether this type is required.

        @return: True if required, else False.
        @rtype: boolean

        """
        return not self.optional()

    def resolve(self, nobuiltin=False):
        """
        Resolve the node's type reference and return the referenced type node.

        Only XSD schema objects that actually support 'having a type' custom
        implement this interface while others simply resolve as themselves.

        @param nobuiltin: Flag indicating whether resolving to an external XSD
            built-in type should not be allowed.
        @return: The resolved (true) type.
        @rtype: L{SchemaObject}
        """
        return self

    def sequence(self):
        """
        Get whether this is an <xsd:sequence/>.

        @return: True if <xsd:sequence/>, else False.
        @rtype: boolean

        """
        return False

    def xslist(self):
        """
        Get whether this is an <xsd:list/>.

        @return: True if <xsd:list/>, else False.
        @rtype: boolean

        """
        return False

    def all(self):
        """
        Get whether this is an <xsd:all/>.

        @return: True if <xsd:all/>, else False.
        @rtype: boolean

        """
        return False

    def choice(self):
        """
        Get whether this is an <xsd:choice/>.

        @return: True if <xsd:choice/>, else False.
        @rtype: boolean

        """
        return False

    def any(self):
        """
        Get whether this is an <xsd:any/>.

        @return: True if <xsd:any/>, else False.
        @rtype: boolean

        """
        return False

    def builtin(self):
        """
        Get whether this is a built-in schema-instance XSD type.

        @return: True if a built-in type, else False.
        @rtype: boolean

        """
        return False

    def enum(self):
        """
        Get whether this is a simple-type containing an enumeration.

        @return: True if enumeration, else False.
        @rtype: boolean

        """
        return False

    def isattr(self):
        """
        Get whether the object is a schema I{attribute} definition.

        @return: True if an attribute, else False.
        @rtype: boolean

        """
        return False

    def extension(self):
        """
        Get whether the object is an extension of another type.

        @return: True if an extension, else False.
        @rtype: boolean

        """
        return False

    def restriction(self):
        """
        Get whether the object is an restriction of another type.

        @return: True if a restriction, else False.
        @rtype: boolean

        """
        return False

    def mixed(self):
        """Get whether the object has I{mixed} content."""
        return False

    def find(self, qref, classes=[], ignore=None):
        """
        Find a referenced type in self or children. Return None if not found.

        Qualified references for all schema objects checked in this search will
        be added to the set of ignored qualified references to avoid the find
        operation going into an infinite loop in case of recursively defined
        structures.

        @param qref: A qualified reference.
        @type qref: qref
        @param classes: A collection of classes used to qualify the match.
        @type classes: Collection(I{class},...), e.g. [I(class),...]
        @param ignore: A set of qualified references to ignore in this search.
        @type ignore: {qref,...}
        @return: The referenced type.
        @rtype: L{SchemaObject}
        @see: L{qualify()}

        """
        if not len(classes):
            classes = (self.__class__,)
        if ignore is None:
            ignore = set()
        if self.qname in ignore:
            return
        ignore.add(self.qname)
        if self.qname == qref and self.__class__ in classes:
            return self
        for c in self.rawchildren:
            p = c.find(qref, classes, ignore=ignore)
            if p is not None:
                return p

    def translate(self, value, topython=True):
        """
        Translate between an XSD type values and Python objects.

        When converting a Python object to an XSD type value the operation may
        return any Python object whose string representation matches the
        desired XSD type value.

        @param value: A value to translate.
        @type value: str if topython is True; any Python object otherwise
        @param topython: Flag indicating the translation direction.
        @type topython: bool
        @return: The converted I{language} type.

        """
        return value

    def childtags(self):
        """
        Get a list of valid child tag names.

        @return: A list of child tag names.
        @rtype: [str,...]

        """
        return ()

    def dependencies(self):
        """
        Get a list of dependencies for dereferencing.

        @return: A merge dependency index and a list of dependencies.
        @rtype: (int, [L{SchemaObject},...])

        """
        return None, []

    def autoqualified(self):
        """
        The list of I{auto} qualified attribute values.

        Qualification means to convert values into I{qref}.

        @return: A list of attribute names.
        @rtype: list

        """
        return ["type", "ref"]

    def qualify(self):
        """
        Convert reference attribute values into a I{qref}.

        Constructed I{qref} uses the default document namespace. Since many
        WSDL schemas are written improperly: when the document does not define
        its default namespace, the schema target namespace is used to qualify
        references.

        """
        defns = self.root.defaultNamespace()
        if Namespace.none(defns):
            defns = self.schema.tns
        for a in self.autoqualified():
            ref = getattr(self, a)
            if ref is None:
                continue
            if isqref(ref):
                continue
            qref = qualify(ref, self.root, defns)
            log.debug("%s, convert %s='%s' to %s", self.id, a, ref, qref)
            setattr(self, a, qref)

    def merge(self, other):
        """Merge another object as needed."""
        other.qualify()
        for n in ("default", "max", "min", "name", "nillable", "qname",
                "type"):
            if getattr(self, n) is not None:
                continue
            v = getattr(other, n)
            if v is None:
                continue
            setattr(self, n, v)

    def content(self, collection=None, filter=Filter(), history=None):
        """
        Get a I{flattened} list of this node's contents.

        @param collection: A list to fill.
        @type collection: list
        @param filter: A filter used to constrain the result.
        @type filter: L{Filter}
        @param history: The history list used to prevent cyclic dependency.
        @type history: list
        @return: The filled list.
        @rtype: list

        """
        if collection is None:
            collection = []
        if history is None:
            history = []
        if self in history:
            return collection
        history.append(self)
        if self in filter:
            collection.append(self)
        for c in self.rawchildren:
            c.content(collection, filter, history)
        history.pop()
        return collection

    def str(self, indent=0, history=None):
        """
        Get a string representation of this object.

        @param indent: The indent.
        @type indent: int
        @return: A string.
        @rtype: str

        """
        if history is None:
            history = []
        if self in history:
            return "%s ..." % Repr(self)
        history.append(self)
        tab = "%*s" % (indent * 3, "")
        result = ["%s<%s" % (tab, self.id)]
        for n in self.description():
            if not hasattr(self, n):
                continue
            v = getattr(self, n)
            if v is None:
                continue
            result.append(' %s="%s"' % (n, v))
        if len(self):
            result.append(">")
            for c in self.rawchildren:
                result.append("\n")
                result.append(c.str(indent+1, history[:]))
                if c.isattr():
                    result.append("@")
            result.append("\n%s" % (tab,))
            result.append("</%s>" % (self.__class__.__name__,))
        else:
            result.append(" />")
        return "".join(result)

    def description(self):
        """
        Get the names used for repr() and str() description.

        @return:  A dictionary of relevant attributes.
        @rtype: [str,...]

        """
        return ()

    def __unicode__(self):
        return str(self.str())

    def __repr__(self):
        s = []
        s.append("<%s" % (self.id,))
        for n in self.description():
            if not hasattr(self, n):
                continue
            v = getattr(self, n)
            if v is None:
                continue
            s.append(' %s="%s"' % (n, v))
        s.append(" />")
        return "".join(s)

    def __len__(self):
        n = 0
        for x in self:
            n += 1
        return n

    def __iter__(self):
        return Iter(self)

    def __getitem__(self, index):
        """
        Returns a contained schema object referenced by its 0-based index.

        Returns None if such an object does not exist.

        """
        i = 0
        for c in self:
            if i == index:
                return c
            i += 1


class Iter:
    """
    The content iterator - used to iterate the L{Content} children.

    The iterator provides a I{view} of the children that is free of container
    elements such as <xsd::all/>, <xsd:choice/> or <xsd:sequence/>.

    @ivar stack: A stack used to control nesting.
    @type stack: list

    """

    class Frame:
        """A content iterator frame."""

        def __init__(self, sx):
            """
            @param sx: A schema object.
            @type sx: L{SchemaObject}

            """
            self.sx = sx
            self.items = sx.rawchildren
            self.index = 0

        def __next__(self):
            """
            Get the I{next} item in the frame's collection.

            @return: The next item or None
            @rtype: L{SchemaObject}

            """
            if self.index < len(self.items):
                result = self.items[self.index]
                self.index += 1
                return result

    def __init__(self, sx):
        """
        @param sx: A schema object.
        @type sx: L{SchemaObject}

        """
        self.stack = []
        self.push(sx)

    def push(self, sx):
        """
        Create a frame and push the specified object.

        @param sx: A schema object to push.
        @type sx: L{SchemaObject}

        """
        self.stack.append(Iter.Frame(sx))

    def pop(self):
        """
        Pop the I{top} frame.

        @return: The popped frame.
        @rtype: L{Frame}
        @raise StopIteration: when stack is empty.

        """
        if self.stack:
            return self.stack.pop()
        raise StopIteration()

    def top(self):
        """
        Get the I{top} frame.

        @return: The top frame.
        @rtype: L{Frame}
        @raise StopIteration: when stack is empty.

        """
        if self.stack:
            return self.stack[-1]
        raise StopIteration()

    def __next__(self):
        """
        Get the next item.

        @return: A tuple: the next (child, ancestry).
        @rtype: (L{SchemaObject}, [L{SchemaObject},..])
        @raise StopIteration: A the end.

        """
        frame = self.top()
        while True:
            result = next(frame)
            if result is None:
                self.pop()
                return next(self)
            if isinstance(result, Content):
                ancestry = [f.sx for f in self.stack]
                return result, ancestry
            self.push(result)
            return next(self)

    def __iter__(self):
        return self


class XBuiltin(SchemaObject):
    """Represents a built-in XSD schema <xsd:*/> node."""

    def __init__(self, schema, name):
        """
        @param schema: The containing schema.
        @type schema: L{schema.Schema}

        """
        root = Element(name)
        SchemaObject.__init__(self, schema, root)
        self.name = name
        self.nillable = True

    def namespace(self, prefix=None):
        return Namespace.xsdns

    def builtin(self):
        return True


class Content(SchemaObject):
    """XSD schema objects representing real XML document content."""
    pass


class NodeFinder:
    """
    Find nodes based on flexable criteria.

    I{matcher} may be any object implementing a match(n) method.

    @ivar matcher: An object used as criteria for match.
    @type matcher: I{any}.match(n)
    @ivar limit: Limit the number of matches.  0=unlimited.
    @type limit: int

    """
    def __init__(self, matcher, limit=0):
        """
        @param matcher: An object used as criteria for match.
        @type matcher: I{any}.match(n)
        @param limit: Limit the number of matches. 0=unlimited.
        @type limit: int

        """
        self.matcher = matcher
        self.limit = limit

    def find(self, node, list):
        """
        Traverse the tree looking for matches.

        @param node: A node to match on.
        @type node: L{SchemaObject}
        @param list: A list to fill.
        @type list: list

        """
        if self.matcher.match(node):
            list.append(node)
            self.limit -= 1
            if self.limit == 0:
                return
        for c in node.rawchildren:
            self.find(c, list)
        return self