Added DataLibrary from_xml function

This commit is contained in:
Adam Nelson 2016-11-07 19:01:35 -05:00
parent 11040fb5f9
commit fb512fb1ef

View file

@ -22,6 +22,12 @@ class DataLibrary(EqualityMixin):
def __init__(self):
self.libraries = []
def __getitem__(self, material):
for library in self.libraries:
if material in library['materials']:
return library
return None
def register_file(self, filename):
"""Register a file with the data library.
@ -78,3 +84,33 @@ class DataLibrary(EqualityMixin):
tree = ET.ElementTree(root)
tree.write(path, xml_declaration=True, encoding='utf-8',
method='xml')
@classmethod
def from_xml(cls, path):
"""Read cross section data library from an XML file.
Parameters
----------
path : str
Path to XML file to read.
"""
data = cls()
tree = ET.parse(path)
root = tree.getroot()
if root.find('directory') is not None:
directory = root.find('directory').text
else:
directory = os.path.dirname(path)
for lib_element in root.findall('library'):
filename = os.path.join(directory, lib_element.attrib['path'])
filetype = lib_element.attrib['type']
materials = lib_element.attrib['materials'].split()
library = {'path': filename, 'type': filetype,
'materials': materials}
data.libraries.append(library)
return data