File: strformat.py

package info (click to toggle)
linkchecker 7.9-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,216 kB
  • sloc: python: 27,155; lex: 1,141; yacc: 821; makefile: 407; sh: 108; ansic: 95; sql: 19; awk: 4
file content (313 lines) | stat: -rw-r--r-- 9,563 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2000-2012 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Some functions have been taken and adjusted from the quodlibet
# source. Quodlibet is (C) 2004-2005 Joe Wreschnig, Michael Urman
# and licensed under the GNU General Public License version 2.
"""
Various string utility functions. Note that these functions are not
necessarily optimised for large strings, so use with care.
"""

import re
import textwrap
import codecs
import os
import math
import time
import urlparse
import pydoc
from . import i18n


def unicode_safe (s, encoding=i18n.default_encoding, errors='replace'):
    """Get unicode string without raising encoding errors. Unknown
    characters of the given encoding will be ignored.

    @param s: the string to be decoded
    @type s: any object except None
    @return: if s is already unicode, return s unchanged; else return
        decoded unicode string of str(s)
    @rtype: unicode
    """
    assert s is not None, "argument to unicode_safe was None"
    if isinstance(s, unicode):
        # s is already unicode, nothing to do
        return s
    return unicode(str(s), encoding, errors)


def ascii_safe (s):
    """Get ASCII string without raising encoding errors. Unknown
    characters of the given encoding will be ignored.

    @param s: the Unicode string to be encoded
    @type s: unicode or None
    @return: encoded ASCII version of s, or None if s was None
    @rtype: string
    """
    if isinstance(s, unicode):
        s = s.encode('ascii', 'ignore')
    return s


def is_ascii (s):
    """Test if a string can be encoded in ASCII."""
    try:
        s.encode('ascii', 'strict')
        return True
    except (UnicodeEncodeError, UnicodeDecodeError):
        return False


def is_encoding (text):
    """Check if string is a valid encoding."""
    try:
        return codecs.lookup(text)
    except (LookupError, ValueError):
        return False


def url_unicode_split (url):
    """Like urlparse.urlsplit(), but always returning unicode parts."""
    return [unicode_safe(s) for s in urlparse.urlsplit(url)]


def unquote (s, matching=False):
    """Remove leading and ending single and double quotes.
    The quotes need to match if matching is True. Only one quote from each
    end will be stripped.

    @return: if s evaluates to False, return s as is, else return
        string with stripped quotes
    @rtype: unquoted string, or s unchanged if it is evaluting to False
    """
    if not s:
        return s
    if len(s) < 2:
        return s
    if matching:
        if s[0] in ("\"'") and s[0] == s[-1]:
            s = s[1:-1]
    else:
        if s[0] in ("\"'"):
            s = s[1:]
        if s[-1] in ("\"'"):
            s = s[:-1]
    return s


_para_mac = r"(?:%(sep)s)(?:(?:%(sep)s)\s*)+" % {'sep': '\r'}
_para_posix = r"(?:%(sep)s)(?:(?:%(sep)s)\s*)+" % {'sep': '\n'}
_para_win = r"(?:%(sep)s)(?:(?:%(sep)s)\s*)+" % {'sep': '\r\n'}
_para_ro = re.compile("%s|%s|%s" % (_para_mac, _para_posix, _para_win))

def get_paragraphs (text):
    """A new paragraph is considered to start at a line which follows
    one or more blank lines (lines containing nothing or just spaces).
    The first line of the text also starts a paragraph."""
    if not text:
        return []
    return _para_ro.split(text)


def wrap (text, width, **kwargs):
    """Adjust lines of text to be not longer than width. The text will be
    returned unmodified if width <= 0.
    See textwrap.wrap() for a list of supported kwargs.
    Returns text with lines no longer than given width."""
    if width <= 0 or not text:
        return text
    ret = []
    for para in get_paragraphs(text):
        ret.extend(textwrap.wrap(para.strip(), width, **kwargs))
    return os.linesep.join(ret)


def indent (text, indent_string="  "):
    """Indent each line of text with the given indent string."""
    lines = str(text).splitlines()
    return os.linesep.join("%s%s" % (indent_string, x) for x in lines)


def get_line_number (s, index):
    r"""Return the line number of s[index] or zero on errors.
    Lines are assumed to be separated by the ASCII character '\n'."""
    i = 0
    if index < 0:
        return 0
    line = 1
    while i < index:
        if s[i] == '\n':
            line += 1
        i += 1
    return line


def paginate (text):
    """Print text in pages of lines."""
    pydoc.pager(text)


_markup_re = re.compile("<.*?>", re.DOTALL)

def remove_markup (s):
    """Remove all <*> html markup tags from s."""
    mo = _markup_re.search(s)
    while mo:
        s = s[0:mo.start()] + s[mo.end():]
        mo = _markup_re.search(s)
    return s


def strsize (b):
    """Return human representation of bytes b. A negative number of bytes
    raises a value error."""
    if b < 0:
        raise ValueError("Invalid negative byte number")
    if b < 1024:
        return u"%dB" % b
    if b < 1024 * 10:
        return u"%dKB" % (b // 1024)
    if b < 1024 * 1024:
        return u"%.2fKB" % (float(b) / 1024)
    if b < 1024 * 1024 * 10:
        return u"%.2fMB" % (float(b) / (1024*1024))
    if b < 1024 * 1024 * 1024:
        return u"%.1fMB" % (float(b) / (1024*1024))
    if b < 1024 * 1024 * 1024 * 10:
        return u"%.2fGB" % (float(b) / (1024*1024*1024))
    return u"%.1fGB" % (float(b) / (1024*1024*1024))


def strtime (t):
    """Return ISO 8601 formatted time."""
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t)) + \
           strtimezone()


# from quodlibet
def strduration (duration):
    """Turn a time value in seconds into hh:mm:ss or mm:ss."""
    if duration < 0:
        duration = abs(duration)
        prefix = "-"
    else:
        prefix = ""
    duration = math.ceil(duration)
    if duration >= 3600: # 1 hour
        # time, in hours:minutes:seconds
        return "%s%02d:%02d:%02d" % (prefix, duration // 3600,
                                   (duration % 3600) // 60, duration % 60)
    else:
        # time, in minutes:seconds
        return "%s%02d:%02d" % (prefix, duration // 60, duration % 60)


# from quodlibet
def strduration_long (duration, do_translate=True):
    """Turn a time value in seconds into x hours, x minutes, etc."""
    if do_translate:
        # use global translator functions
        global _, _n
    else:
        # do not translate
        _ = lambda x: x
        _n = lambda a, b, n: a if n==1 else b
    if duration < 0:
        duration = abs(duration)
        prefix = "-"
    else:
        prefix = ""
    if duration < 1:
        return _("%(prefix)s%(duration).02f seconds") % \
               {"prefix": prefix, "duration": duration}
    # translation dummies
    _n("%d second", "%d seconds", 1)
    _n("%d minute", "%d minutes", 1)
    _n("%d hour", "%d hours", 1)
    _n("%d day", "%d days", 1)
    _n("%d year", "%d years", 1)
    cutoffs = [
        (60, "%d second", "%d seconds"),
        (60, "%d minute", "%d minutes"),
        (24, "%d hour", "%d hours"),
        (365, "%d day", "%d days"),
        (None, "%d year", "%d years"),
    ]
    time_str = []
    for divisor, single, plural in cutoffs:
        if duration < 1:
            break
        if divisor is None:
            duration, unit = 0, duration
        else:
            duration, unit = divmod(duration, divisor)
        if unit:
            time_str.append(_n(single, plural, unit) % unit)
    time_str.reverse()
    if len(time_str) > 2:
        time_str.pop()
    return "%s%s" % (prefix, ", ".join(time_str))


def strtimezone ():
    """Return timezone info, %z on some platforms, but not supported on all.
    """
    if time.daylight:
        zone = time.altzone
    else:
        zone = time.timezone
    return "%+04d" % (-zone//3600)


def stripurl(s):
    """Remove any lines from string after the first line.
    Also remove whitespace at start and end from given string."""
    if not s:
        return s
    return s.splitlines()[0].strip()


def limit (s, length=72):
    """If the length of the string exceeds the given limit, it will be cut
    off and three dots will be appended.

    @param s: the string to limit
    @type s: string
    @param length: maximum length
    @type length: non-negative integer
    @return: limited string, at most length+3 characters long
    """
    assert length >= 0, "length limit must be a non-negative integer"
    if not s or len(s) <= length:
        return s
    if length == 0:
        return ""
    return "%s..." % s[:length]


def strline (s):
    """Display string representation on one line."""
    return u"`%s'" % unicode(s).replace(u"\n", u"\\n")


def format_feature_warning (**kwargs):
    """Format warning that a module could not be imported and that it should
    be installed for a certain URL.
    """
    return _("Could not import %(module)s for %(feature)s. Install %(module)s from %(url)s to use this feature.") % kwargs