error.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # coding: utf-8
  2. import warnings
  3. import textwrap
  4. from typing import Any, Dict, Optional, List, Text # NOQA
  5. __all__ = [
  6. 'FileMark',
  7. 'StringMark',
  8. 'CommentMark',
  9. 'YAMLError',
  10. 'MarkedYAMLError',
  11. 'ReusedAnchorWarning',
  12. 'UnsafeLoaderWarning',
  13. 'MarkedYAMLWarning',
  14. 'MarkedYAMLFutureWarning',
  15. ]
  16. class StreamMark:
  17. __slots__ = 'name', 'index', 'line', 'column'
  18. def __init__(self, name: Any, index: int, line: int, column: int) -> None:
  19. self.name = name
  20. self.index = index
  21. self.line = line
  22. self.column = column
  23. def __str__(self) -> Any:
  24. where = f' in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}'
  25. return where
  26. def __eq__(self, other: Any) -> bool:
  27. if self.line != other.line or self.column != other.column:
  28. return False
  29. if self.name != other.name or self.index != other.index:
  30. return False
  31. return True
  32. def __ne__(self, other: Any) -> bool:
  33. return not self.__eq__(other)
  34. class FileMark(StreamMark):
  35. __slots__ = ()
  36. class StringMark(StreamMark):
  37. __slots__ = 'name', 'index', 'line', 'column', 'buffer', 'pointer'
  38. def __init__(
  39. self, name: Any, index: int, line: int, column: int, buffer: Any, pointer: Any,
  40. ) -> None:
  41. StreamMark.__init__(self, name, index, line, column)
  42. self.buffer = buffer
  43. self.pointer = pointer
  44. def get_snippet(self, indent: int = 4, max_length: int = 75) -> Any:
  45. if self.buffer is None: # always False
  46. return None
  47. head = ""
  48. start = self.pointer
  49. while start > 0 and self.buffer[start - 1] not in '\0\r\n\x85\u2028\u2029':
  50. start -= 1
  51. if self.pointer - start > max_length / 2 - 1:
  52. head = ' ... '
  53. start += 5
  54. break
  55. tail = ""
  56. end = self.pointer
  57. while end < len(self.buffer) and self.buffer[end] not in '\0\r\n\x85\u2028\u2029':
  58. end += 1
  59. if end - self.pointer > max_length / 2 - 1:
  60. tail = ' ... '
  61. end -= 5
  62. break
  63. snippet = self.buffer[start:end]
  64. caret = '^'
  65. caret = f'^ (line: {self.line + 1})'
  66. return (
  67. ' ' * indent
  68. + head
  69. + snippet
  70. + tail
  71. + '\n'
  72. + ' ' * (indent + self.pointer - start + len(head))
  73. + caret
  74. )
  75. def __str__(self) -> Any:
  76. snippet = self.get_snippet()
  77. where = f' in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}'
  78. if snippet is not None:
  79. where += ':\n' + snippet
  80. return where
  81. def __repr__(self) -> Any:
  82. snippet = self.get_snippet()
  83. where = f' in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}'
  84. if snippet is not None:
  85. where += ':\n' + snippet
  86. return where
  87. class CommentMark:
  88. __slots__ = ('column',)
  89. def __init__(self, column: Any) -> None:
  90. self.column = column
  91. class YAMLError(Exception):
  92. pass
  93. class MarkedYAMLError(YAMLError):
  94. def __init__(
  95. self,
  96. context: Any = None,
  97. context_mark: Any = None,
  98. problem: Any = None,
  99. problem_mark: Any = None,
  100. note: Any = None,
  101. warn: Any = None,
  102. ) -> None:
  103. self.context = context
  104. self.context_mark = context_mark
  105. self.problem = problem
  106. self.problem_mark = problem_mark
  107. self.note = note
  108. # warn is ignored
  109. def __str__(self) -> Any:
  110. lines: List[str] = []
  111. if self.context is not None:
  112. lines.append(self.context)
  113. if self.context_mark is not None and (
  114. self.problem is None
  115. or self.problem_mark is None
  116. or self.context_mark.name != self.problem_mark.name
  117. or self.context_mark.line != self.problem_mark.line
  118. or self.context_mark.column != self.problem_mark.column
  119. ):
  120. lines.append(str(self.context_mark))
  121. if self.problem is not None:
  122. lines.append(self.problem)
  123. if self.problem_mark is not None:
  124. lines.append(str(self.problem_mark))
  125. if self.note is not None and self.note:
  126. note = textwrap.dedent(self.note)
  127. lines.append(note)
  128. return '\n'.join(lines)
  129. class YAMLStreamError(Exception):
  130. pass
  131. class YAMLWarning(Warning):
  132. pass
  133. class MarkedYAMLWarning(YAMLWarning):
  134. def __init__(
  135. self,
  136. context: Any = None,
  137. context_mark: Any = None,
  138. problem: Any = None,
  139. problem_mark: Any = None,
  140. note: Any = None,
  141. warn: Any = None,
  142. ) -> None:
  143. self.context = context
  144. self.context_mark = context_mark
  145. self.problem = problem
  146. self.problem_mark = problem_mark
  147. self.note = note
  148. self.warn = warn
  149. def __str__(self) -> Any:
  150. lines: List[str] = []
  151. if self.context is not None:
  152. lines.append(self.context)
  153. if self.context_mark is not None and (
  154. self.problem is None
  155. or self.problem_mark is None
  156. or self.context_mark.name != self.problem_mark.name
  157. or self.context_mark.line != self.problem_mark.line
  158. or self.context_mark.column != self.problem_mark.column
  159. ):
  160. lines.append(str(self.context_mark))
  161. if self.problem is not None:
  162. lines.append(self.problem)
  163. if self.problem_mark is not None:
  164. lines.append(str(self.problem_mark))
  165. if self.note is not None and self.note:
  166. note = textwrap.dedent(self.note)
  167. lines.append(note)
  168. if self.warn is not None and self.warn:
  169. warn = textwrap.dedent(self.warn)
  170. lines.append(warn)
  171. return '\n'.join(lines)
  172. class ReusedAnchorWarning(YAMLWarning):
  173. pass
  174. class UnsafeLoaderWarning(YAMLWarning):
  175. text = """
  176. The default 'Loader' for 'load(stream)' without further arguments can be unsafe.
  177. Use 'load(stream, Loader=ruamel.yaml.Loader)' explicitly if that is OK.
  178. Alternatively include the following in your code:
  179. import warnings
  180. warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)
  181. In most other cases you should consider using 'safe_load(stream)'"""
  182. pass
  183. warnings.simplefilter('once', UnsafeLoaderWarning)
  184. class MantissaNoDotYAML1_1Warning(YAMLWarning):
  185. def __init__(self, node: Any, flt_str: Any) -> None:
  186. self.node = node
  187. self.flt = flt_str
  188. def __str__(self) -> Any:
  189. line = self.node.start_mark.line
  190. col = self.node.start_mark.column
  191. return f"""
  192. In YAML 1.1 floating point values should have a dot ('.') in their mantissa.
  193. See the Floating-Point Language-Independent Type for YAML™ Version 1.1 specification
  194. ( http://yaml.org/type/float.html ). This dot is not required for JSON nor for YAML 1.2
  195. Correct your float: "{self.flt}" on line: {line}, column: {col}
  196. or alternatively include the following in your code:
  197. import warnings
  198. warnings.simplefilter('ignore', ruamel.yaml.error.MantissaNoDotYAML1_1Warning)
  199. """
  200. warnings.simplefilter('once', MantissaNoDotYAML1_1Warning)
  201. class YAMLFutureWarning(Warning):
  202. pass
  203. class MarkedYAMLFutureWarning(YAMLFutureWarning):
  204. def __init__(
  205. self,
  206. context: Any = None,
  207. context_mark: Any = None,
  208. problem: Any = None,
  209. problem_mark: Any = None,
  210. note: Any = None,
  211. warn: Any = None,
  212. ) -> None:
  213. self.context = context
  214. self.context_mark = context_mark
  215. self.problem = problem
  216. self.problem_mark = problem_mark
  217. self.note = note
  218. self.warn = warn
  219. def __str__(self) -> Any:
  220. lines: List[str] = []
  221. if self.context is not None:
  222. lines.append(self.context)
  223. if self.context_mark is not None and (
  224. self.problem is None
  225. or self.problem_mark is None
  226. or self.context_mark.name != self.problem_mark.name
  227. or self.context_mark.line != self.problem_mark.line
  228. or self.context_mark.column != self.problem_mark.column
  229. ):
  230. lines.append(str(self.context_mark))
  231. if self.problem is not None:
  232. lines.append(self.problem)
  233. if self.problem_mark is not None:
  234. lines.append(str(self.problem_mark))
  235. if self.note is not None and self.note:
  236. note = textwrap.dedent(self.note)
  237. lines.append(note)
  238. if self.warn is not None and self.warn:
  239. warn = textwrap.dedent(self.warn)
  240. lines.append(warn)
  241. return '\n'.join(lines)