mirror of
https://github.com/TECHNOFAB11/bumpver.git
synced 2025-12-17 08:43:50 +01:00
add pretty printing for regex patterns
This commit is contained in:
parent
e10f858c40
commit
e2b274a7bf
7 changed files with 278 additions and 44 deletions
42
src/pycalver/pysix.py
Normal file
42
src/pycalver/pysix.py
Normal 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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue