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