本文整理汇总了Python中coalib.parsing.ConfParser.ConfParser.reparse方法的典型用法代码示例。如果您正苦于以下问题:Python ConfParser.reparse方法的具体用法?Python ConfParser.reparse怎么用?Python ConfParser.reparse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类coalib.parsing.ConfParser.ConfParser
的用法示例。
在下文中一共展示了ConfParser.reparse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ConfWriterTestCase
# 需要导入模块: from coalib.parsing.ConfParser import ConfParser [as 别名]
# 或者: from coalib.parsing.ConfParser.ConfParser import reparse [as 别名]
class ConfWriterTestCase(unittest.TestCase):
example_file = "to be ignored \n\
save=true\n\
a_default, another = val \n\
TEST = tobeignored # do you know that thats a comment \n\
test = push \n\
t = \n\
[MakeFiles] \n\
j , ANother = a \n\
multiline \n\
value \n\
; just a omment \n\
; just a omment \n"
def setUp(self):
self.file = os.path.join(tempfile.gettempdir(), "ConfParserTestFile")
with open(self.file, "w", encoding='utf-8') as filehandler:
filehandler.write(self.example_file)
self.conf_parser = ConfParser()
self.write_file_name = os.path.join(tempfile.gettempdir(),
"ConfWriterTestFile")
self.uut = ConfWriter(self.write_file_name)
def tearDown(self):
os.remove(self.file)
os.remove(self.write_file_name)
def test_exceptions(self):
self.assertRaises(TypeError, self.uut.write_section, 5)
def test_write(self):
result_file = ["[Default]\n",
"save = true\n",
"a_default, another = val\n",
"# do you know that thats a comment\n",
"test = push\n",
"t = \n",
"\n",
"[MakeFiles]\n",
"j, ANother = a\n",
"multiline\n",
"value\n",
"; just a omment\n",
"; just a omment\n"]
self.uut.write_sections(self.conf_parser.reparse(self.file))
del self.uut
with open(self.write_file_name, "r") as f:
lines = f.readlines()
self.assertEqual(result_file, lines)
示例2: run
# 需要导入模块: from coalib.parsing.ConfParser import ConfParser [as 别名]
# 或者: from coalib.parsing.ConfParser.ConfParser import reparse [as 别名]
class SectionManager:
"""
The SectionManager does the following things:
- Reading all settings in sections from
- Default config
- CLI
- Configuration file
- Collecting all the bears
- Filling up all needed settings
- Write back the new sections to the configuration file if needed
- Give all information back to caller
This is done when the run() method is invoked. Anything else is just helper
stuff and initialization.
"""
def __init__(self):
self.cli_sections = None
self.default_sections = None
self.user_sections = None
self.coafile_sections = None
self.sections = None
self.cli_parser = CliParser()
self.conf_parser = ConfParser()
self.conf_writer = None
self.local_bears = {}
self.global_bears = {}
self.targets = []
def run(self, arg_list=sys.argv[1:]):
self._load_configuration(arg_list)
self._fill_settings()
self._save_configuration()
self._warn_nonexistent_targets()
return self.sections, self.local_bears, self.global_bears, self.targets
def _load_configuration(self, arg_list):
self.cli_sections = self.cli_parser.reparse(arg_list=arg_list)
# We dont want to store targets argument back to file, thus remove it
for item in list(
self.cli_sections["default"].contents.pop("targets", "")):
self.targets.append(item.lower())
self.default_sections = self._load_config_file(
StringConstants.system_coafile)
self.user_sections = self._load_config_file(
StringConstants.user_coafile,
silent=True)
default_config = str(
self.default_sections["default"].get("config", ".coafile"))
user_config = str(
self.user_sections["default"].get("config", default_config))
config = os.path.abspath(str(
self.cli_sections["default"].get("config", user_config)))
self.coafile_sections = self._load_config_file(config)
self.sections = self._merge_section_dicts(self.default_sections,
self.user_sections)
self.sections = self._merge_section_dicts(self.sections,
self.coafile_sections)
self.sections = self._merge_section_dicts(self.sections,
self.cli_sections)
for section in self.sections:
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.cli_sections["default"].retrieve_logging_objects()
self.cli_sections["default"].log_printer.warn(
_("The requested coafile '{filename}' does not exist. "
"Thus it will not be used.").format(filename=filename))
return {"default": Section("default")}
#.........这里部分代码省略.........