forked from mozilla/zamboni
/
__init__.py
83 lines (63 loc) · 2.33 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Miscellaneous helpers that make Django compatible with AMO.
"""
import re
import threading
import commonware.log
from product_details import product_details
from constants.applications import *
from constants.base import *
from constants.licenses import *
from constants.platforms import *
from .log import (LOG, LOG_BY_ID, LOG_ADMINS, LOG_EDITORS,
LOG_HIDE_DEVELOPER, LOG_KEEP, LOG_REVIEW_QUEUE,
LOG_REVIEW_EMAIL_USER, log)
logger_log = commonware.log.getLogger('z.amo')
_locals = threading.local()
_locals.user = None
def get_user():
return _locals.user
def set_user(user):
_locals.user = user
def cached_property(*args, **kw):
# Handles invocation as a direct decorator or
# with intermediate keyword arguments.
if args: # @cached_property
return CachedProperty(args[0])
else: # @cached_property(name=..., writable=...)
return lambda f: CachedProperty(f, **kw)
class CachedProperty(object):
"""
A decorator that converts a function into a lazy property. The
function wrapped is called the first time to retrieve the result
and than that calculated result is used the next time you access
the value::
class Foo(object):
@cached_property
def foo(self):
# calculate something important here
return 42
Lifted from werkzeug.
"""
def __init__(self, func, name=None, doc=None, writable=False):
self.func = func
self.writable = writable
self.__name__ = name or func.__name__
self.__doc__ = doc or func.__doc__
def __get__(self, obj, type=None):
if obj is None:
return self
_missing = object()
value = obj.__dict__.get(self.__name__, _missing)
if value is _missing:
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value
def __set__(self, obj, value):
if not self.writable:
raise TypeError('read only attribute')
obj.__dict__[self.__name__] = value
# For unproven performance gains put firefox and thunderbird parsing
# here instead of constants
FIREFOX.latest_version = product_details.firefox_versions['LATEST_FIREFOX_VERSION']
THUNDERBIRD.latest_version = product_details.thunderbird_versions['LATEST_THUNDERBIRD_VERSION']