Skip to content

Commit

Permalink
Show error message for empty entry in config
Browse files Browse the repository at this point in the history
  • Loading branch information
tyru committed Jan 12, 2020
1 parent bae27f7 commit 7758955
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 6 additions & 2 deletions vint/linting/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from vint.linting.linter import Linter
from vint.linting.env import build_environment
from vint.linting.config.config_container import ConfigContainer
from vint.linting.config.config_container import ConfigContainer, ConfigEmptyEntryException
from vint.linting.config.config_cmdargs_source import ConfigCmdargsSource
from vint.linting.config.config_default_source import ConfigDefaultSource
from vint.linting.config.config_global_source import ConfigGlobalSource
Expand Down Expand Up @@ -34,7 +34,11 @@ def start_cli():

_adjust_log_level(env)

config_dict = _build_config_dict(env)
try:
config_dict = _build_config_dict(env)
except ConfigEmptyEntryException as e:
sys.stderr.write("[error] " + str(e) + "\n")
sys.exit(1)
violations = _lint_all(env, config_dict)

parser = _build_arg_parser()
Expand Down
28 changes: 21 additions & 7 deletions vint/linting/config/config_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@
from vint.linting.config.config_source import ConfigSource


def merge_dict_deeply(posterior, prior):
class ConfigEmptyEntryException(BaseException):
def __init__(self, path):
self.path = path

def __str__(self):
return 'empty entry in config: `{path}`'.format(path=self.path)


def merge_dict_deeply(posterior, prior, path=[]):
def try_get(obj, path):
if obj is None:
raise ConfigEmptyEntryException(".".join(path))
return obj

# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
tmp = {}

if posterior is None:
posterior = {}
for key in set(posterior.keys()) | set(prior.keys()):
child_path = path + [key]
if key in prior:
if isinstance(prior[key], dict):
tmp[key] = merge_dict_deeply(posterior.get(key, {}), prior[key])
child_posterior = try_get(posterior.get(key, {}), child_path)
tmp[key] = merge_dict_deeply(child_posterior, prior[key],
child_path)
else:
tmp[key] = prior[key]
tmp[key] = try_get(prior[key], child_path)
else:
if isinstance(posterior[key], dict):
tmp[key] = posterior[key].copy()
tmp[key] = try_get(posterior[key], child_path).copy()
else:
tmp[key] = posterior[key]
tmp[key] = try_get(posterior[key], child_path)

return tmp

Expand Down

0 comments on commit 7758955

Please sign in to comment.