2019-09-13 19:14:31 +00:00
|
|
|
"""Utility."""
|
2022-01-24 04:07:52 +00:00
|
|
|
from functools import wraps, lru_cache
|
2019-09-13 19:14:31 +00:00
|
|
|
import warnings
|
|
|
|
import re
|
2022-01-24 04:07:52 +00:00
|
|
|
from typing import Callable, Any, Optional, Tuple, List
|
2019-09-13 19:14:31 +00:00
|
|
|
|
|
|
|
DEBUG = 0x00001
|
|
|
|
|
|
|
|
RE_PATTERN_LINE_SPLIT = re.compile(r'(?:\r\n|(?!\r\n)[\n\r])|$')
|
|
|
|
|
|
|
|
UC_A = ord('A')
|
|
|
|
UC_Z = ord('Z')
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
@lru_cache(maxsize=512)
|
|
|
|
def lower(string: str) -> str:
|
2019-09-13 19:14:31 +00:00
|
|
|
"""Lower."""
|
|
|
|
|
|
|
|
new_string = []
|
|
|
|
for c in string:
|
|
|
|
o = ord(c)
|
|
|
|
new_string.append(chr(o + 32) if UC_A <= o <= UC_Z else c)
|
|
|
|
return ''.join(new_string)
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class SelectorSyntaxError(Exception):
|
2019-09-13 19:14:31 +00:00
|
|
|
"""Syntax error in a CSS selector."""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(self, msg: str, pattern: Optional[str] = None, index: Optional[int] = None) -> None:
|
2019-09-13 19:14:31 +00:00
|
|
|
"""Initialize."""
|
|
|
|
|
|
|
|
self.line = None
|
|
|
|
self.col = None
|
|
|
|
self.context = None
|
|
|
|
|
|
|
|
if pattern is not None and index is not None:
|
|
|
|
# Format pattern to show line and column position
|
|
|
|
self.context, self.line, self.col = get_pattern_context(pattern, index)
|
|
|
|
msg = '{}\n line {}:\n{}'.format(msg, self.line, self.context)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
super().__init__(msg)
|
2019-09-13 19:14:31 +00:00
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def deprecated(message: str, stacklevel: int = 2) -> Callable[..., Any]: # pragma: no cover
|
2019-09-13 19:14:31 +00:00
|
|
|
"""
|
|
|
|
Raise a `DeprecationWarning` when wrapped function/method is called.
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
Usage:
|
|
|
|
|
|
|
|
@deprecated("This method will be removed in version X; use Y instead.")
|
|
|
|
def some_method()"
|
|
|
|
pass
|
2019-09-13 19:14:31 +00:00
|
|
|
"""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
|
2019-09-13 19:14:31 +00:00
|
|
|
@wraps(func)
|
2022-01-24 04:07:52 +00:00
|
|
|
def _deprecated_func(*args: Any, **kwargs: Any) -> Any:
|
2019-09-13 19:14:31 +00:00
|
|
|
warnings.warn(
|
2022-01-24 04:07:52 +00:00
|
|
|
f"'{func.__name__}' is deprecated. {message}",
|
2019-09-13 19:14:31 +00:00
|
|
|
category=DeprecationWarning,
|
|
|
|
stacklevel=stacklevel
|
|
|
|
)
|
|
|
|
return func(*args, **kwargs)
|
2022-01-24 04:07:52 +00:00
|
|
|
return _deprecated_func
|
|
|
|
return _wrapper
|
2019-09-13 19:14:31 +00:00
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def warn_deprecated(message: str, stacklevel: int = 2) -> None: # pragma: no cover
|
2019-09-13 19:14:31 +00:00
|
|
|
"""Warn deprecated."""
|
|
|
|
|
|
|
|
warnings.warn(
|
|
|
|
message,
|
|
|
|
category=DeprecationWarning,
|
|
|
|
stacklevel=stacklevel
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def get_pattern_context(pattern: str, index: int) -> Tuple[str, int, int]:
|
2019-09-13 19:14:31 +00:00
|
|
|
"""Get the pattern context."""
|
|
|
|
|
|
|
|
last = 0
|
|
|
|
current_line = 1
|
|
|
|
col = 1
|
2022-01-24 04:07:52 +00:00
|
|
|
text = [] # type: List[str]
|
2019-09-13 19:14:31 +00:00
|
|
|
line = 1
|
2022-01-24 04:07:52 +00:00
|
|
|
offset = None # type: Optional[int]
|
2019-09-13 19:14:31 +00:00
|
|
|
|
|
|
|
# Split pattern by newline and handle the text before the newline
|
|
|
|
for m in RE_PATTERN_LINE_SPLIT.finditer(pattern):
|
|
|
|
linetext = pattern[last:m.start(0)]
|
|
|
|
if not len(m.group(0)) and not len(text):
|
|
|
|
indent = ''
|
|
|
|
offset = -1
|
|
|
|
col = index - last + 1
|
|
|
|
elif last <= index < m.end(0):
|
|
|
|
indent = '--> '
|
|
|
|
offset = (-1 if index > m.start(0) else 0) + 3
|
|
|
|
col = index - last + 1
|
|
|
|
else:
|
|
|
|
indent = ' '
|
|
|
|
offset = None
|
|
|
|
if len(text):
|
|
|
|
# Regardless of whether we are presented with `\r\n`, `\r`, or `\n`,
|
|
|
|
# we will render the output with just `\n`. We will still log the column
|
|
|
|
# correctly though.
|
|
|
|
text.append('\n')
|
|
|
|
text.append('{}{}'.format(indent, linetext))
|
|
|
|
if offset is not None:
|
|
|
|
text.append('\n')
|
|
|
|
text.append(' ' * (col + offset) + '^')
|
|
|
|
line = current_line
|
|
|
|
|
|
|
|
current_line += 1
|
|
|
|
last = m.end(0)
|
|
|
|
|
|
|
|
return ''.join(text), line, col
|