Project

General

Profile

1
# String manipulation
2

    
3
import codecs
4
import re
5

    
6
import util
7

    
8
##### Sentinel values
9

    
10
class NonInternedStr(str):
11
    '''Each instance is unique and does not compare identical with `is`.'''
12
    pass
13

    
14
none_str = NonInternedStr()
15

    
16
##### Parsing
17

    
18
def concat(str0, str1, max_len):
19
    str0, str1 = map(to_raw_str, [str0, str1])
20
    return to_unicode(str0[:max_len-len(str1)]+str1)
21

    
22
def split(sep, str_):
23
    '''Returns [] if str_ == ""'''
24
    if str_ == '': return []
25
    else: return str_.split(sep)
26

    
27
def remove_prefix(prefix, str_, removed_ref=None):
28
    if removed_ref == None: removed_ref = [False]
29
    
30
    removed_ref[0] = str_.startswith(prefix)
31
    if removed_ref[0]: return str_[len(prefix):]
32
    else: return str_
33

    
34
def remove_prefixes(prefixes, str_):
35
    for prefix in prefixes: str_ = remove_prefix(prefix, str_)
36
    return str_
37

    
38
def with_prefixes(prefixes, str_): return (p+str_ for p in prefixes)
39

    
40
def remove_suffix(suffix, str_, removed_ref=None):
41
    if removed_ref == None: removed_ref = [False]
42
    
43
    removed_ref[0] = str_.endswith(suffix)
44
    if removed_ref[0]: return str_[:-len(suffix)]
45
    else: return str_
46

    
47
def contains_any(haystack, needles):
48
    for needle in needles:
49
        if haystack.find(needle) >= 0: return True
50
    return False
51

    
52
def overlaps(str0, str1): return str0.find(str1) >= 0 or str1.find(str0) >= 0
53

    
54
##### Escaping
55

    
56
def esc_for_mogrify(query):
57
    '''Escapes a query right before being passed to a mogrifying function.'''
58
    return query.replace('%', '%%')
59

    
60
##### Unicode
61

    
62
def to_raw_str(str_):
63
    if isinstance(str_, unicode): str_ = str_.encode('utf_8')
64
    return str_
65

    
66
unicode_reader = codecs.getreader('utf_8')
67

    
68
def to_unicode(str_):
69
    if isinstance(str_, unicode): return str_
70
    encodings = ['utf_8', 'latin_1']
71
    for encoding in encodings:
72
        try: return unicode(str_, encoding)
73
        except UnicodeDecodeError, e: pass
74
    raise AssertionError(encoding+' is not a catch-all encoding')
75

    
76
def ustr(value):
77
    '''Like built-in str() but converts to unicode object'''
78
    if util.is_str(value): str_ = value # already a string
79
    elif hasattr(value, '__str__'): str_ = value.__str__()
80
    else: str_ = str(value)
81
    return to_unicode(str_)
82

    
83
def urepr(value):
84
    '''Like built-in repr() but converts to unicode object'''
85
    if hasattr(value, '__repr__'): str_ = value.__repr__()
86
    else: str_ = repr(value)
87
    return to_unicode(str_)
88

    
89
def repr_no_u(value):
90
    '''Like built-in repr() but removes the "u" in `u'...'`'''
91
    return re.sub(r"^u(?=')", r'', urepr(value))
92

    
93
##### Line endings
94

    
95
def extract_line_ending(line):
96
    '''@return tuple (contents, ending)'''
97
    contents = remove_suffix('\r', remove_suffix('\n', line))
98
    return (contents, line[len(contents):])
99

    
100
def remove_line_ending(line): return extract_line_ending(line)[0]
101

    
102
def ensure_newl(str_): return remove_line_ending(str_)+'\n'
103

    
104
def is_multiline(str_):
105
    newl_idx = str_.find('\n')
106
    return newl_idx >= 0 and newl_idx != len(str_)-1 # has newline before end
107

    
108
def remove_extra_newl(str_):
109
    if is_multiline(str_): return str_
110
    else: return str_.rstrip('\n')
111

    
112
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n')
113

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

    
116
##### Whitespace
117

    
118
def cleanup(str_): return std_newl(str_.strip())
119

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

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

    
124
##### Control characters
125

    
126
def is_ctrl(char):
127
    '''Whether char is a (non-printable) control character'''
128
    return ord(char) < 32 and not char.isspace()
129

    
130
def strip_ctrl(str_):
131
    '''Strips (non-printable) control characters'''
132
    return ''.join(filter(lambda c: not is_ctrl(c), str_))
133

    
134
##### Text
135

    
136
def first_word(str_): return str_.partition(' ')[0]
137

    
138
##### Formatting
139

    
140
def indent(str_, level=1, indent_str='    '):
141
    indent_str *= level
142
    return ('\n'.join((indent_str+l for l in str_.rstrip().split('\n'))))+'\n'
143

    
144
def as_tt(str_): return '@'+str_+'@'
145

    
146
def as_code(str_, lang=None, multiline=True):
147
    '''Wraps a string in Redmine tags to syntax-highlight it.'''
148
    str_ = '\n'+str_.rstrip('\n')+'\n'
149
    if lang != None: str_ = '<code class="'+lang+'">'+str_+'</code>'
150
    if multiline: str_ = '<pre>'+str_+'</pre>'
151
    return str_
152

    
153
def as_inline_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'
157
    str_ += row([key_label, value_label])
158
    for entry in dict_.iteritems(): str_ += row([ustr(v) for v in entry])
159
    return '<pre>\n'+str_+'</pre>'
160

    
161
def as_table(dict_, key_label='Output', value_label='Input', ustr=ustr):
162
    '''Wraps a dict in Redmine tags to format it as a table.'''
163
    str_ = ''
164
    def row(entry): return ('|'.join(['']+entry+['']))+'\n'# '' for outer border
165
    str_ += row([key_label, value_label])
166
    for entry in dict_.iteritems(): str_ += row([as_tt(ustr(v)) for v in entry])
167
    return '\n'+str_+' ' # space protects last \n so blank line ends table
(31-31/40)