Commit abe20c0c authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added metainfo unload and deep copy.

parent 3f6bfc40
......@@ -537,10 +537,9 @@ class MResource():
for collections in self.__data.values():
for section in collections:
section.m_parent = None
section.__dict__.clear()
collections.clear()
# TODO break actual references via quantities
def m_to_dict(self, filter: TypingCallable[['MSection'], bool] = None):
if filter is None:
def filter(_): # pylint: disable=function-redefined
......@@ -1468,14 +1467,23 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
return errors, warnings
def m_copy(self: MSectionBound) -> MSectionBound:
def m_copy(self: MSectionBound, deep=False, parent=None) -> MSectionBound:
# TODO this a shallow copy, but should be a deep copy
copy = self.m_def.section_cls()
copy.__dict__.update(**self.__dict__)
copy.m_parent = None
copy.m_parent_index = -1
copy.m_parent_sub_section = None
copy.m_parent = parent
copy.m_parent_index = -1 if parent is None else self.m_parent_index
copy.m_parent_sub_section = None if parent is None else self.m_parent_sub_section
copy.m_resource = None
if deep:
for sub_section_def in self.m_def.all_sub_sections.values():
sub_sections_copy = [
sub_section.m_copy(deep=True, parent=copy)
for sub_section in self.m_get_sub_sections(sub_section_def)]
copy.__dict__[sub_section_def.name] = sub_sections_copy
return cast(MSectionBound, copy)
def m_all_validate(self):
......
......@@ -237,6 +237,12 @@ class Calc(Proc):
self.archiving()
finally:
# close loghandler that was not closed due to failures
try:
if self._parser_backend and self._parser_backend.resource:
self._parser_backend.resource.unload()
except Exception as e:
logger.error('could unload processing results', exc_info=e)
try:
if self._calc_proc_logwriter is not None:
self._calc_proc_logwriter.close()
......@@ -283,6 +289,12 @@ class Calc(Proc):
self.archiving()
finally:
# close loghandler that was not closed due to failures
try:
if self._parser_backend and self._parser_backend.resource:
self._parser_backend.resource.unload()
except Exception as e:
logger.error('could unload processing results', exc_info=e)
try:
if self._calc_proc_logwriter is not None:
self._calc_proc_logwriter.close()
......
......@@ -553,6 +553,24 @@ class TestM1:
obj = ReferencingSection.m_from_dict(obj.m_to_dict(with_meta=True))
assert obj.proxy.name == 'test_value'
def test_copy(self):
run = Run()
run.m_create(Parsing).parser_name = 'test'
system = run.m_create(System)
system.atom_labels = ['H', 'O']
copy = run.m_copy()
assert copy is not run
assert copy.m_def is run.m_def
assert copy.systems is run.systems
copy = run.m_copy(deep=True)
assert copy is not run
assert copy.systems is not run.systems
assert copy.systems[0] is not run.systems[0]
assert copy.systems[0].m_parent_index == 0
assert copy.systems[0].m_parent_sub_section is run.systems[0].m_parent_sub_section
class TestDatatypes:
......
......@@ -405,7 +405,8 @@ def parser_in_dir(dir):
if parser is not None:
try:
parser.run(file_path)
backend = parser.run(file_path)
backend.resource.unload()
except Exception as e:
print(file_path, parser, 'FAILURE', e)
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment