Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
error.py329 linesDownload Raw Back to yaml
1 2from __future__ import annotations3 4import warnings5# import textwrap6 7if False:  # MYPY8    from typing import Any, Dict, Optional, List, Text  # NOQA9 10 11__all__ = [12    'FileMark',13    'StringMark',14    'CommentMark',15    'YAMLError',16    'MarkedYAMLError',17    'ReusedAnchorWarning',18    'UnsafeLoaderWarning',19    'MarkedYAMLWarning',20    'MarkedYAMLFutureWarning',21]22 23 24class StreamMark:25    __slots__ = 'name', 'index', 'line', 'column'26 27    def __init__(self, name: Any, index: int, line: int, column: int) -> None:28        self.name = name29        self.index = index30        self.line = line31        self.column = column32 33    def __str__(self) -> Any:34        where = f'  in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}'35        return where36 37    def __eq__(self, other: Any) -> bool:38        if self.line != other.line or self.column != other.column:39            return False40        if self.name != other.name or self.index != other.index:41            return False42        return True43 44    def __ne__(self, other: Any) -> bool:45        return not self.__eq__(other)46 47 48class FileMark(StreamMark):49    __slots__ = ()50 51 52class StringMark(StreamMark):53    __slots__ = 'name', 'index', 'line', 'column', 'buffer', 'pointer'54 55    def __init__(56        self, name: Any, index: int, line: int, column: int, buffer: Any, pointer: Any,57    ) -> None:58        StreamMark.__init__(self, name, index, line, column)59        self.buffer = buffer60        self.pointer = pointer61 62    def get_snippet(self, indent: int = 4, max_length: int = 75) -> Any:63        if self.buffer is None:  # always False64            return None65        head = ""66        start = self.pointer67        while start > 0 and self.buffer[start - 1] not in '\0\r\n\x85\u2028\u2029':68            start -= 169            if self.pointer - start > max_length / 2 - 1:70                head = ' ... '71                start += 572                break73        tail = ""74        end = self.pointer75        while end < len(self.buffer) and self.buffer[end] not in '\0\r\n\x85\u2028\u2029':76            end += 177            if end - self.pointer > max_length / 2 - 1:78                tail = ' ... '79                end -= 580                break81        snippet = self.buffer[start:end]82        caret = '^'83        caret = f'^ (line: {self.line + 1})'84        return (85            ' ' * indent86            + head87            + snippet88            + tail89            + '\n'90            + ' ' * (indent + self.pointer - start + len(head))91            + caret92        )93 94    def __str__(self) -> Any:95        snippet = self.get_snippet()96        where = f'  in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}'97        if snippet is not None:98            where += ':\n' + snippet99        return where100 101    def __repr__(self) -> Any:102        snippet = self.get_snippet()103        where = f'  in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}'104        if snippet is not None:105            where += ':\n' + snippet106        return where107 108 109class CommentMark:110    __slots__ = ('column',)111 112    def __init__(self, column: Any) -> None:113        self.column = column114 115 116class YAMLError(Exception):117    pass118 119 120class MarkedYAMLError(YAMLError):121    def __init__(  # NOQA122        self,123        context: Any = None,124        context_mark: Any = None,125        problem: Any = None,126        problem_mark: Any = None,127        note: Any = None,128        warn: Any = None,129    ) -> None:130        self.context = context131        self.context_mark = context_mark132        self.problem = problem133        self.problem_mark = problem_mark134        self.note = note135        # warn is ignored136 137    def __str__(self) -> Any:138        lines: list[str] = []139        if self.context is not None:140            lines.append(self.context)141        if self.context_mark is not None and (142            self.problem is None143            or self.problem_mark is None144            or self.context_mark.name != self.problem_mark.name145            or self.context_mark.line != self.problem_mark.line146            or self.context_mark.column != self.problem_mark.column147        ):148            lines.append(str(self.context_mark))149        if self.problem is not None:150            lines.append(self.problem)151        if self.problem_mark is not None:152            lines.append(str(self.problem_mark))153        # if self.note is not None and self.note:154        #     note = textwrap.dedent(self.note)155        #     lines.append(note)156        self.check_append(lines, self.note)157        return '\n'.join(lines)158 159    def check_append(self, lines: list[str], val: Optional[str]) -> None:160        if val is None or not val:161            return162        import textwrap163 164        note = textwrap.dedent(val)165        lines.append(note)166 167 168class YAMLStreamError(Exception):169    pass170 171 172class YAMLWarning(Warning):173    pass174 175 176class MarkedYAMLWarning(YAMLWarning):177    def __init__(  # NOQA178        self,179        context: Any = None,180        context_mark: Any = None,181        problem: Any = None,182        problem_mark: Any = None,183        note: Any = None,184        warn: Any = None,185    ) -> None:186        self.context = context187        self.context_mark = context_mark188        self.problem = problem189        self.problem_mark = problem_mark190        self.note = note191        self.warn = warn192 193    def __str__(self) -> Any:194        lines: List[str] = []195        if self.context is not None:196            lines.append(self.context)197        if self.context_mark is not None and (198            self.problem is None199            or self.problem_mark is None200            or self.context_mark.name != self.problem_mark.name201            or self.context_mark.line != self.problem_mark.line202            or self.context_mark.column != self.problem_mark.column203        ):204            lines.append(str(self.context_mark))205        if self.problem is not None:206            lines.append(self.problem)207        if self.problem_mark is not None:208            lines.append(str(self.problem_mark))209        # if self.note is not None and self.note:210        #     note = textwrap.dedent(self.note)211        #     lines.append(note)212        self.check_append(lines, self.note)213        # if self.warn is not None and self.warn:214        #     warn = textwrap.dedent(self.warn)215        #     lines.append(warn)216        self.check_append(lines, self.warn)217        return '\n'.join(lines)218 219    def check_append(self, lines: list[str], val: Optional[str]) -> None:220        if val is None or not val:221            return222        import textwrap223 224        note = textwrap.dedent(val)225        lines.append(note)226 227 228class ReusedAnchorWarning(YAMLWarning):229    pass230 231 232class UnsafeLoaderWarning(YAMLWarning):233    text = """234The default 'Loader' for 'load(stream)' without further arguments can be unsafe.235Use 'load(stream, Loader=ruamel.yaml.Loader)' explicitly if that is OK.236Alternatively include the following in your code:237 238  import warnings239  warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)240 241In most other cases you should consider using 'safe_load(stream)'"""242    pass243 244 245warnings.simplefilter('once', UnsafeLoaderWarning)246 247 248class MantissaNoDotYAML1_1Warning(YAMLWarning):249    def __init__(self, node: Any, flt_str: Any) -> None:  # NOQA250        self.node = node251        self.flt = flt_str252 253    def __str__(self) -> Any:254        line = self.node.start_mark.line255        col = self.node.start_mark.column256        return f"""257In YAML 1.1 floating point values should have a dot ('.') in their mantissa.258See the Floating-Point Language-Independent Type for YAML™ Version 1.1 specification259( http://yaml.org/type/float.html ). This dot is not required for JSON nor for YAML 1.2260 261Correct your float: "{self.flt}" on line: {line}, column: {col}262 263or alternatively include the following in your code:264 265  import warnings266  warnings.simplefilter('ignore', ruamel.yaml.error.MantissaNoDotYAML1_1Warning)267 268"""269 270 271warnings.simplefilter('once', MantissaNoDotYAML1_1Warning)272 273 274class YAMLFutureWarning(Warning):275    pass276 277 278class MarkedYAMLFutureWarning(YAMLFutureWarning):279    def __init__(  # NOQA280        self,281        context: Any = None,282        context_mark: Any = None,283        problem: Any = None,284        problem_mark: Any = None,285        note: Any = None,286        warn: Any = None,287    ) -> None:288        self.context = context289        self.context_mark = context_mark290        self.problem = problem291        self.problem_mark = problem_mark292        self.note = note293        self.warn = warn294 295    def __str__(self) -> Any:296        lines: List[str] = []297        if self.context is not None:298            lines.append(self.context)299 300        if self.context_mark is not None and (301            self.problem is None302            or self.problem_mark is None303            or self.context_mark.name != self.problem_mark.name304            or self.context_mark.line != self.problem_mark.line305            or self.context_mark.column != self.problem_mark.column306        ):307            lines.append(str(self.context_mark))308        if self.problem is not None:309            lines.append(self.problem)310        if self.problem_mark is not None:311            lines.append(str(self.problem_mark))312        # if self.note is not None and self.note:313        #     note = textwrap.dedent(self.note)314        #     lines.append(note)315        self.check_append(lines, self.note)316        # if self.warn is not None and self.warn:317        #     warn = textwrap.dedent(self.warn)318        #     lines.append(warn)319        self.check_append(lines, self.warn)320        return '\n'.join(lines)321 322    def check_append(self, lines: list[str], val: Optional[str]) -> None:323        if val is None or not val:324            return325        import textwrap326 327        note = textwrap.dedent(val)328        lines.append(note)329