~raj-abhilash1/mailman/py3

« back to all changes in this revision

Viewing changes to src/mailman/utilities/string.py

  • Committer: Abhilash Raj
  • Date: 2014-11-26 12:21:48 UTC
  • mfrom: (7260.1.4 3.0)
  • Revision ID: raj.abhilash1@gmail.com-20141126122148-3dr8vp22vq830i93
rebaseĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
__all__ = [
24
24
    'expand',
25
25
    'oneline',
26
 
    'uncanonstr',
27
 
    'websafe',
28
26
    'wrap',
29
27
    ]
30
28
 
31
29
 
32
 
import cgi
33
30
import logging
34
31
 
35
32
from email.errors import HeaderParseError
36
33
from email.header import decode_header, make_header
37
34
from string import Template, whitespace
38
35
from textwrap import TextWrapper, dedent
39
 
from zope.component import getUtility
40
 
 
41
 
from mailman.interfaces.languages import ILanguageManager
42
36
 
43
37
 
44
38
EMPTYSTRING = ''
92
86
 
93
87
 
94
88
 
95
 
def websafe(s):
96
 
    return cgi.escape(s, quote=True)
97
 
 
98
 
 
99
 
 
100
 
# The opposite of canonstr() -- sorta.  I.e. it attempts to encode s in the
101
 
# charset of the given language, which is the character set that the page will
102
 
# be rendered in, and failing that, replaces non-ASCII characters with their
103
 
# html references.  It always returns a byte string.
104
 
def uncanonstr(s, lang=None):
105
 
    if s is None:
106
 
        s = ''
107
 
    if lang is None:
108
 
        charset = 'us-ascii'
109
 
    else:
110
 
        charset = getUtility(ILanguageManager)[lang].charset
111
 
    # See if the string contains characters only in the desired character
112
 
    # set.  If so, return it unchanged, except for coercing it to a byte
113
 
    # string.
114
 
    try:
115
 
        if isinstance(s, str):
116
 
            return s.encode(charset)
117
 
        else:
118
 
            str(s, charset)
119
 
            return s
120
 
    except UnicodeError:
121
 
        # Nope, it contains funny characters, so html-ref it
122
 
        a = []
123
 
        for c in s:
124
 
            o = ord(c)
125
 
            if o > 127:
126
 
                a.append('&#%3d;' % o)
127
 
            else:
128
 
                a.append(c)
129
 
        # Join characters together and coerce to byte string
130
 
        return str(EMPTYSTRING.join(a))
131
 
 
132
 
 
133
 
 
134
89
def wrap(text, column=70, honor_leading_ws=True):
135
90
    """Wrap and fill the text to the specified column.
136
91