Files
sics/site_ansto/instrument/TEST_SICS/unit_tests/sics_hdb.py
2014-05-12 15:20:18 +10:00

288 lines
10 KiB
Python

#!/usr/bin/env python
# vim: ts=8 sts=4 sw=4 expandtab
# Author: Douglas Clowes (dcl@ansto.gov.au) 2014-04-24
#
""" This is a SICS hipadaba module.
It allows for loading the gumtree XML obtained from SICS and
accessing the tree by path to obtain the children and properties
of a node on a named path.
"""
#
# Try to import an ElementTree compatible XML library in the order
# of our preference (performance) but fall back to a default version.
#
try:
from lxml import etree
print "running with lxml.etree"
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print "running with cElementTree on Python 2.5+"
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print "running with ElementTree on Python 2.5+"
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print "running with cElementTree"
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print "running with ElementTree"
except ImportError:
print "Failed to import ElementTree from any known place"
class HipadabaTree(object):
"""Hipadaba Tree Class
This can be built from a variety of XML sources and contains
methods for accessing and navigating the SICS hipadaba tree
"""
def __init__(self, from_this):
"""Build a Hipadaba Tree object from an 'ElementTree' object
Parameters
----------
from_this : XML-like
ElementTree compatible object, or string or list of strings
"""
if isinstance(from_this, list) and isinstance(from_this[0], str):
my_root = etree.fromstringlist(from_this)
elif isinstance(from_this, str):
if len(from_this) > 0:
if from_this[0] == "<":
my_root = etree.fromstring(from_this)
elif from_this[0] == "@":
my_root = etree.parse(from_this[1:])
elif from_this.lower().endswith(".xml"):
my_root = etree.parse(from_this)
else:
raise Exception("unknown string")
else:
raise Exception("short string")
else:
directory = dir(from_this)
if "findall" not in directory:
raise Exception("findall not in object")
if "find" not in directory:
raise Exception("find not in object")
my_root = from_this
self.root = my_root
def short_tree(self, tree=None):
"""Creates a map from the tree/node that can be used or printed
Parameters
----------
tree : optional Element or ElementTree
Returns
-------
map containing name(str),
children(list of str),
properties(list of str)
"""
if tree is None:
tree = self.root
if "attrib" not in dir(tree) or 'id' not in tree.attrib:
tree_part = {"name": "<root>"}
else:
tree_part = {"name": tree.attrib['id']}
tree_part["children"] = sorted(
[comp.attrib['id'] for comp in tree.findall('component')])
tree_part["properties"] = sorted(
[propy.attrib['id'] for propy in tree.findall('property')])
return tree_part
def find_path(self, path, tree=None):
"""Finds a node from the given tree and path
Parameters
----------
path : str
tree : optional Element or ElementTree
Returns
-------
requested Element or None
"""
debug = False
if tree is None:
tree = self.root
if debug:
print "Looking for %s in %s" % (repr(path), self.short_tree(tree))
path_list = [propy
for propy in path.lower().strip('/').split('/')
if propy is not '']
if path_list == ['']:
if debug:
print "Finding root: %s in %s" % (
repr(path_list),
self.short_tree(tree))
return tree
child = tree
for node in path_list:
children = [comp
for comp in child.findall('component')
if comp.attrib['id'].lower() == node.lower()]
if len(children) == 0:
if debug:
print "Not found: %s in %s" % (
repr(path_list),
self.short_tree(tree))
return None
child = children[0]
if debug:
print "Finding node: %s in %s" % (
repr(path_list),
self.short_tree(child))
return child
def list_tree(self, tree=None, props=False, indent=0):
"""Returns a list of indented strings for a tree
Parameters
----------
tree : optional Element or ElementTree
props : optional boolean to include node properties
indent : optional nesting/indent depth/level
Returns
-------
list of str ready to print or write to file
"""
if tree is None:
tree = self.root
text = []
if "attrib" not in dir(tree) or 'id' not in tree.attrib:
text += [' '*indent + '* ' + '<root>']
else:
text += [' '*indent + '* ' + tree.attrib['id']]
if props:
properties = sorted(
tree.findall('property'),
key=lambda node: node.attrib['id'].lower())
for propy in properties:
line = ' '*indent + ' - ' + propy.attrib['id'] + '='
items = [valu.text
for valu in propy.findall('value')
if valu.text is not None]
line += ' '.join(items)
text += [line]
children = sorted(
tree.findall('component'),
key=lambda node: node.attrib['id'].lower())
for child in children:
text += self.list_tree(child, props, indent+1)
return text
def print_tree(self, tree=None, props=False, indent=0):
"""Print the indented tree to stdout
Parameters
----------
tree : optional Element or ElementTree
props : optional boolean to include node properties
indent : optional nesting/indent depth/level
Returns
-------
list of str ready to print or write to file
"""
line_list = self.list_tree(tree, props, indent)
for line in line_list:
print line
return line_list
def getNode(self, path, tree=None):
return self.find_path(path, tree)
def getValue(self, path, tree=None):
node = self.find_path(path, tree)
if node is None:
return None
items = [valu.text
for valu in node.findall('value')
if valu.text is not None]
if len(items) == 0:
return ""
return ','.join(items)
def getProperty(self, path, the_property, tree=None):
node = self.find_path(path, tree)
if node is None:
return None
items = [propy
for propy in node.findall('property')
if propy.attrib['id'].lower() == the_property.lower()]
if len(items) == 0:
return None
node = items[0]
items = [valu.text
for valu in node.findall('value')
if valu.text is not None]
if len(items) == 0:
return ""
return ','.join(items)
def getProperties(self, path, tree=None):
node = self.find_path(path, tree)
if node is not None:
return sorted(
[propy.attrib['id'] for propy in node.findall('property')])
return None
def getChildren(self, path, tree=None):
node = self.find_path(path, tree)
if node is not None:
return sorted(
[comp.attrib['id'] for comp in node.findall('component')])
return None
def main_program():
###this test uses a "junk.xml" file extracted for SICS
###
test_file = "junk.xml"
with open(test_file, "r") as input_file:
line_list = input_file.readlines()
print "From file:", HipadabaTree(test_file).short_tree()
print "From list:", HipadabaTree(line_list).short_tree()
print "From text:", HipadabaTree('\n'.join(line_list)).short_tree()
root = etree.parse("junk.xml")
hipadaba = HipadabaTree(root)
print "From tree:", hipadaba.short_tree()
print "Components:", [ch.attrib['id']
for ch in hipadaba.root.findall('component')]
print "Components:", [ch.attrib['id']
for ch in hipadaba.getNode('/sample/ps9').findall('component')]
print "GetNode:", hipadaba.short_tree(hipadaba.getNode('/sample/ps9'))
print "PrintTree:"
hipadaba.print_tree(
tree=hipadaba.getNode('/sample/ps9/'))
hipadaba.print_tree(
tree=hipadaba.getNode('/sample/ps9/status'), props=True)
print "GetChildren:", hipadaba.getChildren('')
print "GetChildren:", hipadaba.getChildren('////')
print "GetChildren:", hipadaba.getChildren('//sample///ps9/////')
print "GetProperties:", hipadaba.getProperties('/sample/ps9')
print "Properties from root:"
for property_name in hipadaba.getProperties('/sample/ps9'):
property_value = hipadaba.getProperty('/sample/ps9', property_name)
print ' ', property_name, '=', property_value
node = hipadaba.getNode('/sample/ps9')
print "Properties from node:"
for property_name in hipadaba.getProperties('/', node):
property_value = hipadaba.getProperty('/', property_name, node)
print ' ', property_name, '=', property_value
if __name__ == "__main__":
main_program()