def create(name, *children, **kargs): '''\ Creates an element and converts parameters to appropriate types. :param children: The element child nodes. All items that are not :class:`XMLNode` instances create :class:`Text` nodes after they have been converted to Unicode strings. :param kargs: The item ``attributes`` defines the attributes and must have a method :meth:`items()` (like :class:`dict`) which returns an iterable of 2-:func:`tuple` instances containing the attribute name as the first and the attribute value as the second item. Attribute names and values are converted to Unicode strings. :returns: The created element. :rtype: :class:`Element` :raises ecoxipy.XMLWellFormednessException: If the ``name`` is not a valid XML name. ''' attributes = kargs.get('attributes', {}) return Element(_unicode(name), [ child if isinstance(child, XMLNode) else Text.create(child) for child in children ], {} if attributes is None else { _unicode(attr_name): _unicode(attr_value) for attr_name, attr_value in attributes.items() }, True)
def create(name, *children, **kargs): '''\ Creates an element and converts parameters to appropriate types. :param children: The element child nodes. All items that are not :class:`XMLNode` instances create :class:`Text` nodes after they have been converted to Unicode strings. :param kargs: The item ``attributes`` defines the attributes and must have a method :meth:`items()` (like :class:`dict`) which returns an iterable of 2-:func:`tuple` instances containing the attribute name as the first and the attribute value as the second item. Attribute names and values are converted to Unicode strings. :returns: The created element. :rtype: :class:`Element` :raises ecoxipy.XMLWellFormednessException: If the ``name`` is not a valid XML name. ''' attributes = kargs.get('attributes', {}) return Element(_unicode(name), [ child if isinstance(child, XMLNode) else Text.create(child) for child in children ], {} if attributes is None else { _unicode(attr_name): _unicode(attr_value) for attr_name, attr_value in list(attributes.items()) }, True)
def __call__(self, uri=True, local_name=True): '''\ Retrieve an iterator over the nodes with the namespace information specified by the arguments. :param uri: The namespace URI to match. If this is :const:`True` all nodes with a :attr:`~ecoxipy.pyxom.NamespaceNameMixin.namespace_uri` different from :const:`False` (i.e. invalid namespace information) will be matched. Otherwise only those nodes with a namespace URI equal to this value will be matched. :param local_name: The local name to match. If this is :const:`True` the :attr:`~ecoxipy.pyxom.NamespaceNameMixin.local_name` value is not taken into account. Otherwise those nodes with the local name equal to this value will be matched. :raises KeyError: If either a non-:const:`True` ``uri`` or ``local_name`` cannot be found. ''' if uri is not True and uri is not False: uri = _unicode(uri) if local_name is not True: local_name = _unicode(local_name) def all(): for namespace_uri in self._by_namespace_uri: if namespace_uri is not False: for node in self._by_namespace_uri[namespace_uri]: yield node if uri is True: if local_name is True: return all() return self._by_local_name[local_name] if local_name is True: return self._by_namespace_uri[uri] return iter(self._by_namespace_uri(uri).intersection( self._by_local_name(local_name)))
def endElement(self, name): _name, attrs = self._element_stack.pop() assert name == _name element = self._output.element(_unicode(name), self._current_children, { _unicode(attr_name): _unicode(attr_value) for attr_name, attr_value in attrs.items() }) self._leave_node(element)
def endElement(self, name): _name, attrs = self._element_stack.pop() assert name == _name element = self._output.element(_unicode(name), self._current_children, { _unicode(attr_name): _unicode(attr_value) for attr_name, attr_value in list(attrs.items()) }) self._leave_node(element)
def _parse_values(name, publicid, systemid): if name is None: publicid = None systemid = None else: name = _unicode(name) if publicid is not None: publicid = _unicode(publicid) if systemid is not None: systemid = _unicode(systemid) return name, publicid, systemid
def doctype(self, value): if value is None: name = None publicid = None systemid = None else: if value.__class__ is bytes: value = _unicode(value) try: name = value.get('name', None) publicid = value.get('publicid', None) systemid = value.get('systemid', None) except AttributeError: if value.__class__ is _unicode: name = value publicid = None systemid = None else: if len(value) > 2: systemid = value[2] else: systemid = None if len(value) > 1: publicid = value[1] else: publicid = None if len(value) > 0: name = value[0] else: name = None name, publicid, systemid = DocumentType._parse_values( name, publicid, systemid) self._doctype.name = name self._doctype.publicid = publicid self._doctype.systemid = systemid
def create(*children, **kargs): '''\ Creates a document and converts parameters to appropriate types. :param children: The document root nodes. All items that are not :class:`XMLNode` instances create :class:`Text` nodes after they have been converted to Unicode strings. :param kargs: The same parameters as the constructor has (except ``children``) are recognized. The items ``doctype_name``, ``doctype_publicid``, ``doctype_systemid``, and ``encoding`` are converted to Unicode strings if they are not :const:`None`. ``omit_xml_declaration`` is converted to boolean. :returns: The created document. :rtype: :class:`Document` :raises ecoxipy.XMLWellFormednessException: If ``doctype_name`` is not a valid XML name, ``doctype_publicid`` is not a valid public ID or ``doctype_systemid`` is not a valid system ID. ''' doctype_name = kargs.get('doctype_name', None) doctype_publicid = kargs.get('doctype_publicid', None) doctype_systemid = kargs.get('doctype_systemid', None) doctype_name, doctype_publicid, doctype_systemid = DocumentType._parse_values( doctype_name, doctype_publicid, doctype_systemid) omit_xml_declaration = kargs.get('omit_xml_declaration', None) omit_xml_declaration = bool(omit_xml_declaration) encoding = kargs.get('encoding', None) if encoding is not None: encoding = _unicode(encoding) return Document(doctype_name, doctype_publicid, doctype_systemid, [ child if isinstance(child, XMLNode) else Text.create(child) for child in children ], omit_xml_declaration, encoding, True)
def create_sax_events(self, content_handler=None, out=None, out_encoding='UTF-8', indent_incr=None): '''\ Creates SAX events. :param content_handler: If this is :const:`None` a :class:`xml.sax.saxutils.XMLGenerator` is created and used as the content handler. If in this case ``out`` is not :const:`None`, it is used for output. :type content_handler: :class:`xml.sax.ContentHandler` :param out: The output to write to if no ``content_handler`` is given. It should have a :meth:`write` method like files. :param out_encoding: The output encoding or :const:`None` for Unicode output. :param indent_incr: If this is not :const:`None` this activates pretty printing. In this case it should be a string and it is used for indenting. :type indent_incr: :func:`str` :returns: The content handler used. ''' if content_handler is None: content_handler = XMLGenerator(out, out_encoding) if indent_incr is None: indent = False else: indent_incr = _unicode(indent_incr) indent = (indent_incr, 0) self._create_sax_events(content_handler, indent) return content_handler
def name(self, name): name = _unicode(name) if name == self._name: return if self._check_well_formedness: _helpers.enforce_valid_xml_name(name) self._name = name self._clear_namespace_properties()
def name(self, name): if name is None: self._publicid = None self._systemid = None else: name = _unicode(name) if self._check_well_formedness: _helpers.enforce_valid_xml_name(name) self._name = name
def create(target, content=None): '''\ Creates a processing instruction node and converts the parameters to appropriate types. :param target: The :attr:`target`, will be converted to an Unicode string. :param content: The :attr:`content`, if it is not :const:`None` it will be converted to an Unicode string. :returns: The created processing instruction. :rtype: :class:`ProcessingInstruction` :raises ecoxipy.XMLWellFormednessException: If either the ``target`` or the ``content`` are not valid. ''' target = _unicode(target) if content is not None: content = _unicode(content) return ProcessingInstruction(target, content, True)
def create_attribute(self, name, value): '''\ Create a new :class:`Attribute` as part of the instance. :param name: the attribute's name :param value: the attribute's value :returns: the created attribute :rtype: :class:`Attribute` :raises KeyError: If an attribute with ``name`` already exists in the instance. ''' name = _unicode(name) if name in self._attributes: raise KeyError( u'An attribute with name "{}" already exists.'.format(name)) value = _unicode(value) attribute = Attribute(self, name, value, self._check_well_formedness) self._attributes[name] = attribute return attribute
def create(cls, content): '''\ Creates an instance of the :class:`ContentNode` implementation and converts ``content`` to an Unicode string. :param content: The content of the node. This will be converted to an Unicode string. :returns: The created :class:`ContentNode` implementation instance. ''' return cls(_unicode(content))
def register(self, key, value): '''\ Add ``value`` to the set identified by ``key``. :param key: the identifier :type key: Unicode string :param value: the entry's value ''' key = _unicode(key) self._index[key] = value
def create_attribute(self, name, value): '''\ Create a new :class:`Attribute` as part of the instance. :param name: the attribute's name :param value: the attribute's value :returns: the created attribute :rtype: :class:`Attribute` :raises KeyError: If an attribute with ``name`` already exists in the instance. ''' name = _unicode(name) if name in self._attributes: raise KeyError( 'An attribute with name "{}" already exists.'.format(name)) value = _unicode(value) attribute = Attribute(self, name, value, self._check_well_formedness) self._attributes[name] = attribute return attribute
def create(content): '''\ Creates a comment node. :param content: The content of the comment. This will be converted to an Unicode string. :returns: The created commment node. :rtype: :class:`Comment` :raises ecoxipy.XMLWellFormednessException: If ``content`` is not valid. ''' content = _unicode(content) return Comment(content, True)
def __call__(self, uri=True, local_name=True): '''\ Retrieve an iterator over the nodes with the namespace information specified by the arguments. :param uri: The namespace URI to match. If this is :const:`True` all nodes with a :attr:`~ecoxipy.pyxom.NamespaceNameMixin.namespace_uri` different from :const:`False` (i.e. invalid namespace information) will be matched. Otherwise only those nodes with a namespace URI equal to this value will be matched. :param local_name: The local name to match. If this is :const:`True` the :attr:`~ecoxipy.pyxom.NamespaceNameMixin.local_name` value is not taken into account. Otherwise those nodes with the local name equal to this value will be matched. :raises KeyError: If either a non-:const:`True` ``uri`` or ``local_name`` cannot be found. ''' if uri is not True and uri is not False: uri = _unicode(uri) if local_name is not True: local_name = _unicode(local_name) def all(): for namespace_uri in self._by_namespace_uri: if namespace_uri is not False: for node in self._by_namespace_uri[namespace_uri]: yield node if uri is True: if local_name is True: return all() return self._by_local_name[local_name] if local_name is True: return self._by_namespace_uri[uri] return iter( self._by_namespace_uri(uri).intersection( self._by_local_name(local_name)))
def __init__(self, element_names=None, attribute_names=None, pi_targets=None, blacklist=True, silent=True): if bool(blacklist): self._invalid = self._blacklist_invalid none_container = self._NothingContainer else: self._invalid = self._whitelist_invalid none_container = self._EverythingContainer create_set = lambda items: (none_container if items is None else {_unicode(item) for item in items}) self._element_names = create_set(element_names) self._attribute_names = create_set(attribute_names) self._pi_targets = create_set(pi_targets) self._silent = bool(silent)
def name(self, name): name = _unicode(name) if name == self._name: return if self._check_well_formedness: _helpers.enforce_valid_xml_name(name) if name in self._parent._attributes: raise KeyError( u'An attribute with name "{}" does already exist in the parent.'.format( name)) del self._parent._attributes[self._name] self._parent._attributes[name] = self self._name = name self._clear_namespace_properties() self._update_namespace_prefix()
def name(self, name): name = _unicode(name) if name == self._name: return if self._check_well_formedness: _helpers.enforce_valid_xml_name(name) if name in self._parent._attributes: raise KeyError( 'An attribute with name "{}" does already exist in the parent.' .format(name)) del self._parent._attributes[self._name] self._parent._attributes[name] = self self._name = name self._clear_namespace_properties() self._update_namespace_prefix()
def systemid(self, systemid): if systemid is not None: systemid = _unicode(systemid) if self._check_well_formedness: _helpers.enforce_valid_doctype_systemid(systemid) self._systemid = systemid
def content(self, value): self._content = _unicode(value)
def __delitem__(self, name): name = _unicode(name) item = self._attributes[name] item._clear_namespace_uri() del self._attributes[name] del item._parent
def content(self, content): if content is not None: content = _unicode(content) if self._check_well_formedness: _helpers.enforce_valid_pi_content(content) self._content = content
def __str__(self): return _unicode(self.create_str(encoding=None))
def __getitem__(self, name): name = _unicode(name) return self._attributes[name]
def processingInstruction(self, target, data): pi = self._output.processing_instruction(_unicode(target), _unicode(data)) self._append_node(pi)
def value(self, value): value = _unicode(value) if value == self._value: return self._update_namespace_uri() self._value = value
def __getitem__(self, key): key = _unicode(key) return self._index[key]
def notationDecl(self, name, publicId, systemId): self._doctype_name = _unicode(name) self._doctype_publicid = _unicode(publicId) self._doctype_systemid = _unicode(systemId)
def characters(self, content): text = self._output.text(_unicode(content)) self._append_node(text)
def __contains__(self, key): key = _unicode(key) return key in self._index
def __contains__(self, name): name = _unicode(name) return name in self._attributes
def encoding(self, value): if value is None: value = 'UTF-8' else: value = _unicode(value) self._encoding = value
def target(self, target): target = _unicode(target) if self._check_well_formedness: _helpers.enforce_valid_pi_target(target) self._target = _unicode(target)
def publicid(self, publicid): if publicid is not None: publicid = _unicode(publicid) if self._check_well_formedness: _helpers.enforce_valid_doctype_publicid(publicid) self._publicid = publicid
def content(self, content): content = _unicode(content) if self._check_well_formedness: _helpers.enforce_valid_comment(content) self._content = content
def encoding(self, value): if value is None: value = u'UTF-8' else: value = _unicode(value) self._encoding = value
def comment(self, content): comment = self._output.comment(_unicode(content)) self._append_node(comment)