Skip to content

add documentation how to use the annontation bear as a dependency #272

@hwmrocker

Description

@hwmrocker

I don't have the time to do it myself, but i want to give you what i build during the pycon.

from coalib.bears.LocalBear import LocalBear
from bears.general.AnnotationBear import AnnotationBear
from collections import deque
from coalib.results.Diff import Diff
from coalib.results.Result import RESULT_SEVERITY, Result


class UpperCaseBear(LocalBear):

    BEAR_DEPS = {AnnotationBear}

    def run(self, filename, file, dependency_results):
        results = dependency_results[AnnotationBear.name]
        strings = deque()

        for result in results:
            if result.start.line == result.end.line:
                # we only support single line strings
                strings.extend(result.contents["strings"])
        if not strings:
            return
        next_str = strings.popleft()

        for line_number, line in enumerate(file, start=1):
            if line_number < next_str.start.line:
                continue
            while next_str.start.line == line_number:
                s, e = next_str.start.column - 1, next_str.end.column
                word = line[s:e]
                if word.upper() != word:
                    diff = Diff(file)
                    diff.change_line(
                        line_number,
                        line,
                        line[:s] + word.upper() + line[e:])
                    diffs = {filename: diff}
                    yield Result.from_values(
                        origin=self,
                        message="change to upper",
                        file=filename,
                        line=line_number,
                        column=s,
                        end_line=line_number,
                        end_column=e,
                        severity=RESULT_SEVERITY.INFO,
                        diffs=diffs)
                if not strings:
                    break
                next_str = strings.popleft()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions