1# This program is free software; you can redistribute it and/or modify
2# it under the terms of the (LGPL) GNU Lesser General Public License as
3# published by the Free Software Foundation; either version 3 of the
4# License, or (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9# GNU Library Lesser General Public License for more details at
10# ( http://www.gnu.org/licenses/lgpl.html ).
11#
12# You should have received a copy of the GNU Lesser General Public License
13# along with this program; if not, write to the Free Software
14# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15# written by: Jeff Ortel ( jortel@redhat.com )
16
17
18from suds import *
19from suds.sax import Namespace, splitPrefix
20
21
22def qualify(ref, resolvers, defns=Namespace.default):
23    """
24    Get a reference that is I{qualified} by namespace.
25    @param ref: A referenced schema type name.
26    @type ref: str
27    @param resolvers: A list of objects to be used to resolve types.
28    @type resolvers: [L{sax.element.Element},]
29    @param defns: An optional target namespace used to qualify references
30        when no prefix is specified.
31    @type defns: A default namespace I{tuple: (prefix,uri)} used when ref not prefixed.
32    @return: A qualified reference.
33    @rtype: (name, namespace-uri)
34    """
35    ns = None
36    p, n = splitPrefix(ref)
37    if p is not None:
38        if not isinstance(resolvers, (list, tuple)):
39            resolvers = (resolvers,)
40        for r in resolvers:
41            resolved = r.resolvePrefix(p)
42            if resolved[1] is not None:
43                ns = resolved
44                break
45        if ns is None:
46            raise Exception('prefix (%s) not resolved' % p)
47    else:
48        ns = defns
49    return (n, ns[1])
50
51def isqref(object):
52    """
53    Get whether the object is a I{qualified reference}.
54    @param object: An object to be tested.
55    @type object: I{any}
56    @rtype: boolean
57    @see: L{qualify}
58    """
59    return (\
60        isinstance(object, tuple) and \
61        len(object) == 2 and \
62        isinstance(object[0], str) and \
63        isinstance(object[1], str))
64
65
66class Filter:
67    def __init__(self, inclusive=False, *items):
68        self.inclusive = inclusive
69        self.items = items
70    def __contains__(self, x):
71        if self.inclusive:
72            result = ( x in self.items )
73        else:
74            result = ( x not in self.items )
75        return result
76