| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 only works if the string representation of the parameters is | |
| 18 always unique. | |
| 17 """ | 19 """ |
| 18 def __init__(self, timeout): | 20 def __init__(self, timeout): |
| 19 self.timeout = timeout | 21 self.timeout = timeout |
| 20 self.lastResult = {} | 22 self.lastResult = {} |
| 21 self.lastUpdate = {} | 23 self.lastUpdate = {} |
| 22 | 24 |
| 23 def __call__(self, func): | 25 def __call__(self, func): |
| 24 def wrapped(*args, **kwargs): | 26 def wrapped(*args, **kwargs): |
| 25 key = str(kwargs) | 27 key = str(kwargs) |
| 26 currentTime = time() | 28 currentTime = time() |
| 27 if (args, key) not in self.lastUpdate or currentTime - self.lastUpdate[arg s, key] > self.timeout: | 29 if (args, key) not in self.lastUpdate or currentTime - self.lastUpdate[arg s, key] > self.timeout: |
| 28 self.lastResult[args, key] = func(*args, **kwargs) | 30 self.lastResult[args, key] = func(*args, **kwargs) |
| 29 self.lastUpdate[args, key] = currentTime | 31 self.lastUpdate[args, key] = currentTime |
| 30 return self.lastResult[args, key] | 32 return self.lastResult[args, key] |
| 31 self.wrapped = wrapped | 33 self.func = func |
| 32 return wrapped | 34 return wrapped |
| 33 | 35 |
| 34 def __repr__(self): | 36 def __repr__(self): |
| 35 return repr(self.wrapped) | 37 return repr(self.func) |
|
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.
| |
| 36 | 38 |
| 37 @cached(3600) | 39 @cached(3600) |
| 38 def get_config(): | 40 def get_config(): |
| 39 """ | 41 """ |
| 40 Returns parsed configuration file (SafeConfigParser instance). File paths | 42 Returns parsed configuration file (SafeConfigParser instance). File paths |
| 41 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts, | 43 that will be checked: ~/.sitescripts, ~/sitescripts.ini, /etc/sitescripts, |
| 42 /etc/sitescripts.ini | 44 /etc/sitescripts.ini |
| 43 """ | 45 """ |
| 44 | 46 |
| 45 paths = [] | 47 paths = [] |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 def get_unescaped_template_environment(): | 117 def get_unescaped_template_environment(): |
| 116 """ | 118 """ |
| 117 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 |
| 118 generate HTML files! | 120 generate HTML files! |
| 119 """ | 121 """ |
| 120 from sitescripts.templateFilters import filters | 122 from sitescripts.templateFilters import filters |
| 121 import jinja2 | 123 import jinja2 |
| 122 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath)) | 124 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath)) |
| 123 env.filters.update(filters) | 125 env.filters.update(filters) |
| 124 return env | 126 return env |
| LEFT | RIGHT |