|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +from flake8.formatting.default import Default |
| 4 | +from flake8.options.manager import OptionManager |
| 5 | +from flake8.violation import Violation |
| 6 | + |
| 7 | + |
| 8 | +class Formatter(Default): |
| 9 | + def format(self, error: Violation): |
| 10 | + custom_error = self.get_custom_error(error.code) |
| 11 | + if custom_error: |
| 12 | + # interpolate the original error message in case it is specified in |
| 13 | + # the custom message. |
| 14 | + custom_text = custom_error.text.format(original_message=error.text) |
| 15 | + new_error = Violation( |
| 16 | + error.code, |
| 17 | + error.filename, |
| 18 | + error.line_number, |
| 19 | + error.column_number, |
| 20 | + custom_text, |
| 21 | + error.physical_line, |
| 22 | + ) |
| 23 | + return super().format(new_error) |
| 24 | + else: |
| 25 | + return super().format(error) |
| 26 | + |
| 27 | + def get_custom_error(self, code: str) -> "CustomError": |
| 28 | + for custom_error in self.options.error_messages: |
| 29 | + if code.startswith(custom_error.code): |
| 30 | + return custom_error |
| 31 | + |
| 32 | + def after_init(self): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class Plugin: |
| 37 | + name = "flake8-custom-error-messages" |
| 38 | + version = "0.0.1" |
| 39 | + |
| 40 | + def __init__(self, tree): |
| 41 | + self.tree = tree |
| 42 | + |
| 43 | + def run(self): |
| 44 | + return # because the package doesn't add any rules |
| 45 | + # yield added so python considers the run method as returning a generator |
| 46 | + # even though it's unreachable |
| 47 | + yield # noqa |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def add_options(cls, options_manager: OptionManager): |
| 51 | + options_manager.add_option( |
| 52 | + "--custom-error-messages", |
| 53 | + dest="error_messages", |
| 54 | + type=str, |
| 55 | + parse_from_config=True, |
| 56 | + help=( |
| 57 | + "Specify a custom error message for an error code. The message " |
| 58 | + "should start with the error code in question followed by a " |
| 59 | + "space and then the custom message." |
| 60 | + ), |
| 61 | + metavar="MSG", |
| 62 | + nargs="+", |
| 63 | + ) |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def parse_options(cls, options): |
| 67 | + if not options.error_messages: |
| 68 | + msgs = [] |
| 69 | + elif isinstance(options.error_messages, str): |
| 70 | + # parsed from the config file, so we need to convert it to a list |
| 71 | + # flake8 does not support parsing values containing spaces from config |
| 72 | + # files |
| 73 | + msgs = [ |
| 74 | + line.strip() |
| 75 | + for line in options.error_messages.splitlines() |
| 76 | + if line.strip() |
| 77 | + ] |
| 78 | + else: |
| 79 | + # when parsed from the command line, the value is already a list |
| 80 | + msgs = options.error_messages |
| 81 | + |
| 82 | + # split the error code from the custom error message |
| 83 | + errors = [] |
| 84 | + for msg in msgs: |
| 85 | + code = msg.split()[0] |
| 86 | + text = msg.replace(code, "", 1).strip() |
| 87 | + errors.append(CustomError(code, text)) |
| 88 | + cls.error_messages = errors |
| 89 | + |
| 90 | + |
| 91 | +@dataclass |
| 92 | +class CustomError: |
| 93 | + code: str |
| 94 | + text: str |
0 commit comments