~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/utils.py

  • Committer: Curtis Hovey
  • Date: 2011-08-12 14:39:51 UTC
  • mto: This revision was merged to the branch mainline in revision 13685.
  • Revision ID: curtis.hovey@canonical.com-20110812143951-74vfvrt37gtt4fz2
Sorted imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
    'file_exists',
19
19
    'iter_list_chunks',
20
20
    'iter_split',
21
 
    'load_bz2_pickle',
22
21
    'obfuscate_email',
23
 
    'obfuscate_structure',
24
22
    're_email_address',
25
23
    'run_capturing_output',
26
 
    'save_bz2_pickle',
27
24
    'synchronize',
28
25
    'text_delta',
29
26
    'traceback_info',
31
28
    'value_string',
32
29
    ]
33
30
 
34
 
import bz2
35
 
import cPickle as pickle
36
31
from datetime import datetime
37
32
from itertools import tee
38
33
import os
39
34
import re
 
35
from StringIO import StringIO
40
36
import string
41
 
from StringIO import StringIO
42
37
import sys
43
38
from textwrap import dedent
44
39
from types import FunctionType
140
135
    I'm amazed this isn't in itertools (mwhudson).
141
136
    """
142
137
    for i in range(0, len(a_list), size):
143
 
        yield a_list[i:i + size]
 
138
        yield a_list[i:i+size]
144
139
 
145
140
 
146
141
def synchronize(source, target, add, remove):
250
245
    then reassemble.
251
246
    """
252
247
    # Make sure there is at least one newline so the split works.
253
 
    first, rest = (s + '\n').split('\n', 1)
 
248
    first, rest = (s+'\n').split('\n', 1)
254
249
    return (first + '\n' + dedent(rest)).strip()
255
250
 
256
251
 
323
318
    """, re.VERBOSE)              # ' <- font-lock turd
324
319
 
325
320
 
326
 
def obfuscate_email(text_to_obfuscate, replacement=None):
 
321
def obfuscate_email(text_to_obfuscate):
327
322
    """Obfuscate an email address.
328
323
 
329
 
    The email address is obfuscated as <email address hidden> by default,
330
 
    or with the given replacement.
 
324
    The email address is obfuscated as <email address hidden>.
331
325
 
332
326
    The pattern used to identify an email address is not 2822. It strives
333
327
    to match any possible email address embedded in the text. For example,
334
328
    mailto:person@domain.dom and http://person:password@domain.dom both
335
329
    match, though the http match is in fact not an email address.
336
330
    """
337
 
    if replacement is None:
338
 
        replacement = '<email address hidden>'
339
331
    text = re_email_address.sub(
340
 
        replacement, text_to_obfuscate)
341
 
    # Avoid doubled angle brackets.
 
332
        r'<email address hidden>', text_to_obfuscate)
342
333
    text = text.replace(
343
334
        "<<email address hidden>>", "<email address hidden>")
344
335
    return text
345
 
 
346
 
 
347
 
def save_bz2_pickle(obj, filename):
348
 
    """Save a bz2 compressed pickle of `obj` to `filename`."""
349
 
    fout = bz2.BZ2File(filename, "w")
350
 
    try:
351
 
        pickle.dump(obj, fout, pickle.HIGHEST_PROTOCOL)
352
 
    finally:
353
 
        fout.close()
354
 
 
355
 
 
356
 
def load_bz2_pickle(filename):
357
 
    """Load and return a bz2 compressed pickle from `filename`."""
358
 
    fin = bz2.BZ2File(filename, "r")
359
 
    try:
360
 
        return pickle.load(fin)
361
 
    finally:
362
 
        fin.close()
363
 
 
364
 
 
365
 
def obfuscate_structure(o):
366
 
    """Obfuscate the strings of a json-serializable structure.
367
 
 
368
 
    Note: tuples are converted to lists because json encoders do not
369
 
    distinguish between lists and tuples.
370
 
 
371
 
    :param o: Any json-serializable object.
372
 
    :return: a possibly-new structure in which all strings, list and tuple
373
 
        elements, and dict keys and values have undergone obfuscate_email
374
 
        recursively.
375
 
    """
376
 
    if isinstance(o, basestring):
377
 
        return obfuscate_email(o)
378
 
    elif isinstance(o, (list, tuple)):
379
 
        return [obfuscate_structure(value) for value in o]
380
 
    elif isinstance(o, (dict)):
381
 
        return dict(
382
 
            (obfuscate_structure(key), obfuscate_structure(value))
383
 
            for key, value in o.iteritems())
384
 
    else:
385
 
        return o