File: helpers.py

package info (click to toggle)
python-stone 3.3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,956 kB
  • sloc: python: 21,786; objc: 498; sh: 29; makefile: 11
file content (57 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download
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 re

_split_words_capitalization_re = re.compile(
    '^[a-z0-9]+|[A-Z][a-z0-9]+|[A-Z]+(?=[A-Z][a-z0-9])|[A-Z]+$'
)
_split_words_dashes_re = re.compile('[-_/]+')


def split_words(name):
    """
    Splits name based on capitalization, dashes, and underscores.
        Example: 'GetFile' -> ['Get', 'File']
        Example: 'get_file' -> ['get', 'file']
    """
    all_words = []
    for word in re.split(_split_words_dashes_re, name):
        vals = _split_words_capitalization_re.findall(word)
        if vals:
            all_words.extend(vals)
        else:
            all_words.append(word)
    return all_words


def fmt_camel(name):
    """
    Converts name to lower camel case. Words are identified by capitalization,
    dashes, and underscores.
    """
    words = split_words(name)
    assert len(words) > 0
    first = words.pop(0).lower()
    return first + ''.join([word.capitalize() for word in words])


def fmt_dashes(name):
    """
    Converts name to words separated by dashes. Words are identified by
    capitalization, dashes, and underscores.
    """
    return '-'.join([word.lower() for word in split_words(name)])


def fmt_pascal(name):
    """
    Converts name to pascal case. Words are identified by capitalization,
    dashes, and underscores.
    """
    return ''.join([word.capitalize() for word in split_words(name)])


def fmt_underscores(name):
    """
    Converts name to words separated by underscores. Words are identified by
    capitalization, dashes, and underscores.
    """
    return '_'.join([word.lower() for word in split_words(name)])