Package flumotion :: Package common :: Module fxml :: Class Parser
[hide private]

Class Parser

source code

extern.log.log.Loggable --+
                          |
                         Parser
Known Subclasses:

XML parser base class.

I add some helper functions for specialized XML parsers, mostly the parseFromTable method.

I am here so that the config parser and the registry parser can share code.

Nested Classes [hide private]
  parserError
Error during parsing of XML.
Instance Methods [hide private]
 
getRoot(self, file)
Return the root of the XML tree for the the string or filename passed as an argument.
source code
 
checkAttributes(self, node, required=None, optional=None)
Checks that a given XML node has all of the required attributes, and no unknown attributes.
source code
tuple of string or None, as long as the combined length of the required and optional attributes.
parseAttributes(self, node, required=None, optional=None, type=<type 'str'>)
Checks the validity of the attributes on an XML node, via Parser.checkAttributes, then parses them out and returns them all as a tuple.
source code
 
parseFromTable(self, parent, parsers)
A data-driven verifying XML parser.
source code
 
parseTextNode(self, node, type=<type 'str'>)
Parse a text-containing XML node.
source code

Inherited from extern.log.log.Loggable: __providedBy__, debug, doLog, error, info, log, logFunction, logObjectName, warning, warningFailure, writeMarker

Class Variables [hide private]

Inherited from extern.log.log.Loggable: __implemented__, __provides__, logCategory

Method Details [hide private]

getRoot(self, file)

source code 

Return the root of the XML tree for the the string or filename passed as an argument. Raises fxml.ParserError if the XML could not be parsed.

Parameters:
  • file (File object; can be a duck file like StringIO. Alternately, the path of a file on disk.) - An open file object, or the name of a file. Note that if you pass a file object, this function will leave the file open.

checkAttributes(self, node, required=None, optional=None)

source code 

Checks that a given XML node has all of the required attributes, and no unknown attributes. Raises fxml.ParserError if unknown or missing attributes are detected. An empty attribute (e.g. 'foo=""') is treated as a missing attribute.

Parameters:
  • node (xml.dom.Node) - An XML DOM node.
  • required (Sequence (list, tuple, ...) of strings.) - Set of required attributes, or None.
  • optional (Sequence (list, tuple, ...) of strings.) - Set of optional attributes, or None.

parseAttributes(self, node, required=None, optional=None, type=<type 'str'>)

source code 

Checks the validity of the attributes on an XML node, via Parser.checkAttributes, then parses them out and returns them all as a tuple.

Parameters:
  • node (xml.dom.Node) - An XML DOM node.
  • required (Sequence (list, tuple, ...) of strings.) - Set of required attributes, or None.
  • optional (Sequence (list, tuple, ...) of strings.) - Set of optional attributes, or None.
  • type (Function of type object -> object.) - Type to which to cast attribute values. The original values will always be unicode objects; in most cases you want `str' objects, so this defaults to `str'.
Returns: tuple of string or None, as long as the combined length of the required and optional attributes.
List of all attributes as a tuple. The first element of the returned tuple will be the value of the first required attribute, the second the value of the second required attribute, and so on. The optional attributes follow, with None as the value if the optional attribute was not present.

parseFromTable(self, parent, parsers)

source code 

A data-driven verifying XML parser. Raises fxml.ParserError if an unexpected child node is encountered.

Parameters:
  • parent (xml.dom.Node) - An XML node whose child nodes you are interested in parsing.
  • parsers (dict of string -> (function, function)) - A parse table defining how to parse the child nodes. The keys are the possible child nodes, and the value is a two-tuple of how to parse them consisting of a parser and a value handler. The parser is a one-argument function that will be called with the child node as an argument, and the handler is a one-argument function that will be called with the result of calling the parser.

parseTextNode(self, node, type=<type 'str'>)

source code 

Parse a text-containing XML node.

The node is expected to contain only text children. Recognized node types are xml.dom.Node.TEXT_NODE and xml.dom.Node.CDATA_SECTION_NODE.

Parameters:
  • node (xml.dom.Node) - the node to parse
  • type (function of type unicode -> object) - a function to call on the resulting text
Returns:
The result of calling type on the unicode text. By default, type is str.