From 35a90bed0cd6cb6f0110203f017221ed9bcfb1a9 Mon Sep 17 00:00:00 2001
From: Lauri Himanen <lauri.himanen@aalto.fi>
Date: Wed, 18 Oct 2017 18:27:02 +0300
Subject: [PATCH] Started using the new way of giving the input file to the
 parser.

---
 parser/parser-cp2k/cp2kparser/parser.py          |  2 +-
 .../cp2kparser/versions/cp2k262/commonparser.py  |  4 ++--
 .../cp2kparser/versions/cp2k262/geooptparser.py  |  4 ++--
 .../cp2kparser/versions/cp2k262/inputparser.py   | 16 ++++++++--------
 .../cp2kparser/versions/cp2k262/mdparser.py      |  4 ++--
 .../versions/cp2k262/singlepointforceparser.py   |  8 ++++----
 .../versions/cp2k262/singlepointparser.py        |  8 ++++----
 regtests/regtests.py                             |  6 +++---
 8 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/parser/parser-cp2k/cp2kparser/parser.py b/parser/parser-cp2k/cp2kparser/parser.py
index 74bf4b5..12c22bf 100644
--- a/parser/parser-cp2k/cp2kparser/parser.py
+++ b/parser/parser-cp2k/cp2kparser/parser.py
@@ -153,4 +153,4 @@ class CP2KParser(ParserInterface):
             )
             raise
 
-        self.main_parser = parser_class(self.parser_context.main_file, self.parser_context)
+        self.main_parser = parser_class(self.parser_context)
diff --git a/parser/parser-cp2k/cp2kparser/versions/cp2k262/commonparser.py b/parser/parser-cp2k/cp2kparser/versions/cp2k262/commonparser.py
index 5b2bd27..a7110a2 100644
--- a/parser/parser-cp2k/cp2kparser/versions/cp2k262/commonparser.py
+++ b/parser/parser-cp2k/cp2kparser/versions/cp2k262/commonparser.py
@@ -445,8 +445,8 @@ class CP2KCommonParser(CommonParser):
         # If the input file is available, parse it
         filepath = self.file_service.get_file_by_id("input")
         if filepath is not None:
-            input_parser = CP2KInputParser(filepath, self.parser_context)
-            input_parser.parse()
+            input_parser = CP2KInputParser(self.parser_context)
+            input_parser.parse(filepath)
         else:
             logger.warning("The input file of the calculation could not be found.")
 
diff --git a/parser/parser-cp2k/cp2kparser/versions/cp2k262/geooptparser.py b/parser/parser-cp2k/cp2kparser/versions/cp2k262/geooptparser.py
index 3c1399d..ead4532 100644
--- a/parser/parser-cp2k/cp2kparser/versions/cp2k262/geooptparser.py
+++ b/parser/parser-cp2k/cp2kparser/versions/cp2k262/geooptparser.py
@@ -16,10 +16,10 @@ class CP2KGeoOptParser(MainHierarchicalParser):
     """Used to parse the CP2K calculation with run types:
         -GEO_OPT/GEOMETRY_OPTIMIZATION
     """
-    def __init__(self, file_path, parser_context):
+    def __init__(self, parser_context):
         """
         """
-        super(CP2KGeoOptParser, self).__init__(file_path, parser_context)
+        super(CP2KGeoOptParser, self).__init__(parser_context)
         self.setup_common_matcher(CP2KCommonParser(parser_context))
         self.traj_iterator = None
         self.energy_reeval_quickstep = None
diff --git a/parser/parser-cp2k/cp2kparser/versions/cp2k262/inputparser.py b/parser/parser-cp2k/cp2kparser/versions/cp2k262/inputparser.py
index 65275a0..5504934 100644
--- a/parser/parser-cp2k/cp2kparser/versions/cp2k262/inputparser.py
+++ b/parser/parser-cp2k/cp2kparser/versions/cp2k262/inputparser.py
@@ -28,7 +28,7 @@ class CP2KInputParser(AbstractBaseParser):
     supports variables. This is currently not supported, but may be added at
     some point.
     """
-    def __init__(self, file_path, parser_context):
+    def __init__(self, parser_context):
         """
         Attributes:
             input_tree: The input structure for this version of CP2K. The
@@ -38,7 +38,7 @@ class CP2KInputParser(AbstractBaseParser):
                 variables have been stated explicitly and the additional input files have
                 been merged.
         """
-        super(CP2KInputParser, self).__init__(file_path, parser_context)
+        super(CP2KInputParser, self).__init__(parser_context)
         self.input_tree = None
         self.input_lines = None
         self.unit_mapping = {
@@ -68,16 +68,16 @@ class CP2KInputParser(AbstractBaseParser):
         self.cache_service.add("traj_add_last")
         # self.cache_service.add("electronic_structure_method")
 
-    def parse(self):
+    def parse(self, filepath):
 
         #=======================================================================
         # Preprocess to spell out variables and to include stuff from other
         # files
-        self.preprocess_input()
+        self.preprocess_input(filepath)
 
         #=======================================================================
         # Gather the information from the input file
-        self.fill_input_tree(self.file_path)
+        self.fill_input_tree(filepath)
 
         #=======================================================================
         # Parse everything in the input to cp2k specific metadata
@@ -459,14 +459,14 @@ class CP2KInputParser(AbstractBaseParser):
         input_tree_pickle_file = open(pickle_path, 'rb')
         self.input_tree = pickle.load(input_tree_pickle_file)
 
-    def preprocess_input(self):
+    def preprocess_input(self, filepath):
         """Preprocess the input file. Concatenate .inc files into the main
         input file and explicitly state all variables.
         """
         # Read the input file into memory. It shouldn't be that big so we can
         # do this easily
         input_lines = []
-        with open(self.file_path, "r") as f:
+        with open(filepath, "r") as f:
             for line in f:
                 input_lines.append(line.strip())
 
@@ -477,7 +477,7 @@ class CP2KInputParser(AbstractBaseParser):
             if line.startswith("@INCLUDE") or line.startswith("@include"):
                 split = line.split(None, 1)
                 includepath = split[1]
-                basedir = os.path.dirname(self.file_path)
+                basedir = os.path.dirname(filepath)
                 filepath = os.path.join(basedir, includepath)
                 filepath = os.path.abspath(filepath)
                 if not os.path.isfile(filepath):
diff --git a/parser/parser-cp2k/cp2kparser/versions/cp2k262/mdparser.py b/parser/parser-cp2k/cp2kparser/versions/cp2k262/mdparser.py
index 4e126fc..93b25af 100644
--- a/parser/parser-cp2k/cp2kparser/versions/cp2k262/mdparser.py
+++ b/parser/parser-cp2k/cp2kparser/versions/cp2k262/mdparser.py
@@ -18,10 +18,10 @@ class CP2KMDParser(MainHierarchicalParser):
         -MD
         -MOLECULAR_DYNAMICS
     """
-    def __init__(self, file_path, parser_context):
+    def __init__(self, parser_context):
         """
         """
-        super(CP2KMDParser, self).__init__(file_path, parser_context)
+        super(CP2KMDParser, self).__init__(parser_context)
         self.setup_common_matcher(CP2KCommonParser(parser_context))
         self.traj_iterator = None
         self.vel_iterator = None
diff --git a/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointforceparser.py b/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointforceparser.py
index ba3b529..403f5d2 100644
--- a/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointforceparser.py
+++ b/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointforceparser.py
@@ -9,13 +9,13 @@ class CP2KSinglePointForceParser(AbstractBaseParser):
     calculation. It is not exactly an ZYX file, so here we define separate
     parser.
     """
-    def __init__(self, file_path, parser_context):
-        super(CP2KSinglePointForceParser, self).__init__(file_path, parser_context)
+    def __init__(self, parser_context):
+        super(CP2KSinglePointForceParser, self).__init__(parser_context)
 
-    def parse(self):
+    def parse(self, filepath):
         start = False
         forces = []
-        with open(self.file_path) as f:
+        with open(filepath) as f:
             for line in f:
                 if line.startswith(" # Atom"):
                     start = True
diff --git a/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointparser.py b/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointparser.py
index 001fc04..1cf169e 100644
--- a/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointparser.py
+++ b/parser/parser-cp2k/cp2kparser/versions/cp2k262/singlepointparser.py
@@ -13,10 +13,10 @@ class CP2KSinglePointParser(MainHierarchicalParser):
         -ENERGY
         -ENERGY_FORCE
     """
-    def __init__(self, file_path, parser_context):
+    def __init__(self, parser_context):
         """
         """
-        super(CP2KSinglePointParser, self).__init__(file_path, parser_context)
+        super(CP2KSinglePointParser, self).__init__(parser_context)
         self.setup_common_matcher(CP2KCommonParser(parser_context))
 
         #=======================================================================
@@ -54,8 +54,8 @@ class CP2KSinglePointParser(MainHierarchicalParser):
         if section["atom_forces"] is None:
             force_file = self.file_service.get_file_by_id("force_file_single_point")
             if force_file is not None:
-                force_parser = CP2KSinglePointForceParser(force_file, self.parser_context)
-                force_parser.parse()
+                force_parser = CP2KSinglePointForceParser(self.parser_context)
+                force_parser.parse(force_file)
             else:
                 logger.warning("The file containing the forces printed by ENERGY_FORCE calculation could not be found.")
 
diff --git a/regtests/regtests.py b/regtests/regtests.py
index 5da0635..a64149c 100644
--- a/regtests/regtests.py
+++ b/regtests/regtests.py
@@ -17,8 +17,8 @@ def get_result(folder, metaname=None):
     """
     dirname = os.path.dirname(__file__)
     filename = os.path.join("cp2k_{}".format(VERSION), dirname, folder, "unittest.out")
-    parser = CP2KParser(filename, None, debug=True, log_level=logging.CRITICAL)
-    results = parser.parse()
+    parser = CP2KParser(None, debug=True, log_level=logging.CRITICAL)
+    results = parser.parse(filename)
 
     if metaname is None:
         return results
@@ -32,7 +32,7 @@ class TestErrors(unittest.TestCase):
     def test_no_file(self):
         """File is not no present.
         """
-        with self.assertRaises(IOError):
+        with self.assertRaises(ValueError):
             get_result("errors/no_file", "XC_functional")
 
     def test_invalid_file(self):
-- 
GitLab