Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def file_field_pair(file_name: str, base_file_class: Type[NitpickPlugin]) -> Dict[str, fields.Field]:
"""Return a schema field with info from a config file class."""
valid_toml_key = TOMLFormat.group_name_for(file_name)
unique_file_name_with_underscore = slugify(file_name, separator="_")
kwargs = {"data_key": valid_toml_key}
if base_file_class.validation_schema:
field = fields.Nested(base_file_class.validation_schema, **kwargs)
else:
# For default files (pyproject.toml, setup.cfg...), there is no strict schema;
# it can be anything they allow.
# It's out of Nitpick's scope to validate those files.
field = fields.Dict(fields.String, **kwargs)
return {unique_file_name_with_underscore: field}
TEXT_FILE_RTFD_PAGE = "config_files.html#text-files"
class TextItemSchema(Schema):
"""Validation schema for the object inside ``contains``."""
error_messages = {"unknown": help_message("Unknown configuration", TEXT_FILE_RTFD_PAGE)}
line = fields.NonEmptyString()
class TextSchema(Schema):
"""Validation schema for the text file TOML configuration."""
error_messages = {"unknown": help_message("Unknown configuration", TEXT_FILE_RTFD_PAGE)}
contains = fields.List(fields.Nested(TextItemSchema))
class TextPlugin(NitpickPlugin):
"""Checker for text files.
To check if ``some.txt`` file contains the lines ``abc`` and ``def`` (in any order):
.. code-block:: toml
[["some.txt".contains]]
line = "abc"
[["some.txt".contains]]
line = "def"
"""
"""Validation schema for the ``[nitpick.files]`` section on the style file."""
error_messages = {"unknown": help_message("Unknown file", "nitpick_section.html#nitpick-files")}
absent = fields.Dict(fields.NonEmptyString, fields.String())
present = fields.Dict(fields.NonEmptyString, fields.String())
# TODO: load this schema dynamically, then add this next field setup_cfg
setup_cfg = fields.Nested(SetupCfgSchema, data_key=SetupCfgPlugin.file_name)
class NitpickSectionSchema(BaseNitpickSchema):
"""Validation schema for the ``[nitpick]`` section on the style file."""
minimum_version = fields.NonEmptyString()
styles = fields.Nested(NitpickStylesSectionSchema)
files = fields.Nested(NitpickFilesSectionSchema)
class BaseStyleSchema(Schema):
"""Base validation schema for style files. Dynamic fields will be added to it later."""
error_messages = {"unknown": help_message("Unknown file", "config_files.html")}
nitpick = fields.Nested(NitpickSectionSchema)
class NitpickSectionSchema(BaseNitpickSchema):
"""Validation schema for the ``[nitpick]`` section on the style file."""
minimum_version = fields.NonEmptyString()
styles = fields.Nested(NitpickStylesSectionSchema)
files = fields.Nested(NitpickFilesSectionSchema)
class BaseStyleSchema(Schema):
"""Base validation schema for style files. Dynamic fields will be added to it later."""
error_messages = {"unknown": help_message("Unknown file", "config_files.html")}
nitpick = fields.Nested(NitpickSectionSchema)
"""Validation schema for setup.cfg."""
error_messages = {"unknown": help_message("Unknown configuration", "nitpick_section.html#comma-separated-values")}
comma_separated_values = fields.List(fields.String(validate=fields.validate_section_dot_field))
class NitpickFilesSectionSchema(BaseNitpickSchema):
"""Validation schema for the ``[nitpick.files]`` section on the style file."""
error_messages = {"unknown": help_message("Unknown file", "nitpick_section.html#nitpick-files")}
absent = fields.Dict(fields.NonEmptyString, fields.String())
present = fields.Dict(fields.NonEmptyString, fields.String())
# TODO: load this schema dynamically, then add this next field setup_cfg
setup_cfg = fields.Nested(SetupCfgSchema, data_key=SetupCfgPlugin.file_name)
class NitpickSectionSchema(BaseNitpickSchema):
"""Validation schema for the ``[nitpick]`` section on the style file."""
minimum_version = fields.NonEmptyString()
styles = fields.Nested(NitpickStylesSectionSchema)
files = fields.Nested(NitpickFilesSectionSchema)
class BaseStyleSchema(Schema):
"""Base validation schema for style files. Dynamic fields will be added to it later."""
error_messages = {"unknown": help_message("Unknown file", "config_files.html")}
nitpick = fields.Nested(NitpickSectionSchema)
class NitpickFilesSectionSchema(BaseNitpickSchema):
"""Validation schema for the ``[nitpick.files]`` section on the style file."""
error_messages = {"unknown": help_message("Unknown file", "nitpick_section.html#nitpick-files")}
absent = fields.Dict(fields.NonEmptyString, fields.String())
present = fields.Dict(fields.NonEmptyString, fields.String())
# TODO: load this schema dynamically, then add this next field setup_cfg
setup_cfg = fields.Nested(SetupCfgSchema, data_key=SetupCfgPlugin.file_name)
class NitpickSectionSchema(BaseNitpickSchema):
"""Validation schema for the ``[nitpick]`` section on the style file."""
minimum_version = fields.NonEmptyString()
styles = fields.Nested(NitpickStylesSectionSchema)
files = fields.Nested(NitpickFilesSectionSchema)
class BaseStyleSchema(Schema):
"""Base validation schema for style files. Dynamic fields will be added to it later."""
error_messages = {"unknown": help_message("Unknown file", "config_files.html")}
nitpick = fields.Nested(NitpickSectionSchema)