add pretty printing for regex patterns

This commit is contained in:
Manuel Barkhau 2020-10-03 16:44:30 +00:00
parent e10f858c40
commit e2b274a7bf
7 changed files with 278 additions and 44 deletions

42
src/pycalver/pysix.py Normal file
View file

@ -0,0 +1,42 @@
import sys
import typing as typ
PY2 = sys.version < "3"
try:
from urllib.parse import quote as py3_stdlib_quote
except ImportError:
from urllib import quote as py2_stdlib_quote # type: ignore
# NOTE (mb 2016-05-23): quote in python2 expects bytes argument.
def quote(
string : str,
safe : str = "/",
encoding: typ.Optional[str] = None,
errors : typ.Optional[str] = None,
) -> str:
if not isinstance(string, str):
errmsg = f"Expected str/unicode but got {type(string)}" # type: ignore
raise TypeError(errmsg)
if encoding is None:
_encoding = "utf-8"
else:
_encoding = encoding
if errors is None:
_errors = "strict"
else:
_errors = errors
if PY2:
data = string.encode(_encoding)
res = py2_stdlib_quote(data, safe=safe.encode(_encoding))
return res.decode(_encoding, errors=_errors)
else:
return py3_stdlib_quote(string, safe=safe, encoding=_encoding, errors=_errors)