Package pyxb :: Package namespace :: Module utility
[hide private]
[frames] | no frames]

Source Code for Module pyxb.namespace.utility

 1  # Copyright 2009, Peter A. Bigot 
 2  # 
 3  # Licensed under the Apache License, Version 2.0 (the "License"); you may 
 4  # not use this file except in compliance with the License. You may obtain a 
 5  # copy of the License at: 
 6  # 
 7  #            http://www.apache.org/licenses/LICENSE-2.0 
 8  # 
 9  # Unless required by applicable law or agreed to in writing, software 
10  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
11  # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
12  # License for the specific language governing permissions and limitations 
13  # under the License. 
14   
15  """Utility functions related to U{XML Namespaces<http://www.w3.org/TR/2006/REC-xml-names-20060816/index.html>}.""" 
16   
17  import pyxb 
18  import pyxb.namespace 
19   
20 -def NamespaceInstance (namespace):
21 """Get a namespace instance for the given namespace. 22 23 This is used when it is unclear whether the namespace is specified by URI 24 or by instance or by any other mechanism we might dream up in the 25 future.""" 26 if isinstance(namespace, pyxb.namespace.Namespace): 27 return namespace 28 if isinstance(namespace, basestring): 29 return NamespaceForURI(namespace, True) 30 raise pyxb.LogicError('Cannot identify namespace from value of type %s' % (type(namespace),))
31
32 -def NamespaceForURI (uri, create_if_missing=False):
33 """Given a URI, provide the L{Namespace} instance corresponding to it. 34 35 This can only be used to lookup or create real namespaces. To create 36 absent namespaces, use L{CreateAbsentNamespace}. 37 38 @param uri: The URI that identifies the namespace 39 @type uri: A non-empty C{str} or C{unicode} string 40 @keyword create_if_missing: If C{True}, a namespace for the given URI is 41 created if one has not already been registered. Default is C{False}. 42 @type create_if_missing: C{bool} 43 @return: The Namespace corresponding to C{uri}, if available 44 @rtype: L{Namespace} or C{None} 45 @raise pyxb.LogicError: The uri is not a non-empty string 46 """ 47 if not isinstance(uri, (str, unicode)): 48 raise pyxb.LogicError('Cannot lookup absent namespaces') 49 if 0 == len(uri): 50 raise pyxb.LogicError('Namespace URIs must not be empty strings') 51 rv = pyxb.namespace.Namespace._NamespaceForURI(uri) 52 if (rv is None) and create_if_missing: 53 rv = pyxb.namespace.Namespace(uri) 54 return rv
55
56 -def CreateAbsentNamespace ():
57 """Create an absent namespace. 58 59 Use this when you need a namespace for declarations in a schema with no 60 target namespace. Absent namespaces are not stored in the infrastructure; 61 it is your responsibility to hold on to the reference you get from this, 62 because you won't be able to look it up.""" 63 return pyxb.namespace.Namespace.CreateAbsentNamespace()
64
65 -def AvailableNamespaces ():
66 """Return the complete set of Namespace instances known to the system.""" 67 return pyxb.namespace.Namespace.AvailableNamespaces()
68