Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/schema_salad/exceptions.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 1 from typing import List, Optional, Sequence, Tuple, Union | |
| 2 | |
| 3 from .sourceline import SourceLine, reflow_all, strip_duplicated_lineno | |
| 4 | |
| 5 | |
| 6 class SchemaSaladException(Exception): | |
| 7 """Base class for all schema-salad exceptions.""" | |
| 8 | |
| 9 def __init__( | |
| 10 self, | |
| 11 msg: str, | |
| 12 sl: Optional[SourceLine] = None, | |
| 13 children: Optional[Sequence["SchemaSaladException"]] = None, | |
| 14 bullet_for_children: str = "", | |
| 15 ) -> None: | |
| 16 super().__init__(msg) | |
| 17 self.message = self.args[0] | |
| 18 self.file = None # type: Optional[str] | |
| 19 self.start = None # type: Optional[Tuple[int, int]] | |
| 20 self.end = None # type: Optional[Tuple[int, int]] | |
| 21 | |
| 22 self.is_warning = False # type: bool | |
| 23 | |
| 24 # It will be set by its parent | |
| 25 self.bullet = "" # type: str | |
| 26 | |
| 27 def simplify(exc: "SchemaSaladException") -> List["SchemaSaladException"]: | |
| 28 return [exc] if len(exc.message) else exc.children | |
| 29 | |
| 30 def with_bullet( | |
| 31 exc: "SchemaSaladException", bullet: str | |
| 32 ) -> "SchemaSaladException": | |
| 33 if exc.bullet == "": | |
| 34 exc.bullet = bullet | |
| 35 return exc | |
| 36 | |
| 37 if children is None: | |
| 38 self.children = [] # type: List["SchemaSaladException"] | |
| 39 elif len(children) <= 1: | |
| 40 self.children = sum((simplify(c) for c in children), []) | |
| 41 else: | |
| 42 self.children = sum( | |
| 43 (simplify(with_bullet(c, bullet_for_children)) for c in children), [] | |
| 44 ) | |
| 45 | |
| 46 self.with_sourceline(sl) | |
| 47 self.propagate_sourceline() | |
| 48 | |
| 49 def propagate_sourceline(self) -> None: | |
| 50 if self.file is None: | |
| 51 return | |
| 52 for c in self.children: | |
| 53 if c.file is None: | |
| 54 c.file = self.file | |
| 55 c.start = self.start | |
| 56 c.end = self.end | |
| 57 c.propagate_sourceline() | |
| 58 | |
| 59 def as_warning(self) -> "SchemaSaladException": | |
| 60 self.is_warning = True | |
| 61 for c in self.children: | |
| 62 c.as_warning() | |
| 63 return self | |
| 64 | |
| 65 def with_sourceline(self, sl: Optional[SourceLine]) -> "SchemaSaladException": | |
| 66 if sl and sl.file(): | |
| 67 self.file = sl.file() | |
| 68 self.start = sl.start() | |
| 69 self.end = sl.end() | |
| 70 else: | |
| 71 self.file = None | |
| 72 self.start = None | |
| 73 self.end = None | |
| 74 return self | |
| 75 | |
| 76 def leaves(self) -> List["SchemaSaladException"]: | |
| 77 if len(self.children): | |
| 78 return sum((c.leaves() for c in self.children), []) | |
| 79 if len(self.message): | |
| 80 return [self] | |
| 81 return [] | |
| 82 | |
| 83 def prefix(self) -> str: | |
| 84 pre = "" # type:str | |
| 85 if self.file: | |
| 86 linecol0 = "" # type: Union[int, str] | |
| 87 linecol1 = "" # type: Union[int, str] | |
| 88 if self.start: | |
| 89 linecol0, linecol1 = self.start | |
| 90 pre = f"{self.file}:{linecol0}:{linecol1}: " | |
| 91 | |
| 92 return pre + "Warning: " if self.is_warning else pre | |
| 93 | |
| 94 def summary(self, level: int = 0, with_bullet: bool = False) -> str: | |
| 95 indent_per_level = 2 | |
| 96 spaces = (level * indent_per_level) * " " | |
| 97 bullet = self.bullet + " " if len(self.bullet) and with_bullet else "" | |
| 98 return f"{self.prefix()}{spaces}{bullet}{self.message}" | |
| 99 | |
| 100 def __str__(self) -> str: | |
| 101 return str(self.pretty_str()) | |
| 102 | |
| 103 def pretty_str(self, level: int = 0) -> str: | |
| 104 messages = len(self.message) | |
| 105 my_summary = [self.summary(level, True)] if messages else [] | |
| 106 next_level = level + 1 if messages else level | |
| 107 | |
| 108 ret = "\n".join( | |
| 109 e for e in my_summary + [c.pretty_str(next_level) for c in self.children] | |
| 110 ) | |
| 111 if level == 0: | |
| 112 return strip_duplicated_lineno(reflow_all(ret)) | |
| 113 else: | |
| 114 return ret | |
| 115 | |
| 116 | |
| 117 class SchemaException(SchemaSaladException): | |
| 118 """Indicates error with the provided schema definition.""" | |
| 119 | |
| 120 | |
| 121 class ValidationException(SchemaSaladException): | |
| 122 """Indicates error with document against the provided schema.""" | |
| 123 | |
| 124 | |
| 125 class ClassValidationException(ValidationException): | |
| 126 pass | |
| 127 | |
| 128 | |
| 129 def to_one_line_messages(exc: SchemaSaladException) -> str: | |
| 130 return "\n".join(c.summary() for c in exc.leaves()) |
