Last active
April 11, 2022 09:15
-
-
Save graingert/500e0c9ec7fe45f91993fe3b73877df3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dataclasses | |
import operator | |
@dataclasses.dataclass | |
class WarningFilter: | |
command: str | |
msg: str | |
cls: str | |
module: str | None = None | |
def format(self): | |
prefix = f"{self.command}:{self.msg}:{self.cls}" | |
return prefix if self.module is None else f"{prefix}:{self.module}" | |
def sort_key(self): | |
return (self.module is not None, self.module, self.cls, self.msg) | |
@classmethod | |
def from_format(cls, fmt): | |
return cls(*fmt.strip().split(":")) | |
def warn_sort(v): | |
return "\n".join( | |
h.format() | |
for h in sorted( | |
(WarningFilter.from_format(x) for x in v.split("\n") if x.strip()), | |
key=operator.methodcaller("sort_key"), | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment