summaryrefslogtreecommitdiff
path: root/kvmd/yamlconf
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-06-28 18:59:36 +0300
committerDevaev Maxim <[email protected]>2019-06-28 18:59:36 +0300
commitef3c62a7af520673154233e2d3c89492ffec3195 (patch)
tree2b78c5918143eff8ef2c89484ac99fc6b7588438 /kvmd/yamlconf
parentff270591b032cc1f8e342f20a04b84e5dbb31191 (diff)
f-strings
Diffstat (limited to 'kvmd/yamlconf')
-rw-r--r--kvmd/yamlconf/__init__.py18
-rw-r--r--kvmd/yamlconf/dumper.py5
-rw-r--r--kvmd/yamlconf/loader.py2
3 files changed, 13 insertions, 12 deletions
diff --git a/kvmd/yamlconf/__init__.py b/kvmd/yamlconf/__init__.py
index 99b67255..123dc915 100644
--- a/kvmd/yamlconf/__init__.py
+++ b/kvmd/yamlconf/__init__.py
@@ -41,9 +41,9 @@ def build_raw_from_options(options: List[str]) -> Dict[str, Any]:
for option in options:
(key, value) = (option.split("=", 1) + [None])[:2] # type: ignore
if len(key.strip()) == 0:
- raise ConfigError("Empty option key (required 'key=value' instead of %r)" % (option))
+ raise ConfigError(f"Empty option key (required 'key=value' instead of {option!r})")
if value is None:
- raise ConfigError("No value for key %r" % (key))
+ raise ConfigError(f"No value for key {key!r}")
section = raw
subs = list(filter(None, map(str.strip, key.split("/"))))
@@ -61,7 +61,7 @@ def _parse_value(value: str) -> Any:
and value not in ["true", "false", "null"]
and not value.startswith(("{", "[", "\""))
):
- value = "\"%s\"" % (value)
+ value = f"\"{value}\""
return json.loads(value)
@@ -124,13 +124,13 @@ class Option:
self.help = help
def __repr__(self) -> str:
- return "<Option(default={0.default}, type={0.type}, only_if={0.only_if}, unpack_as={0.unpack_as})>".format(self)
+ return f"<Option(default={self.default}, type={self.type}, only_if={self.only_if}, unpack_as={self.unpack_as})>"
# =====
def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, ...]=()) -> Section:
if not isinstance(raw, dict):
- raise ConfigError("The node %r must be a dictionary" % ("/".join(_keys) or "/"))
+ raise ConfigError(f"The node {('/'.join(_keys) or '/')!r} must be a dictionary")
config = Section()
@@ -150,7 +150,7 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, .
if only_if and no_only_if: # pylint: disable=no-else-raise
# Перекрестный only_if запрещен
- raise RuntimeError("Found only_if recursuon on key %r" % (make_full_name(key)))
+ raise RuntimeError(f"Found only_if recursuon on key {make_full_name(key)!r}")
elif only_if and (
(not only_if_negative and not process_option(only_if, no_only_if=True))
or (only_if_negative and process_option(only_if, no_only_if=True))
@@ -162,7 +162,7 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, .
try:
value = option.type(value)
except ValueError as err:
- raise ConfigError("Invalid value %r for key %r: %s" % (value, make_full_name(key), str(err)))
+ raise ConfigError(f"Invalid value {value!r} for key {make_full_name(key)!r}: {err}")
config[key] = value
config._set_meta( # pylint: disable=protected-access
@@ -179,6 +179,6 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, .
elif isinstance(scheme[key], dict):
config[key] = make_config(raw.get(key, {}), scheme[key], make_full_key(key))
else:
- raise RuntimeError("Incorrect scheme definition for key %r:"
- " the value is %r, not dict() or Option()" % (make_full_name(key), type(scheme[key])))
+ raise RuntimeError(f"Incorrect scheme definition for key {make_full_name(key)!r}:"
+ f" the value is {type(scheme[key])!r}, not dict() or Option()")
return config
diff --git a/kvmd/yamlconf/dumper.py b/kvmd/yamlconf/dumper.py
index 6dac6baa..b0a561e5 100644
--- a/kvmd/yamlconf/dumper.py
+++ b/kvmd/yamlconf/dumper.py
@@ -39,7 +39,8 @@ def make_config_dump(config: Section, indent: int=4) -> str:
def _inner_make_dump(config: Section, indent: int, _level: int=0) -> Generator[str, None, None]:
for (key, value) in sorted(config.items(), key=operator.itemgetter(0)):
if isinstance(value, Section):
- yield "%s%s:" % (" " * indent * _level, key)
+ prefix = " " * indent * _level
+ yield f"{prefix}{key}:"
yield from _inner_make_dump(value, indent, _level + 1)
yield ""
else:
@@ -66,7 +67,7 @@ def _make_yaml_kv(key: str, value: Any, indent: int, level: int, comment: str=""
prefix = " " * indent * level
if commented:
prefix = prefix + "# "
- text = textwrap.indent("%s:%s" % (key, text), prefix=prefix)
+ text = textwrap.indent(f"{key}:{text}", prefix=prefix)
if comment:
lines = text.split("\n")
diff --git a/kvmd/yamlconf/loader.py b/kvmd/yamlconf/loader.py
index c72a45dc..437d9c4a 100644
--- a/kvmd/yamlconf/loader.py
+++ b/kvmd/yamlconf/loader.py
@@ -36,7 +36,7 @@ def load_yaml_file(path: str) -> Any:
return yaml.load(yaml_file, _YamlLoader)
except Exception:
# Reraise internal exception as standard ValueError and show the incorrect file
- raise ValueError("Incorrect YAML syntax in file %r" % (path))
+ raise ValueError(f"Incorrect YAML syntax in file {path!r}")
class _YamlLoader(yaml.SafeLoader):