Package pyxb :: Package namespace :: Module resolution :: Class NamespaceContext
[hide private]
[frames] | no frames]

Class NamespaceContext

source code

object --+
         |
        NamespaceContext

Records information associated with namespaces at a DOM node.

Instance Methods [hide private]
 
__str__(self)
str(x)
 
defaultNamespace(self)
The default namespace in effect at this node.
source code
 
setDefaultNamespace(self, default_namespace)
Set the default namespace for the generated document.
source code
 
targetNamespace(self)
The target namespace in effect at this node.
source code
 
inScopeNamespaces(self)
Map from prefix strings to Namespace instances associated with those prefixes.
source code
 
__removePrefixMap(self, pfx) source code
 
__addPrefixMap(self, pfx, ns) source code
 
__clonePrefixMap(self) source code
 
prefixForNamespace(self, namespace)
Return a prefix associated with the given namespace in this context, or None if the namespace is the default or is not in scope.
source code
 
setNodeContext(self, node) source code
 
declareNamespace(self, namespace, prefix=None, add_to_map=False)
Record the given namespace as one to be used in this document.
source code
 
processXMLNS(self, prefix, uri) source code
 
finalizeTargetNamespace(self, tns_uri=None, including_context=None) source code
 
reset(self)
Reset this instance to the state it was when created, exclusive of XMLNS directives passed in a constructor dom_node parameter.
source code
 
__init__(self, dom_node=None, parent_context=None, including_context=None, recurse=True, default_namespace=None, target_namespace=None, in_scope_namespaces=None, expanded_name=None, finalize_target_namespace=True)
Determine the namespace context that should be associated with the given node and, optionally, its element children.
source code
 
interpretQName(self, name, namespace=None, default_no_namespace=False)
Convert the provided name into an ExpandedName, i.e.
source code
 
queueForResolution(self, component, depends_on=None)
Forwards to queueForResolution() in targetNamespace().
source code
 
__unicode__(self) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Class Methods [hide private]
 
PushContext(cls, ctx)
Make ctx the currently active namespace context.
source code
 
Current(cls)
Access the currently active namespace context.
source code
 
PopContext(cls)
Discard the currently active namespace context, restoring its predecessor.
source code
 
_AddTargetNamespaceAttribute(cls, expanded_name, attribute_name) source code
 
_TargetNamespaceAttribute(cls, expanded_name) source code
 
__BuildInitialPrefixMap(cls) source code
 
GetNodeContext(cls, node, **kw)
Get the NamespaceContext instance that was assigned to the node.
source code
Class Variables [hide private]
  __ContextStack = []
  __TargetNamespaceAttributes = {<pyxb.namespace.ExpandedName ob...
  __pendingReferencedNamespaces = None
hash(x)
  __defaultNamespace = None
hash(x)
  __fallbackToTargetNamespace = False
  __targetNamespace = None
hash(x)
  __inScopeNamespaces = None
Map from Namespace instances to sets of prefix strings associated with the namespace.
  __inScopePrefixes = None
hash(x)
  __InitialScopeNamespaces = {'xml': <pyxb.namespace.builtin._XM...
  __InitialScopePrefixes = {<pyxb.namespace.Namespace object>: s...
  __initialScopeNamespaces = None
hash(x)
  __initialScopePrefixes = None
hash(x)
  __namespacePrefixCounter = None
hash(x)
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__str__(self)
(Informal representation operator)

 

str(x)

Overrides: object.__str__
(inherited documentation)

PushContext(cls, ctx)
Class Method

source code 

Make ctx the currently active namespace context.

Prior contexts are retained on a LIFO stack.

Current(cls)
Class Method

source code 

Access the currently active namespace context.

If no context is active, None is returned. This probably represents mis-use of the infrastructure (viz., failure to record the context within which a QName must be resolved).

PopContext(cls)
Class Method

source code 

Discard the currently active namespace context, restoring its predecessor.

The discarded context is returned.

defaultNamespace(self)

source code 

The default namespace in effect at this node. E.g., xmlns="URN:default".

setDefaultNamespace(self, default_namespace)

source code 

Set the default namespace for the generated document.

Even if invoked post construction, the default namespace will affect the entire document, as all namespace declarations are placed in the document root.

Parameters:
  • default_namespace (pyxb.namespace.Namespace or str or unicode.) - The namespace to be defined as the default namespace in the top-level element of the document. May be provided as a real namespace, or just its URI.

targetNamespace(self)

source code 

The target namespace in effect at this node. Usually from the targetNamespace attribute. If no namespace is specified for the schema, an absent namespace was assigned upon creation and will be returned.

inScopeNamespaces(self)

source code 

Map from prefix strings to Namespace instances associated with those prefixes. The prefix None identifies the default namespace.

GetNodeContext(cls, node, **kw)
Class Method

source code 

Get the NamespaceContext instance that was assigned to the node.

If none has been assigned and keyword parameters are present, create one treating this as the root node and the keyword parameters as configuration information (e.g., default_namespace).

Raises:
  • pyxb.LogicError - no context is available and the keywords required to create one were not provided

declareNamespace(self, namespace, prefix=None, add_to_map=False)

source code 

Record the given namespace as one to be used in this document.

Parameters:
  • namespace (pyxb.namespace.Namespace) - The namespace to be associated with the document.
  • prefix - Optional prefix to be used with this namespace. If not provided, a unique prefix is generated or a standard prefix is used, depending on the namespace.
Returns:
a prefix that may be used with the namespace. If prefix was None the return value may be a previously-assigned prefix.
To Do:

reset(self)

source code 

Reset this instance to the state it was when created, exclusive of XMLNS directives passed in a constructor dom_node parameter.

This preserves parent context and constructor-specified prefix maps, but clears the namespace-prefix mapping of any additions made while processing namespace directives in DOM nodes, or manually added post-construction.

The defaultNamespace is also retained.

__init__(self, dom_node=None, parent_context=None, including_context=None, recurse=True, default_namespace=None, target_namespace=None, in_scope_namespaces=None, expanded_name=None, finalize_target_namespace=True)
(Constructor)

source code 

Determine the namespace context that should be associated with the given node and, optionally, its element children.

Primarily this class maintains a map between namespaces and prefixes used in QName instances. The initial map comprises the bound prefixes (xml and xmlns), prefixes inherited from parent_context, and prefixes passed through the in_scope_namespaces parameter to the constructor. This map is then augmented by any namespace declarations present in a passed dom_node. The initial map prior to augmentation may be restored through the reset() method.

Parameters:
  • dom_node (xml.dom.Element) - The DOM node
  • parent_context (NamespaceContext) - Optional value that specifies the context associated with dom_node's parent node. If not provided, only the xml namespace is in scope.
  • recurse (bool) - If True (default), create namespace contexts for all element children of dom_node
  • default_namespace (NamespaceContext) - Optional value to set as the default namespace. Values from parent_context would override this, as would an xmlns attribute in the dom_node.
  • target_namespace (NamespaceContext) - Optional value to set as the target namespace. Values from parent_context would override this, as would a targetNamespace attribute in the dom_node
  • in_scope_namespaces (dict mapping prefix string to Namespace.) - Optional value to set as the initial set of in-scope namespaces. The always-present namespaces are added to this if necessary.
Overrides: object.__init__

interpretQName(self, name, namespace=None, default_no_namespace=False)

source code 

Convert the provided name into an ExpandedName, i.e. a tuple of Namespace and local name.

If the name includes a prefix, that prefix must map to an in-scope namespace in this context. Absence of a prefix maps to defaultNamespace(), which must be provided (or defaults to the target namespace, if that is not absent).

Parameters:
  • name (str or unicode) - A QName.
  • name (str or unicode) - Optional namespace to use for unqualified names when there is no default namespace. Note that a defined default namespace, even if absent, supersedes this value.
  • default_no_namespace - If False (default), an NCName in a context where namespace is None and no default or fallback namespace can be identified produces an exception. If True, such an NCName is implicitly placed in no namespace.
Returns:
An ExpandedName tuple: ( Namespace, str )
Raises:

Class Variable Details [hide private]

__TargetNamespaceAttributes

Value:
{<pyxb.namespace.ExpandedName object>: <pyxb.namespace.ExpandedName ob\
ject>}

__inScopeNamespaces

Map from Namespace instances to sets of prefix strings associated with the namespace. The default namespace is not represented.

Value:
None

__InitialScopeNamespaces

Value:
{'xml': <pyxb.namespace.builtin._XML object>,
 'xmlns': <pyxb.namespace.Namespace object>}

__InitialScopePrefixes

Value:
{<pyxb.namespace.Namespace object>: set(['xmlns']),
 <pyxb.namespace.builtin._XML object>: set(['xml'])}