~ubuntu-branches/ubuntu/hardy/mako/hardy-backports

« back to all changes in this revision

Viewing changes to lib/mako/util.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-09-09 02:09:15 UTC
  • mfrom: (5.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080909020915-y0zgvojsponlnwx8
Tags: 0.2.2-1~hardy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# util.py
2
 
# Copyright (C) 2006, 2007 Michael Bayer mike_mp@zzzcomputing.com
 
2
# Copyright (C) 2006, 2007, 2008 Michael Bayer mike_mp@zzzcomputing.com
3
3
#
4
4
# This module is part of Mako and is released under
5
5
# the MIT License: http://www.opensource.org/licenses/mit-license.php
6
6
 
 
7
import sys
7
8
try:
8
9
    Set = set
9
10
except:
13
14
try:
14
15
    from cStringIO import StringIO
15
16
except:
16
 
   from StringIO import StringIO
 
17
    from StringIO import StringIO
17
18
 
18
19
import weakref, os, time
19
20
 
20
21
try:
21
 
   import threading
22
 
   import thread
 
22
    import threading
 
23
    import thread
23
24
except ImportError:
24
 
   import dummy_threading as threading
25
 
   import dummy_thread as thread
 
25
    import dummy_threading as threading
 
26
    import dummy_thread as thread
26
27
 
 
28
if sys.platform.startswith('win') or sys.platform.startswith('java'):
 
29
    time_func = time.clock
 
30
else:
 
31
    time_func = time.time 
 
32
   
27
33
def verify_directory(dir):
28
 
   """create and/or verify a filesystem directory."""
29
 
   tries = 0
30
 
   while not os.access(dir, os.F_OK):
31
 
       try:
32
 
           tries += 1
33
 
           os.makedirs(dir, 0750)
34
 
       except:
35
 
           if tries > 5:
36
 
               raise
37
 
  
 
34
    """create and/or verify a filesystem directory."""
 
35
    
 
36
    tries = 0
 
37
    
 
38
    while not os.path.exists(dir):
 
39
        try:
 
40
            tries += 1
 
41
            os.makedirs(dir, 0750)
 
42
        except:
 
43
            if tries > 5:
 
44
                raise
 
45
 
38
46
class SetLikeDict(dict):
39
47
    """a dictionary that has some setlike methods on it"""
40
48
    def union(self, other):
44
52
        x = SetLikeDict(**self)
45
53
        x.update(other)
46
54
        return x
47
 
         
 
55
 
48
56
class FastEncodingBuffer(object):
49
57
    """a very rudimentary buffer that is faster than StringIO, but doesnt crash on unicode data like cStringIO."""
50
 
    def __init__(self, encoding=None, errors='strict'):
 
58
    
 
59
    def __init__(self, encoding=None, errors='strict', unicode=False):
51
60
        self.data = []
52
61
        self.encoding = encoding
 
62
        if unicode:
 
63
            self.delim = u''
 
64
        else:
 
65
            self.delim = ''
 
66
        self.unicode = unicode
53
67
        self.errors = errors
54
 
    def write(self, text):
55
 
        self.data.append(text)
 
68
        self.write = self.data.append
 
69
        
56
70
    def getvalue(self):
57
71
        if self.encoding:
58
 
            return u''.join(self.data).encode(self.encoding, self.errors)
 
72
            return self.delim.join(self.data).encode(self.encoding, self.errors)
59
73
        else:
60
 
            return u''.join(self.data)
 
74
            return self.delim.join(self.data)
61
75
 
62
76
class LRUCache(dict):
63
77
    """A dictionary-like object that stores a limited number of items, discarding
64
78
    lesser used items periodically.
65
 
 
 
79
    
66
80
    this is a rewrite of LRUCache from Myghty to use a periodic timestamp-based
67
81
    paradigm so that synchronization is not really needed.  the size management 
68
82
    is inexact.
69
83
    """
70
 
 
 
84
    
71
85
    class _Item(object):
72
86
        def __init__(self, key, value):
73
87
            self.key = key
74
88
            self.value = value
75
 
            self.timestamp = time.time()
 
89
            self.timestamp = time_func()
76
90
        def __repr__(self):
77
91
            return repr(self.value)
78
 
 
 
92
    
79
93
    def __init__(self, capacity, threshold=.5):
80
94
        self.capacity = capacity
81
95
        self.threshold = threshold
82
 
 
 
96
    
83
97
    def __getitem__(self, key):
84
98
        item = dict.__getitem__(self, key)
85
 
        item.timestamp = time.time()
 
99
        item.timestamp = time_func()
86
100
        return item.value
87
 
 
 
101
    
88
102
    def values(self):
89
103
        return [i.value for i in dict.values(self)]
90
104
    
94
108
        else:
95
109
            self[key] = value
96
110
            return value
97
 
                
 
111
    
98
112
    def __setitem__(self, key, value):
99
113
        item = dict.get(self, key)
100
114
        if item is None:
103
117
        else:
104
118
            item.value = value
105
119
        self._manage_size()
106
 
 
 
120
    
107
121
    def _manage_size(self):
108
122
        while len(self) > self.capacity + self.capacity * self.threshold:
109
123
            bytime = dict.values(self)
115
129
                    # if we couldnt find a key, most likely some other thread broke in 
116
130
                    # on us. loop around and try again
117
131
                    break
 
132
 
 
133
def restore__ast(_ast):
 
134
    """Attempt to restore the required classes to the _ast module if it
 
135
    appears to be missing them
 
136
    """
 
137
    if hasattr(_ast, 'AST'):
 
138
        return
 
139
    _ast.PyCF_ONLY_AST = 2 << 9
 
140
    m = compile("""\
 
141
def foo(): pass
 
142
class Bar(object): pass
 
143
if False: pass
 
144
baz = 'mako'
 
145
1 + 2 - 3 * 4 / 5
 
146
6 // 7 % 8 << 9 >> 10
 
147
11 & 12 ^ 13 | 14
 
148
15 and 16 or 17
 
149
-baz + (not +18) - ~17
 
150
baz and 'foo' or 'bar'
 
151
(mako is baz == baz) is not baz != mako
 
152
mako > baz < mako >= baz <= mako
 
153
mako in baz not in mako""", '<unknown>', 'exec', _ast.PyCF_ONLY_AST)
 
154
    _ast.Module = type(m)
 
155
 
 
156
    for cls in _ast.Module.__mro__:
 
157
        if cls.__name__ == 'mod':
 
158
            _ast.mod = cls
 
159
        elif cls.__name__ == 'AST':
 
160
            _ast.AST = cls
 
161
 
 
162
    _ast.FunctionDef = type(m.body[0])
 
163
    _ast.ClassDef = type(m.body[1])
 
164
    _ast.If = type(m.body[2])
 
165
 
 
166
    _ast.Name = type(m.body[3].targets[0])
 
167
    _ast.Store = type(m.body[3].targets[0].ctx)
 
168
    _ast.Str = type(m.body[3].value)
 
169
 
 
170
    _ast.Sub = type(m.body[4].value.op)
 
171
    _ast.Add = type(m.body[4].value.left.op)
 
172
    _ast.Div = type(m.body[4].value.right.op)
 
173
    _ast.Mult = type(m.body[4].value.right.left.op)
 
174
 
 
175
    _ast.RShift = type(m.body[5].value.op)
 
176
    _ast.LShift = type(m.body[5].value.left.op)
 
177
    _ast.Mod = type(m.body[5].value.left.left.op)
 
178
    _ast.FloorDiv = type(m.body[5].value.left.left.left.op)
 
179
 
 
180
    _ast.BitOr = type(m.body[6].value.op)
 
181
    _ast.BitXor = type(m.body[6].value.left.op)
 
182
    _ast.BitAnd = type(m.body[6].value.left.left.op)
 
183
 
 
184
    _ast.Or = type(m.body[7].value.op)
 
185
    _ast.And = type(m.body[7].value.values[0].op)
 
186
 
 
187
    _ast.Invert = type(m.body[8].value.right.op)
 
188
    _ast.Not = type(m.body[8].value.left.right.op)
 
189
    _ast.UAdd = type(m.body[8].value.left.right.operand.op)
 
190
    _ast.USub = type(m.body[8].value.left.left.op)
 
191
 
 
192
    _ast.Or = type(m.body[9].value.op)
 
193
    _ast.And = type(m.body[9].value.values[0].op)
 
194
 
 
195
    _ast.IsNot = type(m.body[10].value.ops[0])
 
196
    _ast.NotEq = type(m.body[10].value.ops[1])
 
197
    _ast.Is = type(m.body[10].value.left.ops[0])
 
198
    _ast.Eq = type(m.body[10].value.left.ops[1])
 
199
 
 
200
    _ast.Gt = type(m.body[11].value.ops[0])
 
201
    _ast.Lt = type(m.body[11].value.ops[1])
 
202
    _ast.GtE = type(m.body[11].value.ops[2])
 
203
    _ast.LtE = type(m.body[11].value.ops[3])
 
204
 
 
205
    _ast.In = type(m.body[12].value.ops[0])
 
206
    _ast.NotIn = type(m.body[12].value.ops[1])