Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/soupsieve/util.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 """Utility.""" | |
| 2 from functools import wraps | |
| 3 import warnings | |
| 4 import re | |
| 5 | |
| 6 DEBUG = 0x00001 | |
| 7 | |
| 8 RE_PATTERN_LINE_SPLIT = re.compile(r'(?:\r\n|(?!\r\n)[\n\r])|$') | |
| 9 | |
| 10 LC_A = ord('a') | |
| 11 LC_Z = ord('z') | |
| 12 UC_A = ord('A') | |
| 13 UC_Z = ord('Z') | |
| 14 | |
| 15 | |
| 16 def lower(string): | |
| 17 """Lower.""" | |
| 18 | |
| 19 new_string = [] | |
| 20 for c in string: | |
| 21 o = ord(c) | |
| 22 new_string.append(chr(o + 32) if UC_A <= o <= UC_Z else c) | |
| 23 return ''.join(new_string) | |
| 24 | |
| 25 | |
| 26 def upper(string): # pragma: no cover | |
| 27 """Lower.""" | |
| 28 | |
| 29 new_string = [] | |
| 30 for c in string: | |
| 31 o = ord(c) | |
| 32 new_string.append(chr(o - 32) if LC_A <= o <= LC_Z else c) | |
| 33 return ''.join(new_string) | |
| 34 | |
| 35 | |
| 36 class SelectorSyntaxError(Exception): | |
| 37 """Syntax error in a CSS selector.""" | |
| 38 | |
| 39 def __init__(self, msg, pattern=None, index=None): | |
| 40 """Initialize.""" | |
| 41 | |
| 42 self.line = None | |
| 43 self.col = None | |
| 44 self.context = None | |
| 45 | |
| 46 if pattern is not None and index is not None: | |
| 47 # Format pattern to show line and column position | |
| 48 self.context, self.line, self.col = get_pattern_context(pattern, index) | |
| 49 msg = '{}\n line {}:\n{}'.format(msg, self.line, self.context) | |
| 50 | |
| 51 super(SelectorSyntaxError, self).__init__(msg) | |
| 52 | |
| 53 | |
| 54 def deprecated(message, stacklevel=2): # pragma: no cover | |
| 55 """ | |
| 56 Raise a `DeprecationWarning` when wrapped function/method is called. | |
| 57 | |
| 58 Borrowed from https://stackoverflow.com/a/48632082/866026 | |
| 59 """ | |
| 60 | |
| 61 def _decorator(func): | |
| 62 @wraps(func) | |
| 63 def _func(*args, **kwargs): | |
| 64 warnings.warn( | |
| 65 "'{}' is deprecated. {}".format(func.__name__, message), | |
| 66 category=DeprecationWarning, | |
| 67 stacklevel=stacklevel | |
| 68 ) | |
| 69 return func(*args, **kwargs) | |
| 70 return _func | |
| 71 return _decorator | |
| 72 | |
| 73 | |
| 74 def warn_deprecated(message, stacklevel=2): # pragma: no cover | |
| 75 """Warn deprecated.""" | |
| 76 | |
| 77 warnings.warn( | |
| 78 message, | |
| 79 category=DeprecationWarning, | |
| 80 stacklevel=stacklevel | |
| 81 ) | |
| 82 | |
| 83 | |
| 84 def get_pattern_context(pattern, index): | |
| 85 """Get the pattern context.""" | |
| 86 | |
| 87 last = 0 | |
| 88 current_line = 1 | |
| 89 col = 1 | |
| 90 text = [] | |
| 91 line = 1 | |
| 92 | |
| 93 # Split pattern by newline and handle the text before the newline | |
| 94 for m in RE_PATTERN_LINE_SPLIT.finditer(pattern): | |
| 95 linetext = pattern[last:m.start(0)] | |
| 96 if not len(m.group(0)) and not len(text): | |
| 97 indent = '' | |
| 98 offset = -1 | |
| 99 col = index - last + 1 | |
| 100 elif last <= index < m.end(0): | |
| 101 indent = '--> ' | |
| 102 offset = (-1 if index > m.start(0) else 0) + 3 | |
| 103 col = index - last + 1 | |
| 104 else: | |
| 105 indent = ' ' | |
| 106 offset = None | |
| 107 if len(text): | |
| 108 # Regardless of whether we are presented with `\r\n`, `\r`, or `\n`, | |
| 109 # we will render the output with just `\n`. We will still log the column | |
| 110 # correctly though. | |
| 111 text.append('\n') | |
| 112 text.append('{}{}'.format(indent, linetext)) | |
| 113 if offset is not None: | |
| 114 text.append('\n') | |
| 115 text.append(' ' * (col + offset) + '^') | |
| 116 line = current_line | |
| 117 | |
| 118 current_line += 1 | |
| 119 last = m.end(0) | |
| 120 | |
| 121 return ''.join(text), line, col |
