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

Source Code for Module pyxb.namespace.utility

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