85
57
setattr(self, key, value)
86
58
for key, value in kw.iteritems():
87
59
setattr(self, key, value)
91
for key, value in self.__dict__.iteritems():
92
if key.startswith('_') or (getattr(self.__dict__[key], '__call__', None) is not None):
94
out += '%r => %r, ' % (key, value)
99
62
def clean_revid(revid):
175
125
out.insert(0, -n)
178
# only do this if unicode turns out to be a problem
179
#_BADCHARS_RE = re.compile(ur'[\u007f-\uffff]')
183
clean up a string for html display. expand any tabs, encode any html
184
entities, and replace spaces with ' '. this is primarily for use
185
in displaying monospace text.
187
s = cgi.escape(s.expandtabs())
188
# s = _BADCHARS_RE.sub(lambda x: '&#%d;' % (ord(x.group(0)),), s)
189
s = s.replace(' ', ' ')
193
def fake_permissions(kind, executable):
194
# fake up unix-style permissions given only a "kind" and executable bit
195
if kind == 'directory':
202
def if_present(format, value):
204
format a value using a format string, if the value exists and is not None.
208
return format % value
214
P95_MEG = int(0.9 * MEG)
215
P95_GIG = int(0.9 * GIG)
217
def human_size(size, min_divisor=0):
219
if (size == 0) and (min_divisor == 0):
221
if (size < 512) and (min_divisor == 0):
224
if (size >= P95_GIG) or (min_divisor >= GIG):
226
elif (size >= P95_MEG) or (min_divisor >= MEG):
233
dot = dot * 10 // divisor
240
if (base < 100) and (dot != 0):
241
out += '.%d' % (dot,)
251
def fill_in_navigation(history, navigation):
253
given a navigation block (used by the template for the page header), fill
254
in useful calculated values.
256
navigation.position = history.get_revid_sequence(navigation.revid_list, navigation.revid)
257
if navigation.position is None:
258
navigation.position = 0
259
navigation.count = len(navigation.revid_list)
260
navigation.page_position = navigation.position // navigation.pagesize + 1
261
navigation.page_count = (len(navigation.revid_list) + (navigation.pagesize - 1)) // navigation.pagesize
263
def get_offset(offset):
264
if (navigation.position + offset < 0) or (navigation.position + offset > navigation.count - 1):
266
return navigation.revid_list[navigation.position + offset]
268
navigation.prev_page_revid = get_offset(-1 * navigation.pagesize)
269
navigation.next_page_revid = get_offset(1 * navigation.pagesize)
271
params = { 'file_id': navigation.file_id }
272
if getattr(navigation, 'query', None) is not None:
273
params['q'] = navigation.query
275
params['start_revid'] = navigation.start_revid
277
if navigation.prev_page_revid:
278
navigation.prev_page_url = turbogears.url([ navigation.scan_url, navigation.prev_page_revid ], **params)
279
if navigation.next_page_revid:
280
navigation.next_page_url = turbogears.url([ navigation.scan_url, navigation.next_page_revid ], **params)
283
def log_exception(log):
284
for line in ''.join(traceback.format_exception(*sys.exc_info())).split('\n'):
288
def decorator(unbound):
289
def new_decorator(f):
291
g.__name__ = f.__name__
292
g.__doc__ = f.__doc__
293
g.__dict__.update(f.__dict__)
295
new_decorator.__name__ = unbound.__name__
296
new_decorator.__doc__ = unbound.__doc__
297
new_decorator.__dict__.update(unbound.__dict__)
301
# common threading-lock decorator
302
def with_lock(lockname, debug_name=None):
303
if debug_name is None:
304
debug_name = lockname
306
def _decorator(unbound):
307
def locked(self, *args, **kw):
308
#self.log.debug('-> %r lock %r', id(threading.currentThread()), debug_name)
309
getattr(self, lockname).acquire()
311
return unbound(self, *args, **kw)
313
getattr(self, lockname).release()
314
#self.log.debug('<- %r unlock %r', id(threading.currentThread()), debug_name)