Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate_pyproject_tool_nitpick(self) -> bool:
"""Validate the ``pyroject.toml``'s ``[tool.nitpick]`` section against a Marshmallow schema."""
pyproject_path = NitpickApp.current().root_dir / PyProjectTomlPlugin.file_name # type: Path
if pyproject_path.exists():
self.pyproject_toml = TOMLFormat(path=pyproject_path)
self.tool_nitpick_dict = search_dict(TOOL_NITPICK_JMEX, self.pyproject_toml.as_data, {})
pyproject_errors = ToolNitpickSectionSchema().validate(self.tool_nitpick_dict)
if pyproject_errors:
NitpickApp.current().add_style_error(
PyProjectTomlPlugin.file_name,
"Invalid data in [{}]:".format(TOOL_NITPICK),
flatten_marshmallow_errors(pyproject_errors),
)
return False
return True
if not self.validate_pyproject_tool_nitpick():
# If the project is misconfigured, don't even continue.
return
configured_styles = self.tool_nitpick_dict.get("style", "") # type: StrOrList
style = Style()
style.find_initial_styles(configured_styles)
self.style_dict = style.merge_toml_dict()
if not NitpickApp.current().style_errors:
# Don't show duplicated errors: if there are style errors already, don't validate the merged style.
style.validate_style(MERGED_STYLE_TOML, self.style_dict)
from nitpick.flake8 import NitpickExtension # pylint: disable=import-outside-toplevel
minimum_version = search_dict(NITPICK_MINIMUM_VERSION_JMEX, self.style_dict, None)
if minimum_version and version_to_tuple(NitpickExtension.version) < version_to_tuple(minimum_version):
yield self.flake8_error(
3,
"The style file you're using requires {}>={}".format(PROJECT_NAME, minimum_version)
+ " (you have {}). Please upgrade".format(NitpickExtension.version),
)
self.nitpick_section = self.style_dict.get("nitpick", {})
self.nitpick_files_section = self.nitpick_section.get("files", {})
toml = TOMLFormat(path=style_path)
try:
toml_dict = toml.as_data
except TomlDecodeError as err:
NitpickApp.current().add_style_error(style_path.name, pretty_exception(err, "Invalid TOML"))
# If the TOML itself could not be parsed, we can't go on
return
try:
display_name = str(style_path.relative_to(NitpickApp.current().root_dir))
except ValueError:
display_name = style_uri
self.validate_style(display_name, toml_dict)
self._all_styles.add(toml_dict)
sub_styles = search_dict(NITPICK_STYLES_INCLUDE_JMEX, toml_dict, []) # type: StrOrList
if sub_styles:
self.include_multiple_styles(sub_styles)
# E.g.: setup.cfg, pre-commit, pyproject.toml: files whose names we already know at this point.
for subclass in NitpickPlugin.fixed_name_classes:
new_files_found.update(self.file_field_pair(subclass.file_name, subclass))
else:
handled_tags = {} # type: Dict[str, Type[NitpickPlugin]]
# Data was provided; search it to find new dynamic files to add to the validation schema).
# E.g.: JSON files that were configured on some TOML style file.
for subclass in NitpickPlugin.dynamic_name_classes:
for tag in subclass.identify_tags:
# A tag can only be handled by a single subclass.
# If more than one class handle a tag, the latest one will be the handler.
handled_tags[tag] = subclass
jmex = subclass.get_compiled_jmespath_file_names()
for configured_file_name in search_dict(jmex, data, []):
new_files_found.update(self.file_field_pair(configured_file_name, subclass))
self._find_subclasses(data, handled_tags, new_files_found)
# Only recreate the schema if new fields were found.
if new_files_found:
self._dynamic_schema_class = type("DynamicStyleSchema", (self._dynamic_schema_class,), new_files_found)
def __init__(self, config: JsonDict, file_name: str = None) -> None:
if file_name is not None:
self.file_name = file_name
self.error_prefix = "File {}".format(self.file_name)
self.file_path = NitpickApp.current().root_dir / self.file_name # type: Path
# Configuration for this file as a TOML dict, taken from the style file.
self.file_dict = config or {} # type: JsonDict
# Nitpick configuration for this file as a TOML dict, taken from the style file.
self.nitpick_file_dict = search_dict(
'files."{}"'.format(self.file_name), NitpickApp.current().config.nitpick_section, {}
) # type: JsonDict