Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def check_rules(self) -> YieldFlake8Error:
"""Check missing key/value pairs in pyproject.toml."""
if NitpickApp.current().config.pyproject_toml:
comparison = NitpickApp.current().config.pyproject_toml.compare_with_flatten(self.file_dict)
yield from self.warn_missing_different(comparison)
def find_initial_styles(self, configured_styles: StrOrList):
"""Find the initial style(s) and include them."""
if configured_styles:
chosen_styles = configured_styles
log_message = "Styles configured in {}: %s".format(PyProjectTomlPlugin.file_name)
else:
paths = climb_directory_tree(NitpickApp.current().root_dir, [NITPICK_STYLE_TOML])
if paths:
chosen_styles = str(sorted(paths)[0])
log_message = "Found style climbing the directory tree: %s"
else:
chosen_styles = self.get_default_style_url()
log_message = "Loading default Nitpick style %s"
LOGGER.info(log_message, chosen_styles)
self.include_multiple_styles(chosen_styles)
"poetry.toml": "Poetry_",
"pre-commit/bash.toml": "Bash_",
"pre-commit/commitlint.toml": "commitlint_",
"pre-commit/general.toml": "pre-commit_ (hooks)",
"pre-commit/main.toml": "pre-commit_ (main)",
"pre-commit/python.toml": "pre-commit_ (Python hooks)",
"pylint.toml": "Pylint_",
"pytest.toml": "pytest_",
"python35-36-37.toml": "Python 3.5, 3.6 or 3.7",
"python35-36-37-38.toml": "Python 3.5, 3.6, 3.7 to 3.8",
"python36-37.toml": "Python 3.6 or 3.7",
"python36.toml": "Python 3.6",
"python37.toml": "Python 3.7",
}
)
app = NitpickApp.create_app()
divider = ".. auto-generated-from-here"
docs_dir = Path(__file__).parent.absolute() # type: Path
styles_dir = docs_dir.parent / "styles" # type: Path
def write_rst(rst_file: Path, blocks: List[str]):
"""Write content to the .rst file."""
old_content = rst_file.read_text()
cut_position = old_content.index(divider)
new_content = old_content[: cut_position + len(divider) + 1]
new_content += "\n".join(blocks)
rst_file.write_text(new_content.strip() + "\n")
click.secho("{} generated".format(rst_file), fg="green")
def check_exists(self) -> YieldFlake8Error:
"""Check if the file should exist."""
config_data_exists = bool(self.file_dict or self.nitpick_file_dict)
should_exist = NitpickApp.current().config.nitpick_files_section.get(
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:
def flake8_error(self, number: int, message: str, suggestion: str = None, add_to_base_number=True) -> Flake8Error:
"""Return a flake8 error as a tuple."""
# pylint: disable=import-outside-toplevel
from nitpick.app import NitpickApp
from nitpick.exceptions import NitpickError
error = NitpickError()
error.error_base_number = self.error_base_number
error.error_prefix = self.error_prefix
error.number = number
error.message = message
if suggestion:
error.suggestion = suggestion
error.add_to_base_number = add_to_base_number
return NitpickApp.as_flake8_warning(error)
def parse_options(option_manager: OptionManager, options, args): # pylint: disable=unused-argument
"""Create the Nitpick app, set logging from the verbose flags, set offline mode.
This function is called only once by flake8, so it's a good place to create the app.
"""
log_mapping = {1: logging.INFO, 2: logging.DEBUG}
logging.basicConfig(level=log_mapping.get(options.verbose, logging.WARNING))
NitpickApp.create_app(offline=bool(options.nitpick_offline or NitpickApp.get_env(NitpickApp.Flags.OFFLINE)))
LOGGER.info("Offline mode: %s", NitpickApp.current().offline)