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

Side by Side Diff: sitescripts/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Patch Set: Reports - user usefulness Created Oct. 23, 2012, 2:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/reports/web/updateReport.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 cached(object): 14 class cached(object):
15 """ 15 """
16 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.
Wladimir Palant 2012/10/25 15:12:55 Still need a note here like: Note that this only
Andrey Novikov 2012/10/29 07:05:09 Done.
17 Note that this can only be used with functions that take no arguments.
18 """ 17 """
19
20 def __init__(self, timeout): 18 def __init__(self, timeout):
21 self.timeout = timeout 19 self.timeout = timeout
22 self.func = None 20 self.lastResult = {}
23 self.lastUpdate = None 21 self.lastUpdate = {}
24 self.lastResult = None
25 22
26 def __call__(self, *args): 23 def __call__(self, func):
27 if len(args) == 1: 24 def wrapped(*args, **kwargs):
28 # We got called with the function to be decorated - remember the function 25 key = str(kwargs)
29 # and return the same object again
30 self.func = args[0]
31 return self
32 else:
33 currentTime = time() 26 currentTime = time()
34 if self.lastUpdate == None or currentTime - self.lastUpdate > self.timeout : 27 if (args, key) not in self.lastUpdate or currentTime - self.lastUpdate[arg s, key] > self.timeout:
35 self.lastResult = self.func() 28 self.lastResult[args, key] = func(*args, **kwargs)
36 self.lastUpdate = currentTime 29 self.lastUpdate[args, key] = currentTime
37 return self.lastResult 30 return self.lastResult[args, key]
31 self.wrapped = wrapped
32 return wrapped
38 33
39 def __repr__(self): 34 def __repr__(self):
40 """Return the function's docstring""" 35 return repr(self.wrapped)
Wladimir Palant 2012/10/25 15:12:55 I don't think that we should forward this to our w
Andrey Novikov 2012/10/29 07:05:09 Done.
41 return self.func.__doc__
42 36
43 @cached(3600) 37 @cached(3600)
44 def get_config(): 38 def get_config():
45 """ 39 """
46 Returns parsed configuration file (SafeConfigParser instance). File paths 40 Returns parsed configuration file (SafeConfigParser instance). File paths
47 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts, 41 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts,
48 /etc/sitescripts.ini 42 /etc/sitescripts.ini
49 """ 43 """
50 44
51 paths = [] 45 paths = []
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 def get_unescaped_template_environment(): 115 def get_unescaped_template_environment():
122 """ 116 """
123 Returns a Jinja2 template environment without autoescaping. Don't use this t o 117 Returns a Jinja2 template environment without autoescaping. Don't use this t o
124 generate HTML files! 118 generate HTML files!
125 """ 119 """
126 from sitescripts.templateFilters import filters 120 from sitescripts.templateFilters import filters
127 import jinja2 121 import jinja2
128 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath)) 122 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath))
129 env.filters.update(filters) 123 env.filters.update(filters)
130 return env 124 return env
OLDNEW
« no previous file with comments | « sitescripts/reports/web/updateReport.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld