mirror of
https://github.com/triqs/dft_tools
synced 2024-11-07 06:33:48 +01:00
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.
This commit is contained in:
parent
4bbafa239e
commit
4c18c6e09c
@ -68,6 +68,9 @@ class ConfigParameters:
|
|||||||
'normalize' : ('normalize', self.parse_string_logical, True),
|
'normalize' : ('normalize', self.parse_string_logical, True),
|
||||||
'normion' : ('normion', self.parse_string_logical, False)}
|
'normion' : ('normion', self.parse_string_logical, False)}
|
||||||
|
|
||||||
|
self.gen_optional = {
|
||||||
|
'basename' : ('basename', str, 'vasp'),
|
||||||
|
'efermi' : ('efermi', float)}
|
||||||
|
|
||||||
#
|
#
|
||||||
# Special parsers
|
# Special parsers
|
||||||
@ -177,7 +180,7 @@ class ConfigParameters:
|
|||||||
key = param_set[par][0]
|
key = param_set[par][0]
|
||||||
try:
|
try:
|
||||||
par_str = self.cp.get(section, par)
|
par_str = self.cp.get(section, par)
|
||||||
except ConfigParser.NoOptionError:
|
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
|
||||||
if exception:
|
if exception:
|
||||||
message = "Required parameter '%s' not found in section [%s]"%(par, section)
|
message = "Required parameter '%s' not found in section [%s]"%(par, section)
|
||||||
raise Exception(message)
|
raise Exception(message)
|
||||||
@ -438,8 +441,18 @@ class ConfigParameters:
|
|||||||
"""
|
"""
|
||||||
Parses [General] section.
|
Parses [General] section.
|
||||||
"""
|
"""
|
||||||
# TODO: write the parser
|
self.general = {}
|
||||||
pass
|
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)
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
#
|
#
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
[General]
|
[General]
|
||||||
|
BASENAME = test_base
|
||||||
|
EFERMI = 0.1
|
||||||
|
|
||||||
[Group 1]
|
[Group 1]
|
||||||
SHELLS = 1 2
|
SHELLS = 1 2
|
||||||
|
32
python/converters/vasp/test/_inpconf/test_general.py
Normal file
32
python/converters/vasp/test/_inpconf/test_general.py
Normal file
@ -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)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user