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

Added a small debugging function that prints out the parsed quantities in a...

Added a small debugging function that prints out the parsed quantities in a compact, tree-like fashion.
parent 2a0aed4b
Branches
Tags
No related merge requests found
......@@ -5,6 +5,7 @@ from builtins import range
from builtins import object
from builtins import str
import numpy as np
import json
import io
import logging
from collections import defaultdict
......@@ -16,6 +17,7 @@ class DummyFile(object):
def write(self,input):
pass
#===============================================================================
class LocalBackend(object):
"""A backend that outputs results into a regular python dictionary. This is
......@@ -187,9 +189,6 @@ class LocalBackend(object):
def startedParsingSession(self, mainFileUri, parserInfo, parsingStatus=None, parsingErrors=None):
pass
def get_section(self, path):
return self.result_tree[path]
def finishedParsingSession(self, parsingStatus, parsingErrors, mainFileUri=None, parserInfo=None):
"""Called when the parsing finishes.
"""
......@@ -203,16 +202,18 @@ class Results(object):
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)
self._metaInfoEnv = metaInfoEnv
def __getitem__(self, metaname):
"""Return the data that resides in the given path of metainfos. The
path is of the form "metanameA/metanameB" where each metainfo name is
separated by a slash. If there is nothing to return in the given path,
an exception with supporting information is raised.
"""Return the data or section corrresponding the the given metainfo name.
Args:
metaname: The unique name of the metainfo to get.
Raises:
LookupError if the metaname is not defined in the metainfo
environment or the parser has not output any value for it.
"""
self.test_validity(metaname)
......@@ -241,17 +242,7 @@ class Results(object):
else:
return np.array(result)
def get_path(self, metaname):
"""Returns the full path of the given metaname. If multiple options are
available return then as a list.
"""
self.test_validity(metaname)
paths = self._clash_dict[metaname]
if len(paths) == 1:
return paths[0]
else:
return paths
raise LookupError("The metainfo definition doesn't seem to contain '{}'. Check for typos of update you metainfo repository.".format(metaname))
def test_validity(self, metaname):
"""Tests if the given metaname is present in the metainfo environment.
......@@ -260,18 +251,61 @@ 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 get_metainfo(self, metaname):
"""Returns the metainfo object for the given metaname.
"""
self.test_validity(metaname)
return self._metaInfoEnv.infoKinds[metaname]
def print_summary(self):
"""Return a string representing the data contained in the results. This
is a summary that can be used for debugging.
"""
for key, value in self._results.items():
print(key + ": {}".format(value))
metas = {}
roots = {}
for meta in self._metaInfoEnv.infoKinds.values():
metaobj = {}
metaobj["name"] = meta.name
metaobj["children"] = []
metaobj["parents"] = meta.superNames
metaobj["kindStr"] = meta.kindStr
metas[meta.name] = metaobj
for meta in metas.values():
parentNames = meta["parents"]
if len(parentNames) == 0:
roots[meta["name"]] = meta
else:
for parentName in parentNames:
parent = metas[parentName]
parent["children"].append(meta)
# Sort the children according to type
parent["children"].sort(key=lambda x: {"type_section": 0, "type_abstract_document_content": 1, "type_document_content": 2, "type_dimension": 3}[x["kindStr"]])
section_run = roots["section_run"]
self.print_metainfo(section_run)
def print_metainfo(self, meta, level=0):
"""Recursive printing function for the metainfos. To print the whole
tree, call this function on the root section.
"""
name = meta["name"]
metatype = meta["kindStr"]
if metatype != "type_abstract_document_content":
try:
result = self[name]
except LookupError:
return
if isinstance(result, dict):
if len(result.keys()) == 0:
return
if metatype == "type_section":
print(level*" " + name + ":")
elif metatype == "type_document_content":
print(level*" " + name)
elif metatype == "type_dimension":
print(level*" " + name)
level += 1
for child in meta["children"]:
self.print_metainfo(child, level)
#===============================================================================
......@@ -302,16 +336,6 @@ class Section(object):
res = self.subSectionValues.get(metaName, None)
return res
def get_paths(self, path, results):
path.append(self.name)
for parent in self.parents:
new_path_branch = list(path)
parent.get_paths(new_path_branch, results)
if len(self.parents) == 0:
results.append(path)
def addValue(self, metaInfo, value):
if self.backend.store:
vals = self.simpleValues.get(metaInfo.name, None)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment