OLD | NEW |
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. |
17 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. |
18 """ | 19 """ |
19 | |
20 def __init__(self, timeout): | 20 def __init__(self, timeout): |
21 self.timeout = timeout | 21 self.timeout = timeout |
22 self.func = None | 22 self.lastResult = {} |
23 self.lastUpdate = None | 23 self.lastUpdate = {} |
24 self.lastResult = None | |
25 | 24 |
26 def __call__(self, *args): | 25 def __call__(self, func): |
27 if len(args) == 1: | 26 def wrapped(*args, **kwargs): |
28 # We got called with the function to be decorated - remember the function | 27 key = str(kwargs) |
29 # and return the same object again | |
30 self.func = args[0] | |
31 return self | |
32 else: | |
33 currentTime = time() | 28 currentTime = time() |
34 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: |
35 self.lastResult = self.func() | 30 self.lastResult[args, key] = func(*args, **kwargs) |
36 self.lastUpdate = currentTime | 31 self.lastUpdate[args, key] = currentTime |
37 return self.lastResult | 32 return self.lastResult[args, key] |
| 33 self.func = func |
| 34 return wrapped |
38 | 35 |
39 def __repr__(self): | 36 def __repr__(self): |
40 """Return the function's docstring""" | 37 return repr(self.func) |
41 return self.func.__doc__ | |
42 | 38 |
43 @cached(3600) | 39 @cached(3600) |
44 def get_config(): | 40 def get_config(): |
45 """ | 41 """ |
46 Returns parsed configuration file (SafeConfigParser instance). File paths | 42 Returns parsed configuration file (SafeConfigParser instance). File paths |
47 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts, | 43 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts, |
48 /etc/sitescripts.ini | 44 /etc/sitescripts.ini |
49 """ | 45 """ |
50 | 46 |
51 paths = [] | 47 paths = [] |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 def get_unescaped_template_environment(): | 117 def get_unescaped_template_environment(): |
122 """ | 118 """ |
123 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 |
124 generate HTML files! | 120 generate HTML files! |
125 """ | 121 """ |
126 from sitescripts.templateFilters import filters | 122 from sitescripts.templateFilters import filters |
127 import jinja2 | 123 import jinja2 |
128 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath)) | 124 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath)) |
129 env.filters.update(filters) | 125 env.filters.update(filters) |
130 return env | 126 return env |
OLD | NEW |