Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Left Patch Set: Reports - user usefulness Created Oct. 19, 2012, 12:54 p.m.
Right Patch Set: Reports - user usefulness Created Oct. 29, 2012, 12:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/reports/web/updateReport.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This Source Code is subject to the terms of the Mozilla Public License 3 # This Source Code is subject to the terms of the Mozilla Public License
4 # version 2.0 (the "License"). You can obtain a copy of the License at 4 # version 2.0 (the "License"). You can obtain a copy of the License at
5 # http://mozilla.org/MPL/2.0/. 5 # http://mozilla.org/MPL/2.0/.
6 6
7 import os, sys, codecs, subprocess, sitescripts 7 import os, sys, codecs, subprocess, sitescripts
8 from time import time 8 from time import time
9 from tempfile import mkstemp 9 from tempfile import mkstemp
10 from ConfigParser import SafeConfigParser 10 from ConfigParser import SafeConfigParser
11 11
12 siteScriptsPath = sitescripts.__path__[0] 12 siteScriptsPath = sitescripts.__path__[0]
13 13
14 class memoize:
15 """
16 Decorator that caches a function with arguments return value.
17 """
Wladimir Palant 2012/10/22 13:32:52 Any reason to introduce a new decorator instead of
Andrey Novikov 2012/10/23 14:15:29 I thought you were not considering them on purpose
18 def __init__ (self, f):
19 self.f = f
20 self.mem = {}
21
22 def __call__ (self, *args, **kwargs):
23 if (args, str(kwargs)) in self.mem:
Wladimir Palant 2012/10/22 13:32:52 How about calculating str(kwargs) once at the begi
Andrey Novikov 2012/10/23 14:15:29 Done.
24 return self.mem[args, str(kwargs)]
25 else:
26 tmp = self.f(*args, **kwargs)
27 self.mem[args, str(kwargs)] = tmp
28 return tmp
29
30 class cached(object): 14 class cached(object):
31 """ 15 """
32 Decorator that caches a function's return value for a given number of second s. 16 Decorator that caches a function's return value for a given number of second s.
33 Note that this can only be used with functions that take no arguments. 17 Note that this only works if the string representation of the parameters is
18 always unique.
34 """ 19 """
35
36 def __init__(self, timeout): 20 def __init__(self, timeout):
37 self.timeout = timeout 21 self.timeout = timeout
38 self.func = None 22 self.lastResult = {}
39 self.lastUpdate = None 23 self.lastUpdate = {}
40 self.lastResult = None
41 24
42 def __call__(self, *args): 25 def __call__(self, func):
43 if len(args) == 1: 26 def wrapped(*args, **kwargs):
44 # We got called with the function to be decorated - remember the function 27 key = str(kwargs)
45 # and return the same object again
46 self.func = args[0]
47 return self
48 else:
49 currentTime = time() 28 currentTime = time()
50 if self.lastUpdate == None or currentTime - self.lastUpdate > self.timeout : 29 if (args, key) not in self.lastUpdate or currentTime - self.lastUpdate[arg s, key] > self.timeout:
51 self.lastResult = self.func() 30 self.lastResult[args, key] = func(*args, **kwargs)
52 self.lastUpdate = currentTime 31 self.lastUpdate[args, key] = currentTime
53 return self.lastResult 32 return self.lastResult[args, key]
33 self.func = func
34 return wrapped
54 35
55 def __repr__(self): 36 def __repr__(self):
56 """Return the function's docstring""" 37 return repr(self.func)
57 return self.func.__doc__
58 38
59 @cached(3600) 39 @cached(3600)
60 def get_config(): 40 def get_config():
61 """ 41 """
62 Returns parsed configuration file (SafeConfigParser instance). File paths 42 Returns parsed configuration file (SafeConfigParser instance). File paths
63 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts, 43 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts,
64 /etc/sitescripts.ini 44 /etc/sitescripts.ini
65 """ 45 """
66 46
67 paths = [] 47 paths = []
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 def get_unescaped_template_environment(): 117 def get_unescaped_template_environment():
138 """ 118 """
139 Returns a Jinja2 template environment without autoescaping. Don't use this t o 119 Returns a Jinja2 template environment without autoescaping. Don't use this t o
140 generate HTML files! 120 generate HTML files!
141 """ 121 """
142 from sitescripts.templateFilters import filters 122 from sitescripts.templateFilters import filters
143 import jinja2 123 import jinja2
144 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath)) 124 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath))
145 env.filters.update(filters) 125 env.filters.update(filters)
146 return env 126 return env
LEFTRIGHT

Powered by Google App Engine
This is Rietveld