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