Skip to content
Snippets Groups Projects
Commit ee21d45a authored by Himanen, Lauri (himanel1)'s avatar Himanen, Lauri (himanel1)
Browse files

Added a way to access any opened section by index in the local backend.

parent f7e8cfa1
No related branches found
No related tags found
No related merge requests found
......@@ -16,9 +16,9 @@ class LocalBackend(object):
self.__openSections = set()
self.__lastIndex = {}
self.stats = {}
self.results = Results(metaInfoEnv)
self.dataManagers = {}
self.sectionManagers = {}
self.results = Results(metaInfoEnv, self.dataManagers, self.sectionManagers)
self.debug = debug
for ikNames, ik in metaInfoEnv.infoKinds.items():
......@@ -170,54 +170,7 @@ class LocalBackend(object):
def finishedParsingSession(self, parsingStatus, parsingErrors, mainFileUri=None, parserInfo=None):
"""Called when the parsing finishes.
"""
# If the metainfo is uniquely determined by the metainfo name, a new
# key is added to the dictionary. A list of the clashes in metanames is
# also gathered so that an informative prompt may be shown if an
# ambiguous metaname is searched.
results = self.results._results
shortnames = self.results._shortnames
clash_dict = self.results._clash_dict
# Gather the clashing names
for key in results.iterkeys():
metaname = key.rsplit("/", 1)[-1]
clash_dict[metaname].append(key)
# Insert the non-clashing minimal keys to the result dictionary
for short, longs in clash_dict.iteritems():
n_longs = len(longs)
if n_longs == 1:
long_path = longs[0]
shortnames[short] = results[long_path]
#===============================================================================
class Result(object):
"""Represents a parsed result.
During parsing the results will be placed into the python lists _values,
_array_values, _real_values. After the parsing is finished, each of these
lists will be tranformed into a numpy array.
Attributes:
_values: The concrete value/values that was parsed
"""
def __init__(self):
self.values = []
@property
def value(self):
"""Returns the concrete values that belong to this result. One result
object should only contain one of the three different values: values,
realValues or arrayValues. The one that is found is returned.
"""
return self.values
@property
def unit(self):
"""
If the value has units, return them as plain string.
"""
pass
#===============================================================================
......@@ -229,7 +182,9 @@ class Results(object):
Attributes:
"""
def __init__(self, metaInfoEnv):
def __init__(self, metaInfoEnv, datamanagers, sectionmanagers):
self._datamanagers = datamanagers
self._sectionmanagers = sectionmanagers
self._results = defaultdict(list)
self._shortnames = defaultdict(list)
self._clash_dict = defaultdict(list)
......@@ -243,24 +198,28 @@ class Results(object):
"""
self.test_validity(metaname)
# Check if the metaname exists and if it clashes
paths = self._clash_dict.get(metaname)
if paths is None:
raise LookupError("The metaname '{}' does not exist in the parser results.".format(metaname))
elif len(paths) != 1:
message = "The result for metaname '{}' is ambiguous. Use the full path. The following paths lead to the same metainfo:".format(metaname)
for path in paths:
message += " " + path
raise LookupError(message)
# Provide a simplified object if only one entry found
value = self._results.get(metaname)
if value is None:
value = self._shortnames.get(metaname)
if len(value) == 1:
return value[0]
else:
return np.array(value)
# See if in sections
sectionmanager = self._sectionmanagers.get(metaname)
if sectionmanager is not None:
return sectionmanager.openSections
# See if in data
datamanager = self._datamanagers.get(metaname)
if datamanager is not None:
sectionmanager = datamanager.superSectionManager
open_sections = sectionmanager.openSections
result = []
for section in open_sections.itervalues():
data_list = section[metaname]
if data_list is not None:
if len(data_list) == 1:
result.append(data_list[0])
else:
result.append(data_list)
if len(result) == 1:
return result[0]
else:
return np.array(result)
def get_path(self, metaname):
"""Returns the full path of the given metaname. If multiple options are
......@@ -320,6 +279,8 @@ class Section(object):
res = self.arrayValues.get(metaName, None)
if res:
return res
res = self.subSectionValues.get(metaName, None)
return res
def get_paths(self, path, results):
path.append(self.name)
......@@ -363,34 +324,6 @@ class Section(object):
else:
vals.append(section)
def add_results(self):
"""When the section is closed, all the results that are available are
put into the result tree that is availalable through the backend. In
this way only results that have actually been placed through the
backend will be available.
"""
# Get the paths that lead to this section
path_list = []
self.get_paths([], path_list)
# Make the paths into a string
paths = []
for path in path_list:
path_string = ""
for metaname in reversed(path):
path_string += metaname + "/"
paths.append(path_string)
# Add all the value with the long path
for path in paths:
for metaname, value in self.simpleValues.iteritems():
long_path = path + metaname
self.backend.results._results[long_path].extend(value)
for metaname, value in self.arrayValues.iteritems():
long_path = path + metaname
self.backend.results._results[long_path].extend(value)
#===============================================================================
class SectionManager(object):
......@@ -424,15 +357,16 @@ class SectionManager(object):
references.append(pSect.lastSectionGIndex)
else:
references.append(-1)
# If the section is supposed to have parents, and none were actually
# open, raise an error
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)
def closeSection(self, backend, gIndex):
section = self.openSections[gIndex]
section.add_results()
del self.openSections[gIndex]
pass
def addValue(self, valueMetaInfo, value, gIndex):
if (gIndex == -1):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment