~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/util/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
    @license: GNU GPL, see COPYING for details.
8
8
"""
9
9
 
10
 
import os, sys, re
 
10
import os, sys, re, random
 
11
 
 
12
# do the pickle magic once here, so we can just import from here:
 
13
# cPickle can encode normal and Unicode strings
 
14
# see http://docs.python.org/lib/node66.html
 
15
try:
 
16
    import cPickle as pickle
 
17
except ImportError:
 
18
    import pickle
 
19
 
 
20
# Set pickle protocol, see http://docs.python.org/lib/node64.html
 
21
PICKLE_PROTOCOL = pickle.HIGHEST_PROTOCOL
 
22
 
11
23
 
12
24
#############################################################################
13
25
### XML helper functions
14
26
#############################################################################
15
27
 
16
28
g_xmlIllegalCharPattern = re.compile('[\x01-\x08\x0B-\x0D\x0E-\x1F\x80-\xFF]')
17
 
g_undoUtf8Pattern       = re.compile('\xC2([^\xC2])')
18
 
g_cdataCharPattern      = re.compile('[&<\'\"]')
19
 
g_textCharPattern       = re.compile('[&<]')
 
29
g_undoUtf8Pattern = re.compile('\xC2([^\xC2])')
 
30
g_cdataCharPattern = re.compile('[&<\'\"]')
 
31
g_textCharPattern = re.compile('[&<]')
20
32
g_charToEntity = {
21
33
    '&': '&amp;',
22
34
    '<': '&lt;',
31
43
    """
32
44
    new_string, num_subst = re.subn(g_undoUtf8Pattern, lambda m: m.group(1), text)
33
45
    new_string, num_subst = re.subn(g_cdataCharPattern, lambda m, d=g_charToEntity: d[m.group()], new_string)
34
 
    new_string, num_subst = re.subn(g_xmlIllegalCharPattern, lambda m: '&#x%02X;'%ord(m.group()), new_string)
 
46
    new_string, num_subst = re.subn(g_xmlIllegalCharPattern, lambda m: '&#x%02X;' % ord(m.group()), new_string)
35
47
    return new_string
36
48
 
37
49
def TranslateText(text):
41
53
    """
42
54
    new_string, num_subst = re.subn(g_undoUtf8Pattern, lambda m: m.group(1), text)
43
55
    new_string, num_subst = re.subn(g_textCharPattern, lambda m, d=g_charToEntity: d[m.group()], new_string)
44
 
    new_string, num_subst = re.subn(g_xmlIllegalCharPattern, lambda m: '&#x%02X;'%ord(m.group()), new_string)
 
56
    new_string, num_subst = re.subn(g_xmlIllegalCharPattern, lambda m: '&#x%02X;' % ord(m.group()), new_string)
45
57
    return new_string
46
58
 
47
59
 
48
60
#############################################################################
49
 
### Exceptions
50
 
#############################################################################
51
 
 
52
 
class MoinMoinNoFooter(Exception):
53
 
    """Raised by actions to prevent output of a page footer (with timings)."""
54
 
    pass
55
 
 
56
 
#############################################################################
57
61
### Misc
58
62
#############################################################################
59
63
 
68
72
    for i in range(len(numbers)-1):
69
73
        if pattern[-1] == ',':
70
74
            pattern = pattern + str(numbers[i])
71
 
            if numbers[i]+1 == numbers[i+1]:
 
75
            if numbers[i] + 1 == numbers[i+1]:
72
76
                pattern = pattern + '-'
73
77
            else:
74
78
                pattern = pattern + ','
75
 
        elif numbers[i]+1 != numbers[i+1]:
 
79
        elif numbers[i] + 1 != numbers[i+1]:
76
80
            pattern = pattern + str(numbers[i]) + ','
77
81
 
78
82
    if pattern[-1] in ',-':
85
89
    from MoinMoin import wikiutil
86
90
 
87
91
    result = '<dt><strong>Form entries</strong></dt>'
88
 
    for k in form.keys():
 
92
    for k in form:
89
93
        v = form.get(k, ["<empty>"])
90
 
        v = "|".join(v) 
 
94
        v = "|".join(v)
91
95
        result = result + '<dd><em>%s</em>=%s</dd>' % (k, wikiutil.escape(v))
92
96
 
93
97
    return result
108
112
 
109
113
    def __init__(self):
110
114
        self.buffer = []
111
 
        
 
115
 
112
116
    def write(self, foo):
113
117
        if not isinstance(foo, unicode):
114
118
            foo = foo.decode("iso-8859-1", "replace")
115
119
        self.buffer.append(foo)
116
 
    
 
120
 
117
121
    def getvalue(self):
118
122
        return u''.join(self.buffer)
119
123
 
120
124
    def close(self):
121
125
        self.buffer = None
122
126
 
 
127
 
 
128
def random_string(length, allowed_chars=None):
 
129
    """ Generate a random string with given length consisting
 
130
        of the given characters.
 
131
 
 
132
        @param length: length of the string
 
133
        @param allowed_chars: string with allowed characters or None
 
134
                              to indicate all 256 byte values should be used
 
135
        @return: random string
 
136
    """
 
137
    if allowed_chars is None:
 
138
        return ''.join([chr(random.randint(0, 255)) for dummy in xrange(length)])
 
139
 
 
140
    return ''.join([random.choice(allowed_chars) for dummy in xrange(length)])