config: Make get_section a public method.

As hooks and importers both require more configuration, they're gonna need
to have lower-level access to configuration settings.
This commit is contained in:
Brett Smith 2017-12-31 16:50:07 -05:00
parent 76f2707aac
commit 6d1a7cb57d
2 changed files with 11 additions and 4 deletions

View file

@ -206,7 +206,7 @@ class Configuration:
finally:
self.args.use_config = prev_section
def _get_section(self, section_name):
def get_section(self, section_name):
if section_name is None:
section_name = self.args.use_config
return self.conffile[section_name]
@ -226,7 +226,7 @@ class Configuration:
return self._get_from_dict(self.dates, section_name)
def get_loglevel(self, section_name=None):
section_config = self._get_section(section_name)
section_config = self.get_section(section_name)
level_name = section_config['loglevel']
try:
return getattr(logging, level_name.upper())
@ -234,7 +234,7 @@ class Configuration:
raise errors.UserInputConfigurationError("not a valid loglevel", level_name)
def get_output_path(self, section_name=None):
section_config = self._get_section(section_name)
section_config = self.get_section(section_name)
return self._s_to_path(section_config['output_path'])
def open_output_file(self, section_name=None):
@ -242,7 +242,7 @@ class Configuration:
return self._open_path(path, self.stdout, 'a')
def get_template(self, config_key, section_name=None, factory=template.Template):
section_config = self._get_section(section_name)
section_config = self.get_section(section_name)
try:
template_s = section_config[config_key]
except KeyError:

View file

@ -47,6 +47,13 @@ def test_template_parsing():
assert "\nIncome:Donations -{amount}\n" in tmpl_s
assert "\n;IncomeTag: Donations\n" in tmpl_s
def test_get_section():
config = config_from_file('test_config.ini', ['--date-format', '%m/%d/%Y'])
section = config.get_section('Templates')
assert section['output_path'] == 'Template.output'
assert section['date_format'] == '%m/%d/%Y'
assert section['signed_currencies'] == 'EUR'
@pytest.mark.parametrize('arg_s', [None, '-', 'output.ledger'])
def test_output_path(arg_s):
arglist = [] if arg_s is None else ['-O', arg_s]