From 8a586df1ec60031dfb733b680e7ae1841d52111a Mon Sep 17 00:00:00 2001
From: Markus Scheidgen <markus.scheidgen@gmail.com>
Date: Thu, 26 Mar 2020 16:02:52 +0100
Subject: [PATCH] Allow normalizer to access metadata via backend.

---
 nomad/processing/data.py        | 9 ++++++++-
 tests/metainfo/test_metainfo.py | 5 +++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/nomad/processing/data.py b/nomad/processing/data.py
index f102e3bb6e..3ee70bcfde 100644
--- a/nomad/processing/data.py
+++ b/nomad/processing/data.py
@@ -399,6 +399,11 @@ class Calc(Proc):
     @task
     def normalizing(self):
         ''' The *task* that encapsulates all normalizing related actions. '''
+
+        # allow normalizer to access and add data to the entry metadata
+        self._parser_backend.entry_archive.m_add_sub_section(
+            datamodel.EntryArchive.section_metadata, self._entry_metadata)
+
         for normalizer in normalizers:
             if normalizer.domain != parser_dict[self.parser].domain:
                 continue
@@ -459,7 +464,9 @@ class Calc(Proc):
         else:
             entry_archive = datamodel.EntryArchive()
 
-        entry_archive.m_add_sub_section(datamodel.EntryArchive.section_metadata, self._entry_metadata)
+        if entry_archive.section_metadata is None:
+            entry_archive.m_add_sub_section(datamodel.EntryArchive.section_metadata, self._entry_metadata)
+
         entry_archive.processing_logs = self._calc_proc_logs
 
         try:
diff --git a/tests/metainfo/test_metainfo.py b/tests/metainfo/test_metainfo.py
index f9466bb5ae..611c46320f 100644
--- a/tests/metainfo/test_metainfo.py
+++ b/tests/metainfo/test_metainfo.py
@@ -124,6 +124,11 @@ class TestM2:
         assert len(Run.m_def.all_sub_sections_by_section[System.m_def]) == 1
         assert Run.m_def.all_sub_sections_by_section[System.m_def][0].sub_section == System.m_def
 
+    def test_unset_sub_section(self):
+        run = Run()
+        assert run.systems == []
+        assert run.parsing is None
+
     def test_properties(self):
         assert len(Run.m_def.all_properties) == 6
 
-- 
GitLab