Skip to content

Commit 12db3c2

Browse files
Vendor stringcase library (#1727)
- fixes #1655 Commits: - Vendor `stringcase` library - Fix deprecation warning in it - Swap so it is used, remove dependency on actual library --------- Co-authored-by: Pierre Camilleri <[email protected]>
1 parent 6b72909 commit 12db3c2

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

frictionless/helpers/general.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from typing import Any, Dict, Iterator, List, Optional, Tuple, Type, TypeVar, Union
1818
from urllib.parse import parse_qs, urlparse
1919

20-
import stringcase # type: ignore
20+
from ..vendors import stringcase
2121

2222
# General
2323

frictionless/metadata/metadata.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
Union,
2222
)
2323

24-
import stringcase # type: ignore
2524
from typing_extensions import Self
2625

2726
from .. import helpers
2827
from ..exception import FrictionlessException
2928
from ..platform import platform
29+
from ..vendors import stringcase
3030

3131
if TYPE_CHECKING:
3232
from .. import types
@@ -136,7 +136,10 @@ def set_not_defined(self, name: str, value: Any, *, distinct: bool = False) -> N
136136

137137
@classmethod
138138
def validate_descriptor(
139-
cls, descriptor: Union[types.IDescriptor, str], *, basepath: Optional[str] = None
139+
cls,
140+
descriptor: Union[types.IDescriptor, str],
141+
*,
142+
basepath: Optional[str] = None,
140143
) -> Report:
141144
errors = []
142145
timer = helpers.Timer()
@@ -391,7 +394,11 @@ def metadata_validate(
391394

392395
@classmethod
393396
def metadata_import(
394-
cls, descriptor: types.IDescriptor, *, with_basepath: bool = False, **options: Any
397+
cls,
398+
descriptor: types.IDescriptor,
399+
*,
400+
with_basepath: bool = False,
401+
**options: Any,
395402
) -> Self:
396403
merged_options = {}
397404
profile = cls.metadata_ensure_profile()

frictionless/vendors/stringcase.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""
2+
String convert functions
3+
4+
The MIT License (MIT)
5+
6+
Copyright (c) 2015 Taka Okunishi
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
"""
26+
27+
# The "stringcase" package appears to unmaintained
28+
# https://github.com/okunishinishi/python-stringcase/issues/42
29+
# Relevant parts have been vendored here so we can fix warnings
30+
# that will eventually become errors
31+
# Code here was copied from the 1.2.0 version uploaded to PyPI
32+
# https://pypi.org/project/stringcase/1.2.0/
33+
34+
import re
35+
36+
37+
def camelcase(string: str):
38+
"""Convert string into camel case.
39+
40+
Args:
41+
string: String to convert.
42+
43+
Returns:
44+
string: Camel case string.
45+
46+
"""
47+
48+
string = re.sub(r"\w[\s\W]+\w", "", str(string))
49+
if not string:
50+
return string
51+
return lowercase(string[0]) + re.sub(
52+
r"[\-_\.\s]([a-z])", lambda matched: uppercase(matched.group(1)), string[1:]
53+
)
54+
55+
56+
def lowercase(string: str):
57+
"""Convert string into lower case.
58+
59+
Args:
60+
string: String to convert.
61+
62+
Returns:
63+
string: Lowercase case string.
64+
65+
"""
66+
67+
return str(string).lower()
68+
69+
70+
def snakecase(string: str):
71+
"""Convert string into snake case.
72+
Join punctuation with underscore
73+
74+
Args:
75+
string: String to convert.
76+
77+
Returns:
78+
string: Snake cased string.
79+
80+
"""
81+
82+
string = re.sub(r"[\-\.\s]", "_", str(string))
83+
if not string:
84+
return string
85+
return lowercase(string[0]) + re.sub(
86+
r"[A-Z]", lambda matched: "_" + lowercase(matched.group(0)), string[1:]
87+
)
88+
89+
90+
def uppercase(string: str):
91+
"""Convert string into upper case.
92+
93+
Args:
94+
string: String to convert.
95+
96+
Returns:
97+
string: Uppercase case string.
98+
99+
"""
100+
101+
return str(string).upper()

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ dependencies = [
4848
"tabulate>=0.8.10",
4949
"jsonschema>=4.20",
5050
"simpleeval>=0.9.11",
51-
"stringcase>=1.2",
5251
"typer>=0.12",
5352
"validators>=0.18",
5453
"python-slugify>=1.2",

0 commit comments

Comments
 (0)