| | |
- Node
- NodeGenerator
-
- StaticNodeGenerator
- unittest.TestCase(__builtin__.object)
-
- TestXMLData
class Node |
| |
Node: A class representing a node in an XML DOM tree.
This is a lightweight XML representation -- I didn't want to use
the full, foot-crushing power of xml.dom (from the standard Python
library). Each Node represents an XML tag (or tree of tags). There
are no separate objects for documents, attributes, data, etc.
It's just tags, with stuff in them.
Node(name, attrs=None, namespace=None, parent=None, children=None,
data=None) -- constructor.
*name* and *namespace* are what they say; these must be strings
(or unicode). If *attrs* (a dict) is provided, the new node is
given those attributes. If the *parent* is provided, the new node
is appended to its contents. If the *children* (a list) are
provided, they are appended to the new node's contents. If the
*data* is provided (a string or unicode object, or something
convertible to unicode), it is appended as well.
A Node's namespace is assumed to be inherited from its parent, if
you provide none. (This means that the value of nod.getnamespace()
can change after you add it as a child to some other Node.)
(You can also provide a namespace by putting an 'xmlns' key in
*attrs*. This is equivalent to providing a *namespace*.)
A Node can only have one parent. If you try to add a Node somewhere
as a child, and it already has a parent, you will get a ValueError.
(If you call the node's remove() method, it will become parentless,
and you can then give it a new parent.)
Don't do crazy stuff like setting a Node to be its own grandpa.
Class methods:
escapetext() -- escape <, >, ", and & symbols in a string.
parse() -- parse a string into a Node tree.
Public methods:
delete() -- destroy a Node and its children.
copy() -- return a (deep) copy of a Node.
getname() -- return the Node's tag name.
getnamespace() -- return the Node's namespace.
setnamespace() -- set (or clear) the Node's namespace.
getcontents() -- return the Node's list of Nodes and data.
addchild() -- add a Node as a child of this Node.
removechild() -- remove a Node as a child of this Node.
remove() -- remove this Node from its parent, if any.
getparent() -- return the Node's parent.
setparent() -- add this Node as a child of some other Node.
getchild() -- return a child of the Node, according to criteria.
getchildren() -- return some children of the Node, according to criteria.
getdata() -- return all the data in the Node.
setdata() -- change the data in the Node.
adddata() -- append data to the Node.
cleardata() -- clear all data from the Node.
getattrs() -- return all attributes in the Node.
setattrs() -- set several attributes in the Node.
clearattrs() -- clear all attributes from the Node.
getattr() -- return one attribute in the Node.
getattrdef() -- return one attribute in the Node.
hasattr() -- test if the Node has a given attribute.
setattr() -- set an attribute in the Node.
clearattr() -- clear an attribute from the Node.
setchild() -- find or create a child, according to criteria.
getchilddata() -- return the data from one child of the Node.
setchilddata() -- find or create a child, according to criteria, and
set its data.
addchilddata() -- find or create a child, according to criteria, and
append to its data.
getchildattr() -- return an attribute from one child of the Node.
getchildattrdef() -- return an attribute from one child of the Node.
setchildattr() -- find or create a child, according to criteria, and
set an attribute in it.
serialize() -- create a string representing the Node as XML. |
| |
Methods defined here:
- __init__(self, name, attrs=None, namespace=None, parent=None, children=None, data=None)
- __str__(self)
- __unicode__(self)
- addchild(self, nod)
- addchild(node) -> None
Add *node* as a child of this Node. The *node* must not already
have a parent. If the Node already has child nodes or data, the new
child is placed at the end.
- addchilddata(self, name, data='', attrs=None, namespace=None)
- addchilddata(name, data='', attrs=None, namespace=None) -> Node
Find or create a child, according to criteria, and append to its
data.
This finds the first child node whose name is *name*. If there is
no such node, it creates one. It then appends *data* to the child's
character data. Finally, it returns the node that was found or
created.
You can supply additional conditions for which children to
consider:
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
If a new node is created, these values are applied to it.
- adddata(self, data)
- adddata(data) -> None
Append data to the Node. (After any child nodes, if there are any.)
The *data* must be str, unicode, or convertible to unicode. (Or
None, which is considered equivalent to ''.)
- clearattr(self, key)
- clearattr(key) -> None
Clear an attribute from the Node.
If *key* is 'xmlns', this clears the namespace.
- clearattrs(self)
- clearattrs() -> None
Clear all attributes from the Node. (Except namespace; this method
does not affect that.)
- cleardata(self)
- cleardata() -> None
Clear all data from the Node. Previously existing data elements
are removed.
- copy(self)
- copy() -> Node
Return a (deep) copy of a Node.
The copy will not have a parent, even if this Node does. (But it
will copy an inherited namespace as an explicit one.)
- delete(self)
- delete() -> None
Destroy a Node and its children. You don't have to do this, but it
gets rid of the object without relying on garbage collection.
- getattr(self, key, default=None)
- getattr(key, default=None) -> str/unicode
Return one attribute in the Node. If the attribute is not present,
returns None (or *default*).
If *key* is 'xmlns', returns the namespace (which may be inherited).
- getattrdef(self, key, *default)
- getattrdef(key [, default] ) -> str/unicode
Return one attribute in the Node. If the attribute is not present,
raises KeyError (unless *default* has been provided, in which case
it returns that.).
If *key* is 'xmlns', returns the namespace (which may be inherited).
- getattrs(self)
- getattrs() -> dict
Return all attributes in the Node. You should not modify the
returned dict.
The dict will not contain 'xmlns'. Call getnamespace() to see the
Node's namespace.
- getchild(self, name=None, attrs=None, namespace=None)
- getchild(name=None, attrs=None, namespace=None) -> Node
Return a child of the Node, according to criteria.
If no parameters are supplied, this returns the first of the Node's
child nodes. The parameters put conditions on the children which
are considered:
name=string: Only children whose tag names match.
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
If there are no matches, None is returned.
- getchildattr(self, key, default=None, name=None, attrs=None, namespace=None)
- getchildattr(key, default=None, name=None, attrs=None,
namespace=None) -> str/unicode
Return an attribute from one child of the Node.
This finds the first child node, and returns the value of its
*key* attribute. (If there is no such attribute, this returns
None, or *default*. If there are no children, same thing.)
You can supply additional conditions for which children to
consider:
name=string: Only children whose tag names match.
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
- getchildattrdef(self, key, default=<class exceptions.KeyError at 0x20b40>, name=None, attrs=None, namespace=None)
- getchildattrdef(key, default=KeyError, name=None, attrs=None,
namespace=None) -> str/unicode
Return an attribute from one child of the Node.
This finds the first child node, and returns the value of its
*key* attribute. (If there is no such attribute, this raises
KeyError -- unless *default* is supplied, in which case it
returns that. If there are no children, same thing.)
You can supply additional conditions for which children to
consider:
name=string: Only children whose tag names match.
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
- getchilddata(self, name=None, attrs=None, namespace=None)
- getchilddata(name=None, attrs=None, namespace=None) -> str/unicode
Return the data from one child of the Node.
This finds the first child node, and returns all its data.
(If there are no children, it returns None.) If the child has
multiple data elements, they are concatenated, ignoring the Nodes
between them.
You can supply additional conditions for which children to
consider:
name=string: Only children whose tag names match.
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
- getchildren(self, name=None, attrs=None, namespace=None, first=False)
- getchildren(name=None, attrs=None, namespace=None, first=False)
-> list
Return some children of the Node, according to criteria.
If no parameters are supplied, this returns all the Node's child
nodes, as a list. The parameters filter the list:
name=string: Only children whose tag names match.
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
If *first* is True, the first matching child is returned, instead
of a list of all matches. If there are no matches, None is returned.
- getcontents(self)
- getcontents() -> list
Return the Node's list of Nodes and data. Data elements are
represented as string/unicode objects. They are concatenated if
possible -- you will not see two data elements in a row.
- getdata(self)
- getdata() -> str/unicode
Return all the data in the Node. If there are multiple elements,
they are concatenated, ignoring the child Nodes between them.
- getname(self)
- getname() -> str/unicode
Return the Node's tag name.
- getnamespace(self, default=None)
- getnamespace(default=None) -> ns
Return the Node's namespace. If the Node has no namespace set for
itself, this returns its parent's namespace, if it has a parent.
If not, it returns None (or *default*).
- getparent(self)
- getparent() -> Node
Return the Node's parent. If it had none, returns None.
- hasattr(self, key)
- hasattr(key) -> bool
Test if the Node has a given attribute.
If *key* is 'xmlns', returns whether the Node has a namespace.
(Inherited namespaces count.)
- remove(self, preservenamespace=False)
- remove(preservenamespace=False) -> None
Remove this Node from its parent, if any. (If it had no parent,
this method does nothing.)
If *preservenamespace* is True, and the node was inheriting its
namespace from its parent, then it will get that namespace
explicitly set. (So calling getnamespace() on it after removal
will return the same value as before.)
- removechild(self, nod, preservenamespace=False)
- removechild(node, preservenamespace=False) -> Node
Remove *node* as a child of this Node. Returns *node*.
If *preservenamespace* is True, and *node* was inheriting its
namespace from its parent, then *node* will get that namespace
explicitly set. (So calling getnamespace() on it after removal
will return the same value as before.)
- serialize(self, pretty=False, depth=0, namespace=None)
- serialize(pretty=False) -> str/unicode
Create a string representing the Node as XML. The result will
be str if possible, otherwise unicode. (In practice, usually
unicode.)
If *pretty* is True, the string will contain pretty indentation and
line breaks. (It will not end with a linebreak.) Pretty mode also
skips printing whitespace between tags.
- setattr(self, key, data)
- setattr(key, data) -> None
Set an attribute in the Node. The *data* must be str, unicode, or
convertible to unicode. If *data* is None (or ''), this removes
the attribute instead (if present).
If *key* is 'xmlns', this sets or clears the namespace.
- setattrs(self, arg_=None, **dic)
- setattrs(val=None, **dic) -> None
Set (or clear) several attributes in the Node. You get several
options for setting this up:
setattrs( fish='trout', fowl='dove' )
setattrs( {'fish':'trout', 'fowl':'dove'} )
setattrs( [ ('fish','trout'), ('fowl','dove') ] )
setattrs(val) # *val* is anything which can be cast to a dict
Any key whose value is None (or '') causes the removal of that
attribute (if present).
The key 'xmlns' sets or clears the Node's namespace.
- setchild(self, name, attrs=None, namespace=None)
- setchild(name, attrs=None, namespace=None) -> Node
Find or create a child, according to criteria.
This finds the first child node whose name is *name*. If there is
no such node, it creates one. It then returns the node that was
found or created.
You can supply additional conditions for which children to
consider:
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
If a new node is created, these values are applied to it.
- setchildattr(self, name, key, data, attrs=None, namespace=None)
- setchildattr(name, key, data, attrs=None, namespace=None) -> Node
This finds the first child node whose name is *name*. If there is
no such node, it creates one. It then sets the child's *key*
attribute to *data*. (If *data* is None or '', it instead
removes the *key* attribute, if present.) Finally, it returns
the node that was found or created.
You can supply additional conditions for which children to
consider:
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
If a new node is created, these values are applied to it.
- setchilddata(self, name, data='', attrs=None, namespace=None)
- setchilddata(name, data='', attrs=None, namespace=None) -> Node
Find or create a child, according to criteria, and set its data.
This finds the first child node whose name is *name*. If there is
no such node, it creates one. It then sets the child's character
data to *data*. Finally, it returns the node that was found or
created.
You can supply additional conditions for which children to
consider:
attrs=dict: Only children who match each of the given attributes.
namespace=string: Only children whose namespace matches.
(Inherited namespaces count.)
(You can also match the namespace by putting an 'xmlns' key in
*attrs*.)
If a new node is created, these values are applied to it.
- setdata(self, data)
- setdata(data) -> None
Change the data in the Node. Previously existing data elements
are removed, and the new data is added. (After any child nodes,
if there are any.)
The *data* must be str, unicode, or convertible to unicode. (Or
None, which is considered equivalent to ''.)
- setnamespace(self, ns)
- setnamespace(ns) -> None
Set (or clear) the Node's namespace. If *ns* is a string (or unicode),
this sets the namespace. If *ns* is None, it clears it.
If a Node's namespace is unset, it inherits the namespace of its
parent (if it has a parent). Therefore, calling setnamespace(None)
does not imply that getnamespace() will return None.
- setparent(self, nod)
- setparent(node) -> None
Add this Node as a child of *node*. This node must not already
have a parent. If *node* already has child nodes or data, the new
child is placed at the end.
Static methods defined here:
- escapetext(st)
- escapetext(str) -> str
Escape <, >, ", and & symbols in a string or unicode object.
- parse(data, encoding='UTF-8')
- parse(data, encoding='UTF-8') -> Node
Parse a string into a Node tree. If the string is not well-formed
XML, this raises xml.parsers.expat.ExpatError.
By default, *encoding* is UTF-8, and so the *data* must be a string
in that encoding. You can supply other encodings. If *encoding* is
None, the *data* should be a unicode object (or convertible to
unicode).
|
class NodeGenerator |
| |
NodeGenerator: A base class for utilities which parse strings into
Node trees.
This class is not useful on its own. See StaticNodeGenerator for a
functional use of it.
NodeGenerator(encoding='UTF-8') -- constructor.
By default, *encoding* is UTF-8, and so the data must be a string
in that encoding. You can supply other encodings. If *encoding* is
None, the data should be unicode objects (or convertible to
unicode).
Public method:
close() -- shut down the NodeGenerator.
Internal methods:
handle_startnamespace() -- expat parser callback.
handle_startelement() -- expat parser callback.
handle_endelement() -- expat parser callback.
handle_data() -- expat parser callback. |
| |
Methods defined here:
- __init__(self, encoding='UTF-8')
- close(self)
- close() -> None
Shut down the NodeGenerator. You don't have to do this, but it
gets rid of the object without relying on garbage collection.
- handle_data(self, data)
- handle_data() -- expat parser callback. Do not call.
- handle_endelement(self, name)
- handle_endelement() -- expat parser callback. Do not call.
- handle_startelement(self, name, attrs)
- handle_startelement() -- expat parser callback. Do not call.
- handle_startnamespace(self, prefix, uri)
- handle_startnamespace() -- expat parser callback. Do not call.
|
class StaticNodeGenerator(NodeGenerator) |
| |
StaticNodeGenerator: A NodeGenerator which parses a single string.
StaticNodeGenerator(data, encoding='UTF-8') -- constructor.
By default, *encoding* is UTF-8, and so the *data* must be a string
in that encoding. You can supply other encodings. If *encoding* is
None, the *data* should be a unicode object (or convertible to
unicode).
Public method:
get() -- return the Node tree. |
| |
Methods defined here:
- __init__(self, data, encoding='UTF-8')
- get(self)
- get() -> Node
Return the Node tree. If the data passed to the constructor was
not well-formed, this raises xml.parsers.expat.ExpatError.
Methods inherited from NodeGenerator:
- close(self)
- close() -> None
Shut down the NodeGenerator. You don't have to do this, but it
gets rid of the object without relying on garbage collection.
- handle_data(self, data)
- handle_data() -- expat parser callback. Do not call.
- handle_endelement(self, name)
- handle_endelement() -- expat parser callback. Do not call.
- handle_startelement(self, name, attrs)
- handle_startelement() -- expat parser callback. Do not call.
- handle_startnamespace(self, prefix, uri)
- handle_startnamespace() -- expat parser callback. Do not call.
|
class TestXMLData(unittest.TestCase) |
| |
Unit tests for the xmldata module. |
| |
- Method resolution order:
- TestXMLData
- unittest.TestCase
- __builtin__.object
Methods defined here:
- test_attrs(self)
- test_child(self)
- test_contents(self)
- test_copy(self)
- test_data(self)
- test_getchildren(self)
- test_namespace(self)
- test_namespaceattr(self)
- test_parse(self)
- test_parsenamespaces(self)
- test_serialize(self)
- test_setchild(self)
Methods inherited from unittest.TestCase:
- __call__(self, result=None)
- __init__(self, methodName='runTest')
- Create an instance of the class that will use the named test
method when executed. Raises a ValueError if the instance does
not have a method with the specified name.
- __repr__(self)
- __str__(self)
- assertAlmostEqual = failUnlessAlmostEqual(self, first, second, places=7, msg=None)
- Fail if the two objects are unequal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
Note that decimal places (from zero) is usually not the same
as significant digits (measured from the most signficant digit).
- assertAlmostEquals = failUnlessAlmostEqual(self, first, second, places=7, msg=None)
- Fail if the two objects are unequal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
Note that decimal places (from zero) is usually not the same
as significant digits (measured from the most signficant digit).
- assertEqual = failUnlessEqual(self, first, second, msg=None)
- Fail if the two objects are unequal as determined by the '=='
operator.
- assertEquals = failUnlessEqual(self, first, second, msg=None)
- Fail if the two objects are unequal as determined by the '=='
operator.
- assertNotAlmostEqual = failIfAlmostEqual(self, first, second, places=7, msg=None)
- Fail if the two objects are equal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
Note that decimal places (from zero) is usually not the same
as significant digits (measured from the most signficant digit).
- assertNotAlmostEquals = failIfAlmostEqual(self, first, second, places=7, msg=None)
- Fail if the two objects are equal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
Note that decimal places (from zero) is usually not the same
as significant digits (measured from the most signficant digit).
- assertNotEqual = failIfEqual(self, first, second, msg=None)
- Fail if the two objects are equal as determined by the '=='
operator.
- assertNotEquals = failIfEqual(self, first, second, msg=None)
- Fail if the two objects are equal as determined by the '=='
operator.
- assertRaises = failUnlessRaises(self, excClass, callableObj, *args, **kwargs)
- Fail unless an exception of class excClass is thrown
by callableObj when invoked with arguments args and keyword
arguments kwargs. If a different type of exception is
thrown, it will not be caught, and the test case will be
deemed to have suffered an error, exactly as for an
unexpected exception.
- assert_ = failUnless(self, expr, msg=None)
- Fail the test unless the expression is true.
- countTestCases(self)
- debug(self)
- Run the test without collecting errors in a TestResult
- defaultTestResult(self)
- fail(self, msg=None)
- Fail immediately, with the given message.
- failIf(self, expr, msg=None)
- Fail the test if the expression is true.
- failIfAlmostEqual(self, first, second, places=7, msg=None)
- Fail if the two objects are equal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
Note that decimal places (from zero) is usually not the same
as significant digits (measured from the most signficant digit).
- failIfEqual(self, first, second, msg=None)
- Fail if the two objects are equal as determined by the '=='
operator.
- failUnless(self, expr, msg=None)
- Fail the test unless the expression is true.
- failUnlessAlmostEqual(self, first, second, places=7, msg=None)
- Fail if the two objects are unequal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
Note that decimal places (from zero) is usually not the same
as significant digits (measured from the most signficant digit).
- failUnlessEqual(self, first, second, msg=None)
- Fail if the two objects are unequal as determined by the '=='
operator.
- failUnlessRaises(self, excClass, callableObj, *args, **kwargs)
- Fail unless an exception of class excClass is thrown
by callableObj when invoked with arguments args and keyword
arguments kwargs. If a different type of exception is
thrown, it will not be caught, and the test case will be
deemed to have suffered an error, exactly as for an
unexpected exception.
- id(self)
- run(self, result=None)
- setUp(self)
- Hook method for setting up the test fixture before exercising it.
- shortDescription(self)
- Returns a one-line description of the test, or None if no
description has been provided.
The default implementation of this method returns the first line of
the specified test method's docstring.
- tearDown(self)
- Hook method for deconstructing the test fixture after testing it.
Data and other attributes inherited from unittest.TestCase:
- __dict__ = <dictproxy object at 0x6a6250>
- dictionary for instance variables (if defined)
- __weakref__ = <attribute '__weakref__' of 'TestCase' objects>
- list of weak references to the object (if defined)
- failureException = <class exceptions.AssertionError at 0x20a80>
- Assertion failed.
| |