Example #1
0
    def __call__(self, violation: Violation) -> Violation:
        """Replace with pypandoc."""
        description = pypandoc_convert(
            text=violation.description,
            source_format=self.source_format,
            destination_format=self.destination_format,
        )

        return Violation(
            description=description,
            **violation.dict(exclude={
                'description',
            }),
        )
    def __call__(self, violation: Violation) -> Violation:
        first_line, description = violation.description.split('\n', maxsplit=1)
        first_line = first_line.replace('*', '')

        try:
            code, title = re.match(r'#? *([^:]+): (.+)', first_line).groups()
        except AttributeError:
            raise Exception(violation.description)

        return Violation(
            # To avoid conflicts with flake8-bugbear
            code=code.replace('B', 'S'),
            title=title,
            description=description.lstrip('\n'),
            **violation.dict(exclude={'code', 'title', 'description'}, ),
        )
Example #3
0
def docstring_to_violation(docstring: str) -> Violation:
    """Clean up and parse the docstring into Violation instance."""
    violation = Violation(
        description=docstring,
        code='',
        internal_name='',
        title='',
    )

    violation = flow(
        violation,

        # It appears the .. seealso:: block is invalid and is skipped
        Replace(find='\.\. seealso::', replace='See Also::'),
        Pypandoc(),

        # Convert .. [1] lists to Markdown lists
        Replace(find=r' +.. \[\d+\]', replace='*'),

        # Replace ``` sourceCode yaml with just ```yaml for mkdocs compatibility
        Replace(find=' sourceCode ', replace=''),

        # ```none is not a valid lang specifier.
        Replace(find='```none', replace='```'),
        ExtractCodeAndTitle(),
    )

    return violation
Example #4
0
    def process(self) -> Violation:
        """Process the violation description for sections."""
        description = self._format_example(self.violation.description)

        return Violation(description=description,
                         **self.violation.dict(exclude={
                             'description',
                         }))
Example #5
0
def blacklist_item_to_violation(blacklist) -> Violation:
    """Parse flake8-bandit blacklist object into violations stream."""
    return Violation(
        code=blacklist['id'].replace('B', 'S'),
        title=blacklist['message'],
        description='\n'.join(f' * `{qualname}`'
                              for qualname in blacklist['qualnames']),
    )
Example #6
0
def format_violation_document(violation: Violation) -> Violation:
    """Store violation description into a Markdown file with meta."""
    # Ugly transformations
    description = format_violation_description(
        violation.description,
    )
    violation.description = description

    # Nice transformations
    violation = UnpairedQuote(violation=violation).process()

    violation.description = pypandoc_convert(violation.description)

    # Insert macro links
    violation = WPSConfig(violation=violation).process()
    violation = WPSConstants(violation=violation).process()
    violation = RelatedViolations(violation=violation).process()

    violation = FormatTitle(violation=violation).process()
    violation = FormatSections(violation=violation).process()
    violation = FormatExampleSection(violation=violation).process()

    return violation
Example #7
0
    def process(self) -> Violation:
        """It seems a solitary ` is invalid in ReST: they should go in pairs."""
        description = self.violation.description

        # TODO: file a PR for WPS461.
        description = re.sub(
            ' `([^`])',
            r' ``\g<1>',
            description,
        )

        return Violation(
            description=description,
            **self.violation.dict(exclude={
                'description',
            }),
        )
    def process(self) -> Violation:
        """Process the violation description for links to related violations."""
        self.related_violations = []

        description = re.sub(
            r'`~\.*([^`]+Violation)`',
            self._replace_wps_violation,
            self.violation.description,
        )

        return Violation(
            description=description,
            related_violations=self.related_violations or None,
            **self.violation.dict(exclude={
                'description',
                'related_violations',
            })
        )
Example #9
0
    def process(self) -> Violation:
        """Insert links."""
        description = self.violation.description

        description = re.sub(
            r'`(wemake_python_styleguide\.options\.defaults\.[^`]+)`',
            self._replace_wps_config,
            description,
        )

        return Violation(
            description=description,
            related_configuration_parameters=(
                self.related_configuration_parameters or None),
            **self.violation.dict(exclude={
                'description',
                'related_configuration_parameters',
            }),
        )
Example #10
0
def extract_violations_from_module(module) -> Iterator[Violation]:
    """
    Retrieve Violation objects from given module.

    Idea was sourced from `flakehell`:
        https://github.com/life4/flakehell/blob/master/flakehell/
        _logic/_extractors.py#L389
    """
    for violation_class_name in dir(module):
        if not violation_class_name.endswith('Violation'):
            continue

        violation_class = getattr(module, violation_class_name)
        if not hasattr(violation_class, 'code'):
            continue

        yield Violation(
            code=f'WPS{violation_class.code:03}',
            internal_name=violation_class_name,
            title=violation_class.error_template,
            description=violation_class.__doc__,
        )
Example #11
0
    def process(self) -> Violation:
        """Insert links."""
        description = self.violation.description

        description = re.sub(
            r':py`~(wemake_python_styleguide\.constants\.[^`]+)`',
            self._replace_wps_constant,
            description,
        )

        description = re.sub(
            r'`(wemake_python_styleguide\.constants\.[^`]+)`',
            self._replace_wps_constant,
            description,
        )

        return Violation(
            description=description,
            related_constants=(self.related_constants or None),
            **self.violation.dict(exclude={
                'description',
                'related_constants',
            }),
        )
 def __call__(self, violation: Violation) -> Violation:
     return Violation(
         description=re.sub(self.find, self.replace, violation.description),
         **violation.dict(exclude={'description'}, ),
     )