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: Unpack converter source before calling get_html() Created Sept. 17, 2015, 10 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
« no previous file with comments | « 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): 71 def exec_file(self, filename):
72 code = self.read_file(filename) 72 source, filename = self.read_file(filename)
73 code = compile(source, filename, "exec")
73 namespace = {} 74 namespace = {}
74 exec code in namespace 75 exec code in namespace
75 76 return namespace
76 try:
77 return namespace[symbol]
78 except KeyError:
79 raise Exception("Expected symbol %s not found in %s" % (symbol, filename))
80 77
81 # 78 #
82 # Page helpers 79 # Page helpers
83 # 80 #
84 81
85 @staticmethod 82 @staticmethod
86 def page_filename(page, format): 83 def page_filename(page, format):
87 return "pages/%s.%s" % (page, format) 84 return "pages/%s.%s" % (page, format)
88 85
89 def list_pages(self): 86 def list_pages(self):
(...skipping 27 matching lines...) Expand all
117 default_locale = self.read_config().get("general", "defaultlocale") 114 default_locale = self.read_config().get("general", "defaultlocale")
118 return filter( 115 return filter(
119 lambda f: os.path.splitext(f)[1].lower() != ".json", 116 lambda f: os.path.splitext(f)[1].lower() != ".json",
120 self.list_files("locales/%s" % default_locale) 117 self.list_files("locales/%s" % default_locale)
121 ) 118 )
122 119
123 def has_localizable_file(self, locale, filename): 120 def has_localizable_file(self, locale, filename):
124 return self.has_file(self.localizable_file_filename(locale, filename)) 121 return self.has_file(self.localizable_file_filename(locale, filename))
125 122
126 def read_localizable_file(self, locale, filename): 123 def read_localizable_file(self, locale, filename):
127 return self.read_file(self.localizable_file_filename(locale, filename), bina ry=True) 124 return self.read_file(self.localizable_file_filename(locale, filename), bina ry=True)[0]
128 125
129 # 126 #
130 # Static file helpers 127 # Static file helpers
131 # 128 #
132 129
133 @staticmethod 130 @staticmethod
134 def static_filename(filename): 131 def static_filename(filename):
135 return "static/%s" % filename 132 return "static/%s" % filename
136 133
137 def list_static(self): 134 def list_static(self):
138 return self.list_files("static") 135 return self.list_files("static")
139 136
140 def has_static(self, filename): 137 def has_static(self, filename):
141 return self.has_file(self.static_filename(filename)) 138 return self.has_file(self.static_filename(filename))
142 139
143 def read_static(self, filename): 140 def read_static(self, filename):
144 return self.read_file(self.static_filename(filename), binary=True) 141 return self.read_file(self.static_filename(filename), binary=True)[0]
145 142
146 # 143 #
147 # Locale helpers 144 # Locale helpers
148 # 145 #
149 146
150 @classmethod 147 @classmethod
151 def locale_filename(cls, locale, page): 148 def locale_filename(cls, locale, page):
152 return cls.localizable_file_filename(locale, page + ".json") 149 return cls.localizable_file_filename(locale, page + ".json")
153 150
154 def list_locales(self): 151 def list_locales(self):
(...skipping 12 matching lines...) Expand all
167 pass 164 pass
168 return self.has_file(self.locale_filename(locale, page)) 165 return self.has_file(self.locale_filename(locale, page))
169 166
170 def read_locale(self, locale, page): 167 def read_locale(self, locale, page):
171 default_locale = self.read_config().get("general", "defaultlocale") 168 default_locale = self.read_config().get("general", "defaultlocale")
172 result = collections.OrderedDict() 169 result = collections.OrderedDict()
173 if locale != default_locale: 170 if locale != default_locale:
174 result.update(self.read_locale(default_locale, page)) 171 result.update(self.read_locale(default_locale, page))
175 172
176 if self.has_locale(locale, page): 173 if self.has_locale(locale, page):
177 filedata = self.read_file(self.locale_filename(locale, page)) 174 filedata = self.read_file(self.locale_filename(locale, page))[0]
178 localedata = json.loads(filedata) 175 localedata = json.loads(filedata)
179 for key, value in localedata.iteritems(): 176 for key, value in localedata.iteritems():
180 result[key] = value["message"] 177 result[key] = value["message"]
181 178
182 return result 179 return result
183 180
184 # 181 #
185 # Template helpers 182 # Template helpers
186 # 183 #
187 184
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 self._archive.close() 226 self._archive.close()
230 227
231 def has_file(self, filename): 228 def has_file(self, filename):
232 try: 229 try:
233 self._archive.getinfo("./%s" % filename) 230 self._archive.getinfo("./%s" % filename)
234 except KeyError: 231 except KeyError:
235 return False 232 return False
236 return True 233 return True
237 234
238 def read_file(self, filename, binary=False): 235 def read_file(self, filename, binary=False):
239 result = self._archive.read("./%s" % filename) 236 data = self._archive.read("./%s" % filename)
240 if not binary: 237 if not binary:
241 result = result.decode("utf-8") 238 data = data.decode("utf-8")
242 return result 239 return (data, "%s!%s" % (self._name, filename))
243 240
244 def list_files(self, subdir): 241 def list_files(self, subdir):
245 prefix = "./%s/" % subdir 242 prefix = "./%s/" % subdir
246 for filename in self._archive.namelist(): 243 for filename in self._archive.namelist():
247 if filename.startswith(prefix): 244 if filename.startswith(prefix):
248 yield filename[len(prefix):] 245 yield filename[len(prefix):]
249 246
250 if os.name == "posix": 247 if os.name == "posix":
251 def get_cache_dir(self): 248 def get_cache_dir(self):
252 return "/var/cache/" + self._name 249 return "/var/cache/" + self._name
(...skipping 11 matching lines...) Expand all
264 def close(self): 261 def close(self):
265 pass 262 pass
266 263
267 def get_path(self, filename): 264 def get_path(self, filename):
268 return os.path.join(self._dir, *filename.split("/")) 265 return os.path.join(self._dir, *filename.split("/"))
269 266
270 def has_file(self, filename): 267 def has_file(self, filename):
271 return os.path.isfile(self.get_path(filename)) 268 return os.path.isfile(self.get_path(filename))
272 269
273 def read_file(self, filename, binary=False): 270 def read_file(self, filename, binary=False):
274 encoding = None if binary else "utf-8" 271 path = self.get_path(filename)
275 with codecs.open(self.get_path(filename), "rb", encoding=encoding) as handle : 272
276 return handle.read() 273 if binary:
274 file = open(path, "rb")
275 else:
276 file = io.open(path, "r", encoding="utf-8")
277
278 with file:
279 return (file.read(), path)
277 280
278 def list_files(self, subdir): 281 def list_files(self, subdir):
279 result = [] 282 result = []
280 def do_list(dir, relpath): 283 def do_list(dir, relpath):
281 try: 284 try:
282 files = os.listdir(dir) 285 files = os.listdir(dir)
283 except OSError: 286 except OSError:
284 return 287 return
285 288
286 for filename in files: 289 for filename in files:
287 path = os.path.join(dir, filename) 290 path = os.path.join(dir, filename)
288 if os.path.isfile(path): 291 if os.path.isfile(path):
289 result.append(relpath + filename) 292 result.append(relpath + filename)
290 elif os.path.isdir(path): 293 elif os.path.isdir(path):
291 do_list(path, relpath + filename + "/") 294 do_list(path, relpath + filename + "/")
292 do_list(self.get_path(subdir), "") 295 do_list(self.get_path(subdir), "")
293 return result 296 return result
294 297
295 def get_cache_dir(self): 298 def get_cache_dir(self):
296 return os.path.join(self._dir, "cache") 299 return os.path.join(self._dir, "cache")
OLDNEW
« no previous file with comments | « cms/converters.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld