Project

General

Profile

1
# String manipulation
2

    
3
import codecs
4
import re
5

    
6
import util
7

    
8
##### Parsing
9

    
10
def concat(str0, str1, max_len):
11
    str0, str1 = map(to_raw_str, [str0, str1])
12
    return to_unicode(str0[:max_len-len(str1)]+str1)
13

    
14
def split(sep, str_):
15
    '''Returns [] if str_ == ""'''
16
    if str_ == '': return []
17
    else: return str_.split(sep)
18

    
19
def remove_prefix(prefix, str_, removed_ref=None):
20
    if removed_ref == None: removed_ref = [False]
21
    
22
    removed_ref[0] = str_.startswith(prefix)
23
    if removed_ref[0]: return str_[len(prefix):]
24
    else: return str_
25

    
26
def remove_prefixes(prefixes, str_):
27
    for prefix in prefixes: str_ = remove_prefix(prefix, str_)
28
    return str_
29

    
30
def with_prefixes(prefixes, str_): return (p+str_ for p in prefixes)
31

    
32
def remove_suffix(suffix, str_, removed_ref=None):
33
    if removed_ref == None: removed_ref = [False]
34
    
35
    removed_ref[0] = str_.endswith(suffix)
36
    if removed_ref[0]: return str_[:-len(suffix)]
37
    else: return str_
38

    
39
def contains_any(haystack, needles):
40
    for needle in needles:
41
        if haystack.find(needle) >= 0: return True
42
    return False
43

    
44
def overlaps(str0, str1): return str0.find(str1) >= 0 or str1.find(str0) >= 0
45

    
46
##### Escaping
47

    
48
def esc_for_mogrify(query):
49
    '''Escapes a query right before being passed to a mogrifying function.'''
50
    return query.replace('%', '%%')
51

    
52
##### Unicode
53

    
54
def to_raw_str(str_):
55
    if isinstance(str_, unicode): str_ = str_.encode('utf_8')
56
    return str_
57

    
58
unicode_reader = codecs.getreader('utf_8')
59

    
60
def to_unicode(str_):
61
    if isinstance(str_, unicode): return str_
62
    encodings = ['utf_8', 'latin_1']
63
    for encoding in encodings:
64
        try: return unicode(str_, encoding)
65
        except UnicodeDecodeError, e: pass
66
    raise AssertionError(encoding+' is not a catch-all encoding')
67

    
68
def ustr(value):
69
    '''Like built-in str() but converts to unicode object'''
70
    if util.is_str(value): str_ = value # already a string
71
    elif hasattr(value, '__str__'): str_ = value.__str__()
72
    else: str_ = str(value)
73
    return to_unicode(str_)
74

    
75
def urepr(value):
76
    '''Like built-in repr() but converts to unicode object'''
77
    if hasattr(value, '__repr__'): str_ = value.__repr__()
78
    else: str_ = repr(value)
79
    return to_unicode(str_)
80

    
81
def repr_no_u(value):
82
    '''Like built-in repr() but removes the "u" in `u'...'`'''
83
    return re.sub(r"^u(?=')", r'', urepr(value))
84

    
85
##### Line endings
86

    
87
def extract_line_ending(line):
88
    '''@return tuple (contents, ending)'''
89
    contents = remove_suffix('\r', remove_suffix('\n', line))
90
    return (contents, line[len(contents):])
91

    
92
def remove_line_ending(line): return extract_line_ending(line)[0]
93

    
94
def ensure_newl(str_): return remove_line_ending(str_)+'\n'
95

    
96
def is_multiline(str_):
97
    newl_idx = str_.find('\n')
98
    return newl_idx >= 0 and newl_idx != len(str_)-1 # has newline before end
99

    
100
def remove_extra_newl(str_):
101
    if is_multiline(str_): return str_
102
    else: return str_.rstrip('\n')
103

    
104
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n')
105

    
106
def join_lines(lines): return ''.join((l+'\n' for l in lines))
107

    
108
##### Whitespace
109

    
110
def cleanup(str_): return std_newl(str_.strip())
111

    
112
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
113

    
114
def one_line(str_): return re.sub(r'\n *', r' ', cleanup(str_))
115

    
116
##### Control characters
117

    
118
def is_ctrl(char):
119
    '''Whether char is a (non-printable) control character'''
120
    return ord(char) < 32 and not char.isspace()
121

    
122
def strip_ctrl(str_):
123
    '''Strips (non-printable) control characters'''
124
    return ''.join(filter(lambda c: not is_ctrl(c), str_))
125

    
126
##### Text
127

    
128
def first_word(str_): return str_.partition(' ')[0]
129

    
130
##### Formatting
131

    
132
def indent(str_, level=1, indent_str='    '):
133
    indent_str *= level
134
    return ('\n'.join((indent_str+l for l in str_.rstrip().split('\n'))))+'\n'
135

    
136
def as_tt(str_): return '@'+str_+'@'
137

    
138
def as_code(str_, lang=None, multiline=True):
139
    '''Wraps a string in Redmine tags to syntax-highlight it.'''
140
    str_ = '\n'+str_.rstrip('\n')+'\n'
141
    if lang != None: str_ = '<code class="'+lang+'">'+str_+'</code>'
142
    if multiline: str_ = '<pre>'+str_+'</pre>'
143
    return str_
144

    
145
def as_inline_table(dict_, key_label='Output', value_label='Input', ustr=ustr):
146
    '''Wraps a dict in Redmine tags to format it as a table.'''
147
    str_ = ''
148
    def row(entry): return (': '.join(entry))+'\n'
149
    str_ += row([key_label, value_label])
150
    for entry in dict_.iteritems(): str_ += row([ustr(v) for v in entry])
151
    return '<pre>\n'+str_+'</pre>'
152

    
153
def as_table(dict_, key_label='Output', value_label='Input', ustr=ustr):
154
    '''Wraps a dict in Redmine tags to format it as a table.'''
155
    str_ = ''
156
    def row(entry): return ('|'.join(['']+entry+['']))+'\n'# '' for outer border
157
    str_ += row([key_label, value_label])
158
    for entry in dict_.iteritems(): str_ += row([as_tt(ustr(v)) for v in entry])
159
    return '\n'+str_+' ' # space protects last \n so blank line ends table
(31-31/40)