mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-04-10 04:09:58 +00:00
Refactor to unify different code paths.
This commit is contained in:
parent
0d270e1290
commit
b79504a43b
@ -190,7 +190,7 @@ def read_config(basedir, portnumfile, generated_files=[], _valid_config=None):
|
||||
if e.errno != errno.ENOENT:
|
||||
raise
|
||||
# The file is missing, just create empty ConfigParser.
|
||||
parser = configutil.create_parser()
|
||||
parser = configutil.get_config_from_string(u"")
|
||||
|
||||
configutil.validate_config(config_fname, parser, _valid_config)
|
||||
|
||||
@ -207,11 +207,11 @@ def config_from_string(basedir, portnumfile, config_str, _valid_config=None):
|
||||
if _valid_config is None:
|
||||
_valid_config = _common_valid_config()
|
||||
|
||||
# load configuration from in-memory string
|
||||
parser = configutil.create_parser()
|
||||
if isinstance(config_str, bytes):
|
||||
config_str = config_str.decode("utf-8")
|
||||
parser.read_string(config_str)
|
||||
|
||||
# load configuration from in-memory string
|
||||
parser = configutil.get_config_from_string(config_str)
|
||||
|
||||
fname = "<in-memory>"
|
||||
configutil.validate_config(fname, parser, _valid_config)
|
||||
|
@ -29,26 +29,28 @@ class UnknownConfigError(Exception):
|
||||
"""
|
||||
|
||||
|
||||
def create_parser():
|
||||
"""
|
||||
Create a ConfigParser pre-configured the way we want it, for consistency
|
||||
across different code paths.
|
||||
"""
|
||||
return ConfigParser(strict=False)
|
||||
|
||||
|
||||
def get_config(tahoe_cfg):
|
||||
"""Load the config, returning a ConfigParser.
|
||||
|
||||
Configuration is returned as Unicode strings.
|
||||
"""
|
||||
config = create_parser()
|
||||
# Byte Order Mark is an optional garbage code point you sometimes get at
|
||||
# the start of UTF-8 encoded files. Especially on Windows. Skip it by using
|
||||
# utf-8-sig. https://en.wikipedia.org/wiki/Byte_order_mark
|
||||
with open(tahoe_cfg, "r", encoding="utf-8-sig") as f:
|
||||
config.read_file(f)
|
||||
return config
|
||||
cfg_string = f.read()
|
||||
return get_config_from_string(cfg_string)
|
||||
|
||||
|
||||
def get_config_from_string(tahoe_cfg_string):
|
||||
"""Load the config from a string, return the ConfigParser.
|
||||
|
||||
Configuration is returned as Unicode strings.
|
||||
"""
|
||||
parser = ConfigParser(strict=False)
|
||||
parser.read_string(tahoe_cfg_string)
|
||||
return parser
|
||||
|
||||
|
||||
def set_config(config, section, option, value):
|
||||
if not config.has_section(section):
|
||||
|
Loading…
x
Reference in New Issue
Block a user