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

Side by Side Diff: cms/sources.py

Issue 29327966: Issue 3084 - [cms] Show full tracebacks for exceptions passing template code (Closed)
Patch Set: Pass through filename and use execfile() when loading filters/globals Created Sept. 16, 2015, 11:16 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« cms/converters.py ('K') | « cms/converters.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 file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import codecs 18 import io
19 import collections 19 import collections
20 import ConfigParser 20 import ConfigParser
21 import json 21 import json
22 import os 22 import os
23 from StringIO import StringIO 23 from StringIO import StringIO
24 import subprocess 24 import subprocess
25 import urlparse 25 import urlparse
26 import zipfile 26 import zipfile
27 import logging 27 import logging
28 28
(...skipping 27 matching lines...) Expand all
56 logging.warning("Link to %s cannot be resolved", page) 56 logging.warning("Link to %s cannot be resolved", page)
57 57
58 parts = page.split("/") 58 parts = page.split("/")
59 if parts[-1] == default_page: 59 if parts[-1] == default_page:
60 page = "/".join(parts[:-1]) 60 page = "/".join(parts[:-1])
61 61
62 path = "/%s/%s" % (locale, page) 62 path = "/%s/%s" % (locale, page)
63 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) 63 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:])
64 64
65 def read_config(self): 65 def read_config(self):
66 configdata = self.read_file("settings.ini") 66 configdata = self.read_file("settings.ini")[0]
67 config = ConfigParser.SafeConfigParser() 67 config = ConfigParser.SafeConfigParser()
68 config.readfp(StringIO(configdata)) 68 config.readfp(StringIO(configdata))
69 return config 69 return config
70 70
71 def import_symbol(self, filename, symbol):
72 code = self.read_file(filename)
73 namespace = {}
74 exec code in namespace
75
76 try:
77 return namespace[symbol]
78 except KeyError:
79 raise Exception("Expected symbol %s not found in %s" % (symbol, filename))
80
81 # 71 #
82 # Page helpers 72 # Page helpers
83 # 73 #
84 74
85 @staticmethod 75 @staticmethod
86 def page_filename(page, format): 76 def page_filename(page, format):
87 return "pages/%s.%s" % (page, format) 77 return "pages/%s.%s" % (page, format)
88 78
89 def list_pages(self): 79 def list_pages(self):
90 for filename in self.list_files("pages"): 80 for filename in self.list_files("pages"):
(...skipping 26 matching lines...) Expand all
117 default_locale = self.read_config().get("general", "defaultlocale") 107 default_locale = self.read_config().get("general", "defaultlocale")
118 return filter( 108 return filter(
119 lambda f: os.path.splitext(f)[1].lower() != ".json", 109 lambda f: os.path.splitext(f)[1].lower() != ".json",
120 self.list_files("locales/%s" % default_locale) 110 self.list_files("locales/%s" % default_locale)
121 ) 111 )
122 112
123 def has_localizable_file(self, locale, filename): 113 def has_localizable_file(self, locale, filename):
124 return self.has_file(self.localizable_file_filename(locale, filename)) 114 return self.has_file(self.localizable_file_filename(locale, filename))
125 115
126 def read_localizable_file(self, locale, filename): 116 def read_localizable_file(self, locale, filename):
127 return self.read_file(self.localizable_file_filename(locale, filename), bina ry=True) 117 return self.read_file(self.localizable_file_filename(locale, filename), bina ry=True)[0]
128 118
129 # 119 #
130 # Static file helpers 120 # Static file helpers
131 # 121 #
132 122
133 @staticmethod 123 @staticmethod
134 def static_filename(filename): 124 def static_filename(filename):
135 return "static/%s" % filename 125 return "static/%s" % filename
136 126
137 def list_static(self): 127 def list_static(self):
138 return self.list_files("static") 128 return self.list_files("static")
139 129
140 def has_static(self, filename): 130 def has_static(self, filename):
141 return self.has_file(self.static_filename(filename)) 131 return self.has_file(self.static_filename(filename))
142 132
143 def read_static(self, filename): 133 def read_static(self, filename):
144 return self.read_file(self.static_filename(filename), binary=True) 134 return self.read_file(self.static_filename(filename), binary=True)[0]
145 135
146 # 136 #
147 # Locale helpers 137 # Locale helpers
148 # 138 #
149 139
150 @classmethod 140 @classmethod
151 def locale_filename(cls, locale, page): 141 def locale_filename(cls, locale, page):
152 return cls.localizable_file_filename(locale, page + ".json") 142 return cls.localizable_file_filename(locale, page + ".json")
153 143
154 def list_locales(self): 144 def list_locales(self):
(...skipping 12 matching lines...) Expand all
167 pass 157 pass
168 return self.has_file(self.locale_filename(locale, page)) 158 return self.has_file(self.locale_filename(locale, page))
169 159
170 def read_locale(self, locale, page): 160 def read_locale(self, locale, page):
171 default_locale = self.read_config().get("general", "defaultlocale") 161 default_locale = self.read_config().get("general", "defaultlocale")
172 result = collections.OrderedDict() 162 result = collections.OrderedDict()
173 if locale != default_locale: 163 if locale != default_locale:
174 result.update(self.read_locale(default_locale, page)) 164 result.update(self.read_locale(default_locale, page))
175 165
176 if self.has_locale(locale, page): 166 if self.has_locale(locale, page):
177 filedata = self.read_file(self.locale_filename(locale, page)) 167 filedata = self.read_file(self.locale_filename(locale, page))[0]
178 localedata = json.loads(filedata) 168 localedata = json.loads(filedata)
179 for key, value in localedata.iteritems(): 169 for key, value in localedata.iteritems():
180 result[key] = value["message"] 170 result[key] = value["message"]
181 171
182 return result 172 return result
183 173
184 # 174 #
185 # Template helpers 175 # Template helpers
186 # 176 #
187 177
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 self._archive.close() 219 self._archive.close()
230 220
231 def has_file(self, filename): 221 def has_file(self, filename):
232 try: 222 try:
233 self._archive.getinfo("./%s" % filename) 223 self._archive.getinfo("./%s" % filename)
234 except KeyError: 224 except KeyError:
235 return False 225 return False
236 return True 226 return True
237 227
238 def read_file(self, filename, binary=False): 228 def read_file(self, filename, binary=False):
239 result = self._archive.read("./%s" % filename) 229 data = self._archive.read("./%s" % filename)
240 if not binary: 230 if not binary:
241 result = result.decode("utf-8") 231 data = data.decode("utf-8")
242 return result 232 return (data, None)
Wladimir Palant 2015/09/16 17:35:53 How about returning "%s!%s" % (self._name, filenam
Sebastian Noack 2015/09/16 19:04:04 Not sure if I like the notation with the exclamati
Wladimir Palant 2015/09/16 21:08:59 For reference, this notation is inspired by the JA
233
234 def exec_file(self, filename):
235 code = self.read_file(filename)[0]
236 namespace = {}
237 exec code in namespace
238 return namespace
Wladimir Palant 2015/09/16 17:35:53 I looked into this and this change appears to be u
Sebastian Noack 2015/09/16 19:04:04 Nice catch. Done.
243 239
244 def list_files(self, subdir): 240 def list_files(self, subdir):
245 prefix = "./%s/" % subdir 241 prefix = "./%s/" % subdir
246 for filename in self._archive.namelist(): 242 for filename in self._archive.namelist():
247 if filename.startswith(prefix): 243 if filename.startswith(prefix):
248 yield filename[len(prefix):] 244 yield filename[len(prefix):]
249 245
250 if os.name == "posix": 246 if os.name == "posix":
251 def get_cache_dir(self): 247 def get_cache_dir(self):
252 return "/var/cache/" + self._name 248 return "/var/cache/" + self._name
(...skipping 11 matching lines...) Expand all
264 def close(self): 260 def close(self):
265 pass 261 pass
266 262
267 def get_path(self, filename): 263 def get_path(self, filename):
268 return os.path.join(self._dir, *filename.split("/")) 264 return os.path.join(self._dir, *filename.split("/"))
269 265
270 def has_file(self, filename): 266 def has_file(self, filename):
271 return os.path.isfile(self.get_path(filename)) 267 return os.path.isfile(self.get_path(filename))
272 268
273 def read_file(self, filename, binary=False): 269 def read_file(self, filename, binary=False):
274 encoding = None if binary else "utf-8" 270 path = self.get_path(filename)
275 with codecs.open(self.get_path(filename), "rb", encoding=encoding) as handle : 271
276 return handle.read() 272 if binary:
273 file = open(path, "rb")
274 else:
275 file = io.open(path, "r", encoding="utf-8")
276
277 with file:
278 return (file.read(), path)
279
280 def exec_file(self, filename):
281 namespace = {}
282 execfile(self.get_path(filename), namespace)
283 return namespace
277 284
278 def list_files(self, subdir): 285 def list_files(self, subdir):
279 result = [] 286 result = []
280 def do_list(dir, relpath): 287 def do_list(dir, relpath):
281 try: 288 try:
282 files = os.listdir(dir) 289 files = os.listdir(dir)
283 except OSError: 290 except OSError:
284 return 291 return
285 292
286 for filename in files: 293 for filename in files:
287 path = os.path.join(dir, filename) 294 path = os.path.join(dir, filename)
288 if os.path.isfile(path): 295 if os.path.isfile(path):
289 result.append(relpath + filename) 296 result.append(relpath + filename)
290 elif os.path.isdir(path): 297 elif os.path.isdir(path):
291 do_list(path, relpath + filename + "/") 298 do_list(path, relpath + filename + "/")
292 do_list(self.get_path(subdir), "") 299 do_list(self.get_path(subdir), "")
293 return result 300 return result
294 301
295 def get_cache_dir(self): 302 def get_cache_dir(self):
296 return os.path.join(self._dir, "cache") 303 return os.path.join(self._dir, "cache")
OLDNEW
« cms/converters.py ('K') | « cms/converters.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld