Fork me on GitHub

pyhrf.xmliobak.xmlbase module

class pyhrf.xmliobak.xmlbase.FuncWrapper(func, params=None)
class pyhrf.xmliobak.xmlbase.TypedXMLHandler(write_callback=None)

Class handling the xml format with the following generic document structure:

<root>
    <tagName 'type'=tagType>
        tagContent
    </tagName>
</root>

The root tag is mandatory, so is the ‘type’ attribute for every other tag. This class can parse an xml string and build a dictionary of python objects according to each tag (see parseXMLString()). Conversely, it can build the xml string corresponding to a list or dictionary of objects ( see to_xml()). This class is based on the xml.dom python module and relies on the DOM structure to parse XML. XML input/output is handled via a mapping between a type attribute of a tag and a static handling function. This class handles the following basic python types: string, int, float, array.array, list, dict. One can add other type-specific read or write handlers with the functions addDOMTagReader() and addDOMWriter().

Reading XML:

  • specific handlers are added with method addDOMTagReader(stype, myReadHandler) which maps the function myReadHandler to the string stype.

  • a tag reading handler must have the following prototype:

    myReadHandler(domTreeWalker):
        # interpret and process tag content
        # return built python object
    

    , where domTreeWalker is an instance of the _xmlplus.dom.TreeWalker.TreeWalker class.

  • useful things to use the domTreeWalker and implement a handler:

    • node = domTreeWalker.currentNode -> current node in the tree parsing, of class Node, corresponding to the current tag.
    • node.getAttribute(‘my attribute’) -> return the string corresponding to ‘my attribute’ in the tag definition
    • node.childNodes[0].data -> the tag content data (string type), to be parse and build the python object from
    • node.tagName -> the name of the tag
    • node.parentNode -> the parent tag node

Writing XML:

  • handlers are added with method addDOMTagWriter(pythonType, myWriteHandler), where pythonType is of python type ‘type’ and myWriteHandler a function.

  • a tag writing handler must have the following prototype:

    myWriteHandler(domDocument, node, pyObj):
    

    where:

    • domDocument is overall encapsulating structure (_xmlplus.dom.Document instance)
    • node (Node instance) is the current tag node to append data to
    • pyObj is the python object to convert into a ‘human-readable’ string.
  • useful things to write handlers :

ATTRIBUTE_LABEL_META = 'meta'
ATTRIBUTE_LABEL_PYTHON_CLASS = 'pythonClass'
ATTRIBUTE_LABEL_PYTHON_CLASS_INIT_MODE = 'pythonInitMode'
ATTRIBUTE_LABEL_PYTHON_FUNCTION = 'pythonFunction'
ATTRIBUTE_LABEL_PYTHON_MODULE = 'pythonModule'
ATTRIBUTE_LABEL_TYPE = 'type'
TYPE_LABEL_ARRAY = 'array'
TYPE_LABEL_BOOL = 'bool'
TYPE_LABEL_DICT = 'struct'
TYPE_LABEL_FLOAT = 'double'
TYPE_LABEL_INT = 'int'
TYPE_LABEL_KEY_VAL_PAIR = 'dictItem'
TYPE_LABEL_LIST = 'list'
TYPE_LABEL_NONE = 'none'
TYPE_LABEL_ODICT = 'ordered_struct'
TYPE_LABEL_STRING = 'char'
TYPE_LABEL_TUPLE = 'tuple'
TYPE_LABEL_XML_INCLUDE = 'include'
static arrayDOMWriter(node, arrayObj, xmlHandler)
static arrayTagDOMReader(xmlHandler)
static boolDOMWriter(node, boolObj, xmlHandler)
static boolTagDOMReader(xmlHandler)
buildXMLString(obj, label=None, pretty=False)
createDocument()
static dictDOMWriter(node, dictObj, xmlHandler, atype=None)
static dictTagDOMReader(xmlHandler, init_class=None)
static floatDOMWriter(node, floatObj, xmlHandler)
static floatTagDOMReader(xmlHandler)
static includeTagDOMReader(xmlHandler)
inspect_and_append_to_DOM_tree(doc, node, obj)
inspectable(obj)
static intDOMWriter(node, intObj, xmlHandler)
static intTagDOMReader(xmlHandler)
static listDOMWriter(node, listObj, xmlHandler)
static listTagDOMReader(xmlHandler)
mountDefaultHandlers()
static noneDOMWriter(node, noneObj, xmlHandler)
static noneTagDOMReader(xmlHandler)
static odictDOMWriter(node, dictObj, xmlHandler)
static odictTagDOMReader(xmlHandler)
packHandlers()
parseXMLString(xml)
readDOMData(walker)
rootTagDOMReader(walker)
static stringDOMWriter(node, stringObj, xmlHandler)
static stringTagDOMReader(xmlHandler)
static tupleDOMWriter(node, tupleObj, xmlHandler)
static tupleTagDOMReader(xmlHandler)
writeDOMData(doc, node, obj, label, comment=None, meta=None)
class pyhrf.xmliobak.xmlbase.XMLParamDrivenClass(parameters=None, xmlHandler=<pyhrf.xmliobak.xmlbase.TypedXMLHandler instance>, xmlLabel=None, xmlComment=None)

Base “abstract” class to handle parameters with clear specification and default values. Recursive aggregation is availaible to handle aggregated variables which also require parameter specifications.

appendParametersToDOMTree(doc, node)
defaultParameters = {}
fetchDefaultParameters()
parametersComments = {}
parametersMeta = {}
parametersToXml(tagName=None, pretty=False)
updateParameters(newp)
class pyhrf.xmliobak.xmlbase.XMLParamDrivenClassInitException
class pyhrf.xmliobak.xmlbase.XMLable(**kwargs)
get_parameters_comments()
get_parameters_meta()
get_parameters_to_show()
override_init(param_name, init_obj, init_params=None)
override_value(param_name, value)
set_init(init_func, init_params=None)
class pyhrf.xmliobak.xmlbase.XMLable2

Bases: object

check_init_func(params=None)
get_parameters_comments()
get_parameters_meta()
get_parameters_to_show()
override_param_init(init_func, **params)

TODO (if needed)

set_init(init_func, **init_params)
set_init_param(param_name, param_value)
pyhrf.xmliobak.xmlbase.from_xml(s, handler=<pyhrf.xmliobak.xmlbase.TypedXMLHandler instance>)
pyhrf.xmliobak.xmlbase.getargspec(func)
pyhrf.xmliobak.xmlbase.match_init_args(c, argsDict)
pyhrf.xmliobak.xmlbase.read_xml(fn)
pyhrf.xmliobak.xmlbase.to_xml(o, handler=<pyhrf.xmliobak.xmlbase.TypedXMLHandler instance>, objName='anonymObject', pretty=False)
pyhrf.xmliobak.xmlbase.write_xml(obj, fn)