Refactor config and toml files read into staticmethods
This commit is contained in:
1 parent
5a6ddd0d63
commit
85189b7c1d
1 file changed
+36
-18
@@ -1,4 +1,5 @@
|
|||||||
import configparser
|
import configparser
|
||||||
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@@ -143,24 +144,11 @@ class Command(BaseCommand):
|
|||||||
else:
|
else:
|
||||||
logging.basicConfig(format="%(message)s")
|
logging.basicConfig(format="%(message)s")
|
||||||
|
|
||||||
config_parser = configparser.ConfigParser()
|
config_options = self.read_config_file(options)
|
||||||
config_parser.read(DEFAULT_CONFIG_FILES, encoding="utf-8")
|
toml_options = self.read_toml_file(options)
|
||||||
for key, value in options.items():
|
for k, v in itertools.chain(config_options.items(), toml_options.items()):
|
||||||
if isinstance(value, bool):
|
if not options[k]:
|
||||||
config_get_fn = config_parser.getboolean
|
options[k] = v
|
||||||
else:
|
|
||||||
config_get_fn = config_parser.get
|
|
||||||
|
|
||||||
config_value = config_get_fn(CONFIG_NAME, key, fallback=None)
|
|
||||||
if config_value and not options[key]:
|
|
||||||
options[key] = config_value
|
|
||||||
|
|
||||||
if os.path.exists(PYPROJECT_TOML):
|
|
||||||
pyproject_toml = toml.load(PYPROJECT_TOML)
|
|
||||||
section = pyproject_toml.get("tool", {}).get(CONFIG_NAME, {})
|
|
||||||
for key in options:
|
|
||||||
if key in section and not options[key]:
|
|
||||||
options[key] = section[key]
|
|
||||||
|
|
||||||
linter = MigrationLinter(
|
linter = MigrationLinter(
|
||||||
settings_path,
|
settings_path,
|
||||||
@@ -190,5 +178,35 @@ class Command(BaseCommand):
|
|||||||
if linter.has_errors:
|
if linter.has_errors:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def read_config_file(options):
|
||||||
|
config_options = dict()
|
||||||
|
|
||||||
|
config_parser = configparser.ConfigParser()
|
||||||
|
config_parser.read(DEFAULT_CONFIG_FILES, encoding="utf-8")
|
||||||
|
for key, value in options.items():
|
||||||
|
if isinstance(value, bool):
|
||||||
|
config_get_fn = config_parser.getboolean
|
||||||
|
else:
|
||||||
|
config_get_fn = config_parser.get
|
||||||
|
|
||||||
|
config_value = config_get_fn(CONFIG_NAME, key, fallback=None)
|
||||||
|
if config_value is not None:
|
||||||
|
config_options[key] = config_value
|
||||||
|
return config_options
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def read_toml_file(options):
|
||||||
|
toml_options = dict()
|
||||||
|
|
||||||
|
if os.path.exists(PYPROJECT_TOML):
|
||||||
|
pyproject_toml = toml.load(PYPROJECT_TOML)
|
||||||
|
section = pyproject_toml.get("tool", {}).get(CONFIG_NAME, {})
|
||||||
|
for key in options:
|
||||||
|
if key in section:
|
||||||
|
toml_options[key] = section[key]
|
||||||
|
|
||||||
|
return toml_options
|
||||||
|
|
||||||
def get_version(self):
|
def get_version(self):
|
||||||
return __version__
|
return __version__
|
||||||
Reference in new issue
Block a user