Project

General

Profile

1 73 aaronmk
# String manipulation
2
3 1295 aaronmk
import codecs
4 861 aaronmk
import re
5
6 1232 aaronmk
import util
7
8 1622 aaronmk
##### Parsing
9
10 2932 aaronmk
def concat(str0, str1, max_len): return str0[:max_len-len(str1)]+str1
11
12 1622 aaronmk
def split(sep, str_):
13
    '''Returns [] if str_ == ""'''
14
    if str_ == '': return []
15
    else: return str_.split(sep)
16
17 2221 aaronmk
def remove_prefix(prefix, str_, removed_ref=None):
18
    if removed_ref == None: removed_ref = [False]
19
20
    removed_ref[0] = str_.startswith(prefix)
21
    if removed_ref[0]: return str_[len(prefix):]
22 1622 aaronmk
    else: return str_
23
24 1680 aaronmk
def remove_prefixes(prefixes, str_):
25
    for prefix in prefixes: str_ = remove_prefix(prefix, str_)
26
    return str_
27
28
def with_prefixes(prefixes, str_): return (p+str_ for p in prefixes)
29
30 2221 aaronmk
def remove_suffix(suffix, str_, removed_ref=None):
31
    if removed_ref == None: removed_ref = [False]
32
33
    removed_ref[0] = str_.endswith(suffix)
34
    if removed_ref[0]: return str_[:-len(suffix)]
35 1622 aaronmk
    else: return str_
36
37 1959 aaronmk
def contains_any(haystack, needles):
38
    for needle in needles:
39
        if haystack.find(needle) >= 0: return True
40
    return False
41
42 1622 aaronmk
def overlaps(str0, str1): return str0.find(str1) >= 0 or str1.find(str0) >= 0
43
44 2761 aaronmk
##### Escaping
45
46
def esc_for_mogrify(query):
47
    '''Escapes a query right before being passed to a mogrifying function.'''
48
    return query.replace('%', '%%')
49
50 1401 aaronmk
##### Unicode
51
52 2882 aaronmk
def to_raw_str(str_):
53
    if isinstance(str_, unicode): str_ = str_.encode('utf_8')
54
    return str_
55
56 1295 aaronmk
unicode_reader = codecs.getreader('utf_8')
57
58 73 aaronmk
def to_unicode(str_):
59
    if isinstance(str_, unicode): return str_
60
    encodings = ['utf_8', 'latin_1']
61
    for encoding in encodings:
62
        try: return unicode(str_, encoding)
63
        except UnicodeDecodeError, e: pass
64
    raise AssertionError(encoding+' is not a catch-all encoding')
65 340 aaronmk
66 1232 aaronmk
def ustr(val):
67
    '''Like built-in str() but converts to unicode object'''
68
    if not util.is_str(val): val = str(val)
69
    return to_unicode(val)
70
71 2502 aaronmk
def repr_no_u(value):
72
    '''Like built-in repr() but removes the "u" in `u'...'`'''
73 2582 aaronmk
    return re.sub(r"^u(?=')", r'', repr(value))
74 2502 aaronmk
75 1401 aaronmk
##### Line endings
76
77 1622 aaronmk
def extract_line_ending(line):
78
    '''@return tuple (contents, ending)'''
79
    contents = remove_suffix('\r', remove_suffix('\n', line))
80
    return (contents, line[len(contents):])
81 714 aaronmk
82 1622 aaronmk
def remove_line_ending(line): return extract_line_ending(line)[0]
83
84
def ensure_newl(str_): return remove_line_ending(str_)+'\n'
85
86 856 aaronmk
def is_multiline(str_):
87
    newl_idx = str_.find('\n')
88
    return newl_idx >= 0 and newl_idx != len(str_)-1 # has newline before end
89
90 860 aaronmk
def remove_extra_newl(str_):
91 856 aaronmk
    if is_multiline(str_): return str_
92 2480 aaronmk
    else: return str_.rstrip('\n')
93 856 aaronmk
94 714 aaronmk
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n')
95
96 3255 aaronmk
def join_lines(lines): return ''.join((l+'\n' for l in lines))
97
98 1401 aaronmk
##### Whitespace
99
100 714 aaronmk
def cleanup(str_): return std_newl(str_.strip())
101 861 aaronmk
102 1364 aaronmk
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
103
104 861 aaronmk
def one_line(str_): return re.sub(r'\n *', r' ', cleanup(str_))
105 1761 aaronmk
106
##### Control characters
107
108
def is_ctrl(char):
109
    '''Whether char is a (non-printable) control character'''
110
    return ord(char) < 32 and not char.isspace()
111
112
def strip_ctrl(str_):
113
    '''Strips (non-printable) control characters'''
114
    return ''.join(filter(lambda c: not is_ctrl(c), str_))
115 2471 aaronmk
116 3172 aaronmk
##### Text
117
118
def first_word(str_): return str_.partition(' ')[0]
119
120 2471 aaronmk
##### Formatting
121
122 3466 aaronmk
def indent(str_, level=1, indent_str='    '):
123
    indent_str *= level
124
    return ('\n'.join((indent_str+l for l in str_.rstrip().split('\n'))))+'\n'
125
126 2477 aaronmk
def as_tt(str_): return '@'+str_+'@'
127
128 2475 aaronmk
def as_code(str_, lang=None, multiline=True):
129 2471 aaronmk
    '''Wraps a string in Redmine tags to syntax-highlight it.'''
130 2480 aaronmk
    str_ = '\n'+str_.rstrip('\n')+'\n'
131 2471 aaronmk
    if lang != None: str_ = '<code class="'+lang+'">'+str_+'</code>'
132 2475 aaronmk
    if multiline: str_ = '<pre>'+str_+'</pre>'
133
    return str_
134 2477 aaronmk
135 2504 aaronmk
def as_inline_table(dict_, key_label='Output', value_label='Input', ustr=ustr):
136 2477 aaronmk
    '''Wraps a dict in Redmine tags to format it as a table.'''
137
    str_ = ''
138 2481 aaronmk
    def row(entry): return (': '.join(entry))+'\n'
139 2477 aaronmk
    str_ += row([key_label, value_label])
140 2481 aaronmk
    for entry in dict_.iteritems(): str_ += row([ustr(v) for v in entry])
141
    return '<pre>\n'+str_+'</pre>'
142 2482 aaronmk
143 2504 aaronmk
def as_table(dict_, key_label='Output', value_label='Input', ustr=ustr):
144 2482 aaronmk
    '''Wraps a dict in Redmine tags to format it as a table.'''
145
    str_ = ''
146
    def row(entry): return ('|'.join(['']+entry+['']))+'\n'# '' for outer border
147
    str_ += row([key_label, value_label])
148
    for entry in dict_.iteritems(): str_ += row([as_tt(ustr(v)) for v in entry])
149
    return '\n'+str_+' ' # space protects last \n so blank line ends table