-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
57 lines (45 loc) · 1.63 KB
/
utils.py
File metadata and controls
57 lines (45 loc) · 1.63 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
50
51
52
53
54
55
56
57
import datetime
import re
from decimal import Decimal
import six
from slugify import slugify
PROTECTED_TYPES = six.integer_types + (
type(None), float, Decimal, datetime.datetime,
datetime.date, datetime.time)
def camelcase_to_underscore(text):
"""Converts camelcase text to underscore format."""
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def underscore_to_camelcase(text):
"""Converts underscore text to camelcase format."""
text = text.replace('_', ' ').title()
return text.replace(' ', '')
def get_class_name(*args):
"""Generates safe class name based on provided arguments."""
name = '_'.join(args)
name = slugify(name, separator='_')
return underscore_to_camelcase(name)
def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
if isinstance(s, six.text_type):
return s
if strings_only and isinstance(s, PROTECTED_TYPES):
return s
try:
if not isinstance(s, six.string_types):
if six.PY3:
if isinstance(s, bytes):
s = six.text_type(s, encoding, errors)
else:
s = six.text_type(s)
elif hasattr(s, '__unicode__'):
s = six.text_type(s)
else:
s = six.text_type(bytes(s), encoding, errors)
else:
s = s.decode(encoding, errors)
except UnicodeDecodeError:
if not isinstance(s, Exception):
raise
s = ' '.join(force_text(arg, encoding, strings_only, errors)
for arg in s)
return s