Skip to content
Snippets Groups Projects
Commit bcc66b02 authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Added traversing capability to the Results object, added a new Exception class...

Added traversing capability to the Results object, added a new Exception class that is raised when a certain valid metainfo was not outputted by the parser.
parent 2beb276e
No related branches found
No related tags found
No related merge requests found
from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from enum import Enum
from operator import itemgetter
from builtins import range
from builtins import object
......@@ -11,7 +12,23 @@ from collections import defaultdict
logger = logging.getLogger(__name__)
#===============================================================================
class ParserEvent(Enum):
"""Enumerations for the different parser events when traversing the
results.
"""
open_section = 1
close_section = 2
add_value = 3
add_array_value = 4
class ParserKeyError(Exception):
"""A custom exception for all cases where a certain metainfo can not be
found in the results.
"""
pass
class DummyFile(object):
"""Mimics a file object by defininf a write interface, but actually does
not write anything. This allows one to used this object in functions which
......@@ -22,7 +39,6 @@ class DummyFile(object):
pass
#===============================================================================
class LocalBackend(object):
"""A backend that outputs results into a regular python dictionary. This is
useful if you wan't to run the parser with python only.
......@@ -266,14 +282,14 @@ class Results(object):
sectionmanager = datamanager.superSectionManager
open_sections = sectionmanager.openSections
result = []
for section in open_sections.values():
for section in open_sections:
data = section[metaname]
if data is not None:
result.append(data)
if len(result) == 1:
return result[0]
elif len(result) == 0:
raise LookupError("Could not find a parsing result for '{}'. The parser did not output this value.".format(metaname))
raise ParserKeyError("Could not find a parsing result for '{}'. The parser did not output this value.".format(metaname))
else:
return np.array(result)
......@@ -286,6 +302,33 @@ class Results(object):
if metainfo is None:
raise LookupError("The metainfo name '{}' does not exist in the metainfo environment. Check for typos or try updating the metainfo git package.".format(metaname))
def traverse(self):
"""A generator function for traversing the data in the parser results.
This generator returns a tuple of three item: the metainfo name, the
event type, and the event value.
"""
root = self._sectionmanagers["section_run"]
roots = {"section_run": []}
for section in root.openSections:
roots["section_run"].append(section)
for x in self.traverse_recursive(roots):
yield x
def traverse_recursive(self, section_map):
"""A generator function for traversing the data in the parser results.
"""
for name, sections in section_map.items():
for section in sections:
yield (name, ParserEvent.open_section, None)
for name, value in section.simpleValues.items():
yield (name, ParserEvent.add_value, value)
for name, value in section.arrayValues.items():
yield (name, ParserEvent.add_array_value, value)
for x in self.traverse_recursive(section.subSectionValues):
yield x
yield (name, ParserEvent.close_section, None)
def print_summary(self):
"""Return a string representing the data contained in the results. This
is a summary that can be used for debugging.
......@@ -378,8 +421,13 @@ class Section(object):
if res is not None:
return res
res = self.subSectionValues.get(metaName, None)
if res is not None:
return res
raise ParserKeyError(
"The metainfo '{}' could not be found in the section '{}'"
.format(metaName, self.name))
def addValue(self, metaInfo, value):
if self.backend.store:
vals = self.simpleValues.get(metaInfo.name, None)
......@@ -420,11 +468,11 @@ class Section(object):
class SectionManager(object):
"""Manages the sections for the given metainfo.
"""
def __init__(self, metaInfo, parentSectionNames, lastSectionGIndex=-1, openSections={}):
def __init__(self, metaInfo, parentSectionNames, lastSectionGIndex=-1):
self.metaInfo = metaInfo
self.parentSectionNames = parentSectionNames
self.lastSectionGIndex = lastSectionGIndex
self.openSections = dict(openSections)
self.openSections = []
def openSection(self, backend):
newGIndex = self.lastSectionGIndex + 1
......@@ -455,7 +503,10 @@ class SectionManager(object):
if not parent_found and len(self.parentSectionNames) != 0:
raise LookupError("Could not open section '{}' because none of it's parent sections '{}' could not be found".format(self.metaInfo.name, self.parentSectionNames))
self.openSections[gIndex] = Section(gIndex, references, parents, self.metaInfo.name, backend)
new_section = Section(gIndex, references, parents, self.metaInfo.name, backend)
self.openSections.append(new_section)
if parent_found:
parents[0].addSubsection(self.metaInfo, new_section)
def closeSection(self, backend, gIndex):
pass
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment