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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« cms/converters.py ('K') | « cms/converters.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/sources.py
===================================================================
--- a/cms/sources.py
+++ b/cms/sources.py
@@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
-import codecs
+import io
import collections
import ConfigParser
import json
@@ -63,21 +63,11 @@
return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:])
def read_config(self):
- configdata = self.read_file("settings.ini")
+ configdata = self.read_file("settings.ini")[0]
config = ConfigParser.SafeConfigParser()
config.readfp(StringIO(configdata))
return config
- def import_symbol(self, filename, symbol):
- code = self.read_file(filename)
- namespace = {}
- exec code in namespace
-
- try:
- return namespace[symbol]
- except KeyError:
- raise Exception("Expected symbol %s not found in %s" % (symbol, filename))
-
#
# Page helpers
#
@@ -124,7 +114,7 @@
return self.has_file(self.localizable_file_filename(locale, filename))
def read_localizable_file(self, locale, filename):
- return self.read_file(self.localizable_file_filename(locale, filename), binary=True)
+ return self.read_file(self.localizable_file_filename(locale, filename), binary=True)[0]
#
# Static file helpers
@@ -141,7 +131,7 @@
return self.has_file(self.static_filename(filename))
def read_static(self, filename):
- return self.read_file(self.static_filename(filename), binary=True)
+ return self.read_file(self.static_filename(filename), binary=True)[0]
#
# Locale helpers
@@ -174,7 +164,7 @@
result.update(self.read_locale(default_locale, page))
if self.has_locale(locale, page):
- filedata = self.read_file(self.locale_filename(locale, page))
+ filedata = self.read_file(self.locale_filename(locale, page))[0]
localedata = json.loads(filedata)
for key, value in localedata.iteritems():
result[key] = value["message"]
@@ -236,10 +226,16 @@
return True
def read_file(self, filename, binary=False):
- result = self._archive.read("./%s" % filename)
+ data = self._archive.read("./%s" % filename)
if not binary:
- result = result.decode("utf-8")
- return result
+ data = data.decode("utf-8")
+ 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
+
+ def exec_file(self, filename):
+ code = self.read_file(filename)[0]
+ namespace = {}
+ exec code in namespace
+ 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.
def list_files(self, subdir):
prefix = "./%s/" % subdir
@@ -271,9 +267,20 @@
return os.path.isfile(self.get_path(filename))
def read_file(self, filename, binary=False):
- encoding = None if binary else "utf-8"
- with codecs.open(self.get_path(filename), "rb", encoding=encoding) as handle:
- return handle.read()
+ path = self.get_path(filename)
+
+ if binary:
+ file = open(path, "rb")
+ else:
+ file = io.open(path, "r", encoding="utf-8")
+
+ with file:
+ return (file.read(), path)
+
+ def exec_file(self, filename):
+ namespace = {}
+ execfile(self.get_path(filename), namespace)
+ return namespace
def list_files(self, subdir):
result = []
« cms/converters.py ('K') | « cms/converters.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld