Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fetch_style_from_url(self, url: str) -> Optional[Path]:
"""Fetch a style file from a URL, saving the contents in the cache dir."""
if NitpickApp.current().offline:
# No style will be fetched in offline mode
return None
if self._first_full_path and not is_url(url):
prefix, rest = self._first_full_path.split(":/")
domain_plus_url = str(rest).strip("/").rstrip("/") + "/" + url
new_url = "{}://{}".format(prefix, domain_plus_url)
else:
new_url = url
parsed_url = list(urlparse(new_url))
if not parsed_url[2].endswith(TOML_EXTENSION):
parsed_url[2] += TOML_EXTENSION
new_url = urlunparse(parsed_url)
if new_url in self._already_included:
def load_fixed_dynamic_classes(cls) -> None:
"""Separate classes with fixed file names from classes with dynamic files names."""
cls.fixed_name_classes = set()
cls.dynamic_name_classes = set()
for plugin_class in NitpickApp.current().plugin_manager.hook.plugin_class():
if plugin_class.file_name:
cls.fixed_name_classes.add(plugin_class)
else:
cls.dynamic_name_classes.add(plugin_class)
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
style_uris = [chosen_styles] if isinstance(chosen_styles, str) else chosen_styles # type: List[str]
for style_uri in style_uris:
style_path = self.get_style_path(style_uri) # type: Optional[Path]
if not style_path:
continue
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)
TOMLFormat.group_name_for(self.file_name), True
) # type: bool
file_exists = self.file_path.exists()
if config_data_exists and not file_exists:
suggestion = self.suggest_initial_contents()
phrases = [" was not found"]
message = NitpickApp.current().config.nitpick_files_section.get(self.file_name)
if message and isinstance(message, str):
phrases.append(message)
if suggestion:
phrases.append("Create it with this content:")
yield self.flake8_error(1, ". ".join(phrases), suggestion)
elif not should_exist and file_exists:
# Only display this message if the style is valid.
if not NitpickApp.current().style_errors:
yield self.flake8_error(2, " should be deleted")
elif file_exists and config_data_exists:
yield from self.check_rules()
"""Validate a style file (TOML) against a Marshmallow schema."""
self.rebuild_dynamic_schema(original_data)
style_errors = self._dynamic_schema_class().validate(original_data)
if style_errors:
has_nitpick_jsonfile_section = style_errors.get(PROJECT_NAME, {}).pop("JSONFile", None)
if has_nitpick_jsonfile_section:
warnings.warn(
"The [nitpick.JSONFile] section is not needed anymore; just declare your JSON files directly",
DeprecationWarning,
)
if not style_errors[PROJECT_NAME]:
style_errors.pop(PROJECT_NAME)
if style_errors:
NitpickApp.current().add_style_error(
style_file_name, "Invalid config:", flatten_marshmallow_errors(style_errors)
)
def check_files(self, present: bool) -> YieldFlake8Error:
"""Check files that should be present or absent."""
key = "present" if present else "absent"
message = "exist" if present else "be deleted"
absent = not present
for file_name, extra_message in NitpickApp.current().config.nitpick_files_section.get(key, {}).items():
file_path = NitpickApp.current().root_dir / file_name # type: Path
exists = file_path.exists()
if (present and exists) or (absent and not exists):
continue
full_message = "File {} should {}".format(file_name, message)
if extra_message:
full_message += ": {}".format(extra_message)
yield self.flake8_error(3 if present else 4, full_message)