Anki/pylib/anki/_vendor/stringcase.py
RumovZ 9dc3cf216a
PEP8 for rest of pylib (#1451)
* PEP8 dbproxy.py

* PEP8 errors.py

* PEP8 httpclient.py

* PEP8 lang.py

* PEP8 latex.py

* Add decorator to deprectate key words

* Make replacement for deprecated attribute optional

* Use new helper `_print_replacement_warning()`

* PEP8 media.py

* PEP8 rsbackend.py

* PEP8 sound.py

* PEP8 stdmodels.py

* PEP8 storage.py

* PEP8 sync.py

* PEP8 tags.py

* PEP8 template.py

* PEP8 types.py

* Fix DeprecatedNamesMixinForModule

The class methods need to be overridden with instance methods, so every
module has its own dicts.

* Use `# pylint: disable=invalid-name` instead of id

* PEP8 utils.py

* Only decorate `__getattr__` with `@no_type_check`

* Fix mypy issue with snakecase

Importing it from `anki._vendor` raises attribute errors.

* Format

* Remove inheritance of DeprecatedNamesMixin

There's almost no shared code now and overriding classmethods with
instance methods raises mypy issues.

* Fix traceback frames of deprecation warnings

* remove fn/TimedLog (dae)

Neither Anki nor add-ons appear to have been using it

* fix some issues with stringcase use (dae)

- the wheel was depending on the PyPI version instead of our vendored
version
- _vendor:stringcase should not have been listed in the anki py_library.
We already include the sources in py_srcs, and need to refer to them
directly. By listing _vendor:stringcase as well, we were making a
top-level stringcase library available, which would have only worked for
distributing because the wheel definition was also incorrect.
- mypy errors are what caused me to mistakenly add the above - they
were because the type: ignore at the top of stringcase.py was causing
mypy to completely ignore the file, so it was not aware of any attributes
it contained.
2021-10-25 14:50:13 +10:00

256 lines
4.7 KiB
Python

# stringcase 1.2.0 with python warning fix applied
# MIT: https://github.com/okunishinishi/python-stringcase
"""
String convert functions
"""
import re
def camelcase(string):
"""Convert string into camel case.
Args:
string: String to convert.
Returns:
string: Camel case string.
"""
string = re.sub(r"\w[\s\W]+\w", "", str(string))
if not string:
return string
return lowercase(string[0]) + re.sub(
r"[\-_\.\s]([a-z])", lambda matched: uppercase(matched.group(1)), string[1:]
)
def capitalcase(string):
"""Convert string into capital case.
First letters will be uppercase.
Args:
string: String to convert.
Returns:
string: Capital case string.
"""
string = str(string)
if not string:
return string
return uppercase(string[0]) + string[1:]
def constcase(string):
"""Convert string into upper snake case.
Join punctuation with underscore and convert letters into uppercase.
Args:
string: String to convert.
Returns:
string: Const cased string.
"""
return uppercase(snakecase(string))
def lowercase(string):
"""Convert string into lower case.
Args:
string: String to convert.
Returns:
string: Lowercase case string.
"""
return str(string).lower()
def pascalcase(string):
"""Convert string into pascal case.
Args:
string: String to convert.
Returns:
string: Pascal case string.
"""
return capitalcase(camelcase(string))
def pathcase(string):
"""Convert string into path case.
Join punctuation with slash.
Args:
string: String to convert.
Returns:
string: Path cased string.
"""
string = snakecase(string)
if not string:
return string
return re.sub(r"_", "/", string)
def backslashcase(string):
"""Convert string into spinal case.
Join punctuation with backslash.
Args:
string: String to convert.
Returns:
string: Spinal cased string.
"""
str1 = re.sub(r"_", r"\\", snakecase(string))
return str1
# return re.sub(r"\\n", "", str1)) # TODO: make regex fot \t ...
def sentencecase(string):
"""Convert string into sentence case.
First letter capped and each punctuations are joined with space.
Args:
string: String to convert.
Returns:
string: Sentence cased string.
"""
joiner = " "
string = re.sub(r"[\-_\.\s]", joiner, str(string))
if not string:
return string
return capitalcase(
trimcase(
re.sub(
r"[A-Z]", lambda matched: joiner + lowercase(matched.group(0)), string
)
)
)
def snakecase(string):
"""Convert string into snake case.
Join punctuation with underscore
Args:
string: String to convert.
Returns:
string: Snake cased string.
"""
string = re.sub(r"[\-\.\s]", "_", str(string))
if not string:
return string
return lowercase(string[0]) + re.sub(
r"[A-Z]", lambda matched: "_" + lowercase(matched.group(0)), string[1:]
)
def spinalcase(string):
"""Convert string into spinal case.
Join punctuation with hyphen.
Args:
string: String to convert.
Returns:
string: Spinal cased string.
"""
return re.sub(r"_", "-", snakecase(string))
def dotcase(string):
"""Convert string into dot case.
Join punctuation with dot.
Args:
string: String to convert.
Returns:
string: Dot cased string.
"""
return re.sub(r"_", ".", snakecase(string))
def titlecase(string):
"""Convert string into sentence case.
First letter capped while each punctuations is capitalsed
and joined with space.
Args:
string: String to convert.
Returns:
string: Title cased string.
"""
return " ".join([capitalcase(word) for word in snakecase(string).split("_")])
def trimcase(string):
"""Convert string into trimmed string.
Args:
string: String to convert.
Returns:
string: Trimmed case string
"""
return str(string).strip()
def uppercase(string):
"""Convert string into upper case.
Args:
string: String to convert.
Returns:
string: Uppercase case string.
"""
return str(string).upper()
def alphanumcase(string):
"""Cuts all non-alphanumeric symbols,
i.e. cuts all expect except 0-9, a-z and A-Z.
Args:
string: String to convert.
Returns:
string: String with cutted non-alphanumeric symbols.
"""
# return filter(str.isalnum, str(string))
return re.sub(r"\W+", "", string)