From 4c18c6e09c99752d801a2d558e8880730a4aa628 Mon Sep 17 00:00:00 2001 From: "Oleg E. Peil" Date: Wed, 12 Aug 2015 12:10:31 +0200 Subject: [PATCH] Added the parser of section [General] Parsing of two optional parameters (BASENAME and EFERMI) from section [General] from the config-file is implemented. If this section is not found the parameters are set to their default values, which is 'vasp' for BASENAME and nothing for EFERMI. Appropriate test is added to the 'inpconf' test suite. --- python/converters/vasp/python/inpconf.py | 19 +++++++++-- .../converters/vasp/test/_inpconf/example.cfg | 2 ++ .../vasp/test/_inpconf/test_general.py | 32 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 python/converters/vasp/test/_inpconf/test_general.py diff --git a/python/converters/vasp/python/inpconf.py b/python/converters/vasp/python/inpconf.py index 685f9b65..e27d082c 100644 --- a/python/converters/vasp/python/inpconf.py +++ b/python/converters/vasp/python/inpconf.py @@ -68,6 +68,9 @@ class ConfigParameters: 'normalize' : ('normalize', self.parse_string_logical, True), 'normion' : ('normion', self.parse_string_logical, False)} + self.gen_optional = { + 'basename' : ('basename', str, 'vasp'), + 'efermi' : ('efermi', float)} # # Special parsers @@ -177,7 +180,7 @@ class ConfigParameters: key = param_set[par][0] try: par_str = self.cp.get(section, par) - except ConfigParser.NoOptionError: + except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): if exception: message = "Required parameter '%s' not found in section [%s]"%(par, section) raise Exception(message) @@ -438,8 +441,18 @@ class ConfigParameters: """ Parses [General] section. """ -# TODO: write the parser - pass + self.general = {} + sections = self.cp.sections() + gen_section = filter(lambda s: s.lower() == 'general', sections) +# If no [General] section is found parse a dummy section name to the parser +# to reset parameters to their default values + if len(gen_section) > 1: + raise Exception("More than one section [General] is found") + if len(gen_section) == 0: + gen_section = 'general' + gen_section = gen_section[0] + parsed = self.parse_parameter_set(gen_section, self.gen_optional, exception=False) + self.general.update(parsed) ################################################################################ # diff --git a/python/converters/vasp/test/_inpconf/example.cfg b/python/converters/vasp/test/_inpconf/example.cfg index 4b03d4e0..1919bc23 100644 --- a/python/converters/vasp/test/_inpconf/example.cfg +++ b/python/converters/vasp/test/_inpconf/example.cfg @@ -1,4 +1,6 @@ [General] +BASENAME = test_base +EFERMI = 0.1 [Group 1] SHELLS = 1 2 diff --git a/python/converters/vasp/test/_inpconf/test_general.py b/python/converters/vasp/test/_inpconf/test_general.py new file mode 100644 index 00000000..64e7518e --- /dev/null +++ b/python/converters/vasp/test/_inpconf/test_general.py @@ -0,0 +1,32 @@ +r""" +Tests of 'parse_general()' defined in ConfigParameters class +""" +import arraytest +import numpy as np +from inpconf import ConfigParameters + +################################################################################ +# +# TestParseGeneral +# +################################################################################ +class TestParseGeneral(arraytest.ArrayTestCase): + """ + Function: + + def parse_general(self) + + Scenarios: + + - **if** a correct [General] section is defined **return** a dictionary + """ +# Scenario 1 + def test_example(self): + conf_pars = ConfigParameters('example.cfg') + conf_pars.parse_general() + res = conf_pars.general + expected = {'basename': 'test_base', 'efermi': 0.1} + self.assertDictEqual(res, expected) + + +