Pycopia XML package

These modules provide methods for parsing, modifying, and generating XML documents. They have a functional style that allows you to compose document trees easily.

It is also partially validating, and needs a DTD file to work with. The model won’t allow you to, say, add an attribute to a node if it is not allowed by the DTD. Or if an attribute is required but not present when serialized to XML an exception is raised. These DTDs are compiled into a Python form. You add node objects by instantiated a class with a name based on an element name in the DTD. This prevents you from accidentally writing invalid XML.

Pythonic Object Model

This module implements the XML POM – the Python Object Model for XML. It is something like DOM, but more Pythonic, and easier to use. These base classes are used to build POM source files which are self-validating python-based XML constructor objects. The major parts of the dtd2py command line tool are also here.

class pycopia.XML.POM.ASIS(data=u'', encoding='utf-8')[source]

Holder for pre-made markup that may be inserted into POM tree. It is a text leaf-node only. You can cache pre-constructed markup and insert it into the POM to speed up some page emission.

add_text(data, encoding=None)[source]
append(data, encoding=None)
destroy()[source]
detach()[source]
emit(fo, encoding=None)[source]
encode(encoding)[source]
fullpath
get_escape_length()[source]
get_text()[source]
has_attributes()[source]
has_children()[source]
insert(data, encoding=None)[source]
matchpath(pe)[source]
set_text(data, encoding='utf-8')[source]
walk(visitor)[source]
class pycopia.XML.POM.BeautifulWriter(fo, inline=[])[source]

A wrapper for a file-like object that is itself a file-like object. It is basically a shim. It attempts to beautify the XML stream emitted by the POM tree. Pass one of these to the emit method if you want better looking output.

write(data)[source]
class pycopia.XML.POM.CDATA(data=u'', encoding='utf-8')[source]
emit(fo, encoding=None)[source]
encode(encoding)[source]
class pycopia.XML.POM.Comment(data=u'', encoding='utf-8')[source]
add_text(data, encoding=None)[source]
append(data, encoding=None)
emit(fo, encoding=None)[source]
encode(encoding)[source]
get_text()[source]
insert(data, encoding=None)[source]
set_text(data, encoding='utf-8')[source]
class pycopia.XML.POM.ContentModel(rawmodel=None)[source]

Represents and validates a content model.

is_empty()[source]
class pycopia.XML.POM.ElementNode(**attribs)[source]
ATTRIBUTES = {}
CONTENTMODEL = None
KWATTRIBUTES = {}
add(klass, **kwargs)[source]

Add an element class from a dtd module.

add_asis(xmltext)[source]

Add text that is already XML markup, or script text.

add_cdata(text, encoding='utf-8')[source]

Add character data that is not parsed.

add_text(text, encoding='utf-8')[source]

Adding text to elements is so common, there is a special method for it.

append(obj)[source]

Append an existing DTD object instance.

attributes()[source]
comment()[source]
del_attribute(name)[source]
del_namespace()[source]
destroy()[source]

destroy() Remove this node and all child node references.

detach()[source]

Detach this node from the tree. It becomes the root of another tree.

elements(elclass)[source]

Return iterator that iterates over list of elements matching elclass

emit(fo, encoding=None, verify=False)[source]
encode(encoding, verify=False)[source]
extend(objlist)[source]
find(elclass, **attribs)[source]
find_elements(pathelement)[source]
fullpath

fullpath() returns the ElementNode’s full path as a string in xpath format.

getAttribute(name)
get_attribute(name)[source]
get_attributes()[source]

return list of DTD defined attribute names.

get_children()[source]
get_element(pathelement)[source]
get_keyword_attributes()[source]

return list of names that may be used as keyword names when instantiating this element.

get_parent()[source]
get_text()[source]
getall(elclass, depth=9223372036854775807, collect=None)[source]
hasAttributes()
has_attribute(name)[source]
has_attributes()[source]
has_children()[source]
index(obj)[source]
insert(index, obj)[source]
matchpath(pathelement)[source]
namespace
node()[source]
parent
pathname()[source]

pathname() returns the ElementNode as a string in xpath format.

pop(idx=-1)[source]

Pop a child element from child list and return it.

processing_instruction()[source]
removeAttribute(name)
replace(newtree)[source]
replace_text(text)[source]
setAttribute(name, val, ns=u'')

set_attribute(name, value) Set the element node attribute “name” to “value”. The name can be the shorthand identifier, or the real name.

set_attribute(name, value)[source]

Set the element node attribute “name” to “value”. The name can be the shorthand identifier, or the real name.

set_encoding(encoding)[source]
set_namespace(ns)[source]
text()[source]
validate(encoding='utf-8')[source]
walk(visitor)[source]
class pycopia.XML.POM.Enumeration[source]
verify(value)[source]
class pycopia.XML.POM.Fragments(**attribs)[source]

Fragments is a special holder class to hold ‘loose’ markup fragments. That is, bits of markup that don’t have a common container (e.g. not in root element). It is invisible.

emit(fo, encoding=None)[source]
encode(encoding)[source]
matchpath(pathelement)[source]
class pycopia.XML.POM.NodeIterator(node, elclass)[source]
next()[source]
class pycopia.XML.POM.Notation(name, pubid, sysid)[source]
class pycopia.XML.POM.POMAttribute(name, value, namespace=u'', encoding='utf-8')[source]
encode(encoding='utf-8')[source]
name
namespace
value
class pycopia.XML.POM.POMDocument(dtd=None, doctype=None, lang=None, encoding='utf-8')[source]
DOCTYPE = ''
MIMETYPE = 'application/xml'
XMLHEADER = '<?xml version="1.0" encoding="utf-8"?>\n'
add_asis(basepath, xmltext)[source]
add_dtd(dtdmod)[source]
add_path(basepath, newnode)[source]
add_text(basepath, text, encoding=None)[source]
del_path(path)[source]
emit(fo, encoding='utf-8')[source]
encode(encoding='utf-8')[source]
get_identity(name)[source]
get_parser(**kwargs)[source]
get_path(path)[source]

Returns an ElementNode instance in the tree addressed by the path.

getpath(path)

Returns an ElementNode instance in the tree addressed by the path.

initialize()[source]

New document initializer.

parse(url, data=None, encoding='utf-8', **kwargs)[source]
parseFile(fo, **kwargs)[source]
parseString(string, **kwargs)[source]
set_dirty(val=1)[source]
set_doctype(doctype)[source]
set_dtd(dtdmod)[source]
set_encoding(encoding)[source]
set_language(lang)[source]
set_path(path, text)[source]
set_root(root)[source]
validate(encoding='utf-8')[source]
walk(visitor)[source]
write_paths(fileobject)[source]
write_xmlfile(filename=None, encoding='utf-8')[source]
writefile(filename=None, encoding='utf-8')
class pycopia.XML.POM.ProcessingInstruction[source]
class pycopia.XML.POM.Text(data=u'', encoding='utf-8')[source]
add_text(data)[source]
append(data)
destroy()[source]
detach()[source]
emit(fo, encoding=None)[source]
encode(encoding)[source]
fullpath
get_escape_length()[source]
get_text()[source]
getall(elclass, depth=0, collect=None)[source]
has_attributes()[source]
has_children()[source]
insert(data)[source]
matchpath(pe)[source]
set_text(data, encoding='utf-8')[source]
walk(visitor)[source]
class pycopia.XML.POM.XMLAttribute(name, a_type, a_decl, a_def=None)[source]

Holds information from the DTD, instantiated from compiled dtd module.

a_decl
a_type
default
name
verify(value)[source]
pycopia.XML.POM.escape(s)[source]
pycopia.XML.POM.find_nodes(node, elclass)[source]
pycopia.XML.POM.make_node(path, modules, value=None)[source]

Makes a node or an XML fragment given a path, element module list, and an optional value.

pycopia.XML.POM.new_document(doctype, encoding='utf-8')[source]
pycopia.XML.POM.normalize_unicode(uni)[source]
pycopia.XML.POM.set_default_encoding(newcodec)[source]
pycopia.XML.POM.swapPOM(dst, src=None)[source]

Replace dest in a DOM tree with source, returning detached tree (dest).

pycopia.XML.POM.to_unicode(arg, encoding)[source]
pycopia.XML.POM.unescape(s)[source]
pycopia.XML.POM.uunescape(s)[source]
pycopia.XML.POM.verify_encoding(newcodec)[source]

Pythonic Object Model parser.

class pycopia.XML.POMparse.XMLAttribute(name, a_type, a_decl, a_def=None)[source]

Holds information from the DTD, instantiated from compiled dtd module.

DTD Parsing

Deals with DTD files. Primarily, provides the DTD parser and Python “compiler”.

class pycopia.XML.DTD.AttributeMap[source]
class pycopia.XML.DTD.DTDConsumerForSourceGeneration(generator, mixins=None, doctype=None)[source]
dtd_end()[source]
dtd_start()[source]
handle_comment(contents)[source]

Receives the contents of a comment.

handle_pi(target, data)[source]

Receives the target and data of processing instructions.

make_new_element(elem_name, contentmodel)[source]
new_attribute(elem, a_name, a_type, a_decl, a_def)[source]

Receives the declaration of a new attribute.

new_element_type(elem_name, elem_cont)[source]

Receives the declaration of an element type.

new_external_entity(ent_name, pub_id, sys_id, ndata)[source]

Receives external general entity declarations. ‘ndata’ is the empty string if the entity is parsed.

new_external_pe(name, pubid, sysid)[source]

Receives external parameter entity declarations.

new_general_entity(name, val)[source]

Receives internal general entity declarations.

new_notation(name, pubid, sysid)[source]

Receives notation declarations.

new_parameter_entity(name, val)[source]

Receives internal parameter entity declarations.

pycopia.XML.DTD.get_dtd_compiler(fo, mixinmodule=None, doctype=None)[source]
pycopia.XML.DTD.get_identifier(uname)[source]

Table Of Contents

Previous topic

Pycopia audio package

Next topic

Pycopia WWW package