forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
49 lines (34 loc) · 1.23 KB
/
utils.py
File metadata and controls
49 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import re
from keyword import iskeyword
import stringcase
def sanitize(value: str) -> str:
return re.sub(r"[^\w _\-]+", "", value)
def fix_keywords(value: str) -> str:
if iskeyword(value):
return f"{value}_"
return value
def group_title(value: str) -> str:
value = re.sub(r"([A-Z]{2,})([A-Z][a-z]|[ \-_]|$)", lambda m: m.group(1).title() + m.group(2), value.strip())
value = re.sub(r"(^|[ _-])([A-Z])", lambda m: m.group(1) + m.group(2).lower(), value)
return value
def snake_case(value: str) -> str:
base = stringcase.snakecase(group_title(sanitize(value))).split('_')
res = []
prev_short = False
for item in base:
if len(item) == 1 and res:
prev_short = True
res[-1] += item
elif prev_short:
prev_short = False
res[-1] += item
else:
prev_short = False
res.append(item)
return fix_keywords('_'.join(res))
def pascal_case(value: str) -> str:
return fix_keywords(stringcase.pascalcase(sanitize(value)))
def kebab_case(value: str) -> str:
return fix_keywords(stringcase.spinalcase(group_title(sanitize(value))))
def remove_string_escapes(value: str) -> str:
return value.replace('"', r"\"")