Skip to content

Commit

Permalink
SectionManager: Move out load_config_file()
Browse files Browse the repository at this point in the history
Part of refactoring SectionManager to a bunch of functions.
  • Loading branch information
sils committed May 29, 2015
1 parent eeb6912 commit 4678483
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions coalib/settings/SectionManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ def merge_section_dicts(lower, higher):
return lower


def load_config_file(filename, log_printer, silent=False):
"""
Loads sections from a config file. Prints an appropriate warning if
it doesn't exist and returns a section dict containing an empty
default section in that case.
It assumes that the cli_sections are available.
:param filename: The file to load settings from.
:param log_printer: The log printer to log the warning to (in case).
:param silent: Whether or not to warn the user if the file doesn't
exist.
"""
filename = os.path.abspath(filename)
conf_parser = ConfParser()

try:
return conf_parser.reparse(filename)
except conf_parser.FileNotFoundError:
if not silent:
log_printer.warn(
_("The requested coafile '{filename}' does not exist. "
"Thus it will not be used.").format(filename=filename))

return {"default": Section("default")}


class SectionManager:
"""
The SectionManager does the following things:
Expand Down Expand Up @@ -111,11 +138,13 @@ def _load_configuration(self, arg_list):
self.cli_sections["default"].contents.pop("targets", "")):
self.targets.append(item.lower())

self.default_sections = self._load_config_file(
StringConstants.system_coafile)
self.default_sections = load_config_file(
StringConstants.system_coafile,
self.log_printer)

self.user_sections = self._load_config_file(
self.user_sections = load_config_file(
StringConstants.user_coafile,
self.log_printer,
silent=True)

default_config = str(
Expand All @@ -125,7 +154,7 @@ def _load_configuration(self, arg_list):
config = os.path.abspath(str(
self.cli_sections["default"].get("config", user_config)))

self.coafile_sections = self._load_config_file(config)
self.coafile_sections = load_config_file(config, self.log_printer)

self.sections = merge_section_dicts(self.default_sections,
self.user_sections)
Expand All @@ -140,30 +169,6 @@ def _load_configuration(self, arg_list):
if section != "default":
self.sections[section].defaults = self.sections["default"]

def _load_config_file(self, filename, silent=False):
"""
Loads sections from a config file. Prints an appropriate warning if
it doesn't exist and returns a section dict containing an empty
default section in that case.
It assumes that the cli_sections are available.
:param filename: The file to load settings from.
:param silent: Whether or not to warn the user if the file doesn't
exist.
"""
filename = os.path.abspath(filename)

try:
return self.conf_parser.reparse(filename)
except self.conf_parser.FileNotFoundError:
if not silent:
self.log_printer.warn(
_("The requested coafile '{filename}' does not exist. "
"Thus it will not be used.").format(filename=filename))

return {"default": Section("default")}

def retrieve_logging_objects(self, section):
"""
Creates an appropriate log printer and interactor according to the
Expand Down

0 comments on commit 4678483

Please sign in to comment.