Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def format_hook(expected_dict) -> str:
"""Format the hook so it's easy to copy and paste it to the .yaml file: ID goes first, indent with spaces."""
lines = YAMLFormat(data=expected_dict).reformatted
output = [] # type: List[str]
for line in lines.split("\n"):
if line.startswith("id:"):
output.insert(0, " - {}".format(line))
else:
output.append(" {}".format(line))
return "\n".join(output)
def check_repo_block(self, expected_repo_block: OrderedDict) -> YieldFlake8Error:
"""Check a repo with a YAML string configuration."""
expected_hooks = PreCommitHook.get_all_hooks_from(YAMLFormat(string=expected_repo_block.get(KEY_YAML)).as_list)
for unique_key, hook in expected_hooks.items():
if unique_key not in self.actual_hooks:
yield self.flake8_error(
2,
": hook {!r} not found. Use this:".format(hook.hook_id),
YAMLFormat(data=hook.yaml.as_data).reformatted,
)
continue
comparison = YAMLFormat(data=self.actual_hooks[unique_key].single_hook).compare_with_dictdiffer(
hook.single_hook
)
# Display the current revision of the hook
current_revision = comparison.flat_actual.get("rev", None)
revision_message = " (rev: {})".format(current_revision) if current_revision else ""
yield from self.warn_missing_different(comparison, ": hook {!r}{}".format(hook.hook_id, revision_message))
"""Suggest the initial content for this missing file."""
original = dict(self.file_dict).copy()
original_repos = original.pop(KEY_REPOS, [])
suggested = {KEY_REPOS: []} if original_repos else {} # type: Dict[str, Any]
for repo in original_repos:
new_repo = dict(repo)
hooks_or_yaml = repo.get(KEY_HOOKS, repo.get(KEY_YAML, {}))
if KEY_YAML in repo:
repo_list = YAMLFormat(string=hooks_or_yaml).as_list
suggested[KEY_REPOS].extend(repo_list)
else:
# TODO: show a deprecation warning for this case
new_repo[KEY_HOOKS] = YAMLFormat(string=hooks_or_yaml).as_data
suggested[KEY_REPOS].append(new_repo)
suggested.update(original)
return YAMLFormat(data=suggested).reformatted
def check_repo_block(self, expected_repo_block: OrderedDict) -> YieldFlake8Error:
"""Check a repo with a YAML string configuration."""
expected_hooks = PreCommitHook.get_all_hooks_from(YAMLFormat(string=expected_repo_block.get(KEY_YAML)).as_list)
for unique_key, hook in expected_hooks.items():
if unique_key not in self.actual_hooks:
yield self.flake8_error(
2,
": hook {!r} not found. Use this:".format(hook.hook_id),
YAMLFormat(data=hook.yaml.as_data).reformatted,
)
continue
comparison = YAMLFormat(data=self.actual_hooks[unique_key].single_hook).compare_with_dictdiffer(
hook.single_hook
)
# Display the current revision of the hook
current_revision = comparison.flat_actual.get("rev", None)
revision_message = " (rev: {})".format(current_revision) if current_revision else ""
def suggest_initial_contents(self) -> str:
"""Suggest the initial content for this missing file."""
original = dict(self.file_dict).copy()
original_repos = original.pop(KEY_REPOS, [])
suggested = {KEY_REPOS: []} if original_repos else {} # type: Dict[str, Any]
for repo in original_repos:
new_repo = dict(repo)
hooks_or_yaml = repo.get(KEY_HOOKS, repo.get(KEY_YAML, {}))
if KEY_YAML in repo:
repo_list = YAMLFormat(string=hooks_or_yaml).as_list
suggested[KEY_REPOS].extend(repo_list)
else:
# TODO: show a deprecation warning for this case
new_repo[KEY_HOOKS] = YAMLFormat(string=hooks_or_yaml).as_data
suggested[KEY_REPOS].append(new_repo)
suggested.update(original)
return YAMLFormat(data=suggested).reformatted
actual_repo_dict = find_object_by_key(actual, KEY_REPO, repo_name)
if not actual_repo_dict:
yield self.flake8_error(3, ": repo {!r} does not exist under {!r}".format(repo_name, KEY_REPOS))
return
if KEY_HOOKS not in actual_repo_dict:
yield self.flake8_error(4, ": missing {!r} in repo {!r}".format(KEY_HOOKS, repo_name))
return
actual_hooks = actual_repo_dict.get(KEY_HOOKS) or []
yaml_expected_hooks = repo_data.get(KEY_HOOKS)
if not yaml_expected_hooks:
yield self.flake8_error(5, ": style file is missing {!r} in repo {!r}".format(KEY_HOOKS, repo_name))
return
expected_hooks = YAMLFormat(string=yaml_expected_hooks).as_data
for expected_dict in expected_hooks:
hook_id = expected_dict.get(KEY_ID)
if not hook_id:
expected_yaml = self.format_hook(expected_dict)
yield self.flake8_error(6, ": style file is missing {!r} in hook:\n{}".format(KEY_ID, expected_yaml))
continue
actual_dict = find_object_by_key(actual_hooks, KEY_ID, hook_id)
if not actual_dict:
expected_yaml = self.format_hook(expected_dict)
yield self.flake8_error(7, ": missing hook with id {!r}:\n{}".format(hook_id, expected_yaml))
continue