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

Side by Side Diff: cms/converters.py

Issue 6439782653624320: Issue 2434 - Fixed: Custom filters and global functions can not be instance methods (Closed)
Patch Set: Created April 29, 2015, 12:06 p.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 | « no previous file | 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,
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 "translate": self.translate, 301 "translate": self.translate,
302 "linkify": self.linkify, 302 "linkify": self.linkify,
303 "toclist": self.toclist, 303 "toclist": self.toclist,
304 } 304 }
305 305
306 globals = { 306 globals = {
307 "get_string": self.get_string, 307 "get_string": self.get_string,
308 "get_page_content": self.get_page_content, 308 "get_page_content": self.get_page_content,
309 } 309 }
310 310
311 self._module_refs = []
311 for dirname, dictionary in [("filters", filters), ("globals", globals)]: 312 for dirname, dictionary in [("filters", filters), ("globals", globals)]:
312 for filename in self._params["source"].list_files(dirname): 313 for filename in self._params["source"].list_files(dirname):
313 root, ext = os.path.splitext(filename) 314 root, ext = os.path.splitext(filename)
314 if ext.lower() != ".py": 315 if ext.lower() != ".py":
315 continue 316 continue
316 317
317 path = "%s/%s" % (dirname, filename) 318 path = "%s/%s" % (dirname, filename)
318 code = self._params["source"].read_file(path) 319 code = self._params["source"].read_file(path)
319 module = imp.new_module(root.replace("/", ".")) 320 module = imp.new_module(root.replace("/", "."))
320 exec code in module.__dict__ 321 exec code in module.__dict__
321 322
322 name = os.path.basename(root) 323 name = os.path.basename(root)
323 if not hasattr(module, name): 324 if not hasattr(module, name):
324 raise Exception("Expected symbol %s not found in %s file %s" % (name, dirname, filename)) 325 raise Exception("Expected symbol %s not found in %s file %s" % (name, dirname, filename))
325 dictionary[name] = getattr(module, name) 326 dictionary[name] = getattr(module, name)
326 327
327 # HACK: The module we created here can be garbage collected because it 328 # HACK: The module we created here can be garbage collected because it
328 # isn't added to sys.modules. If a function is called and its module is 329 # isn't added to sys.modules. If a function is called and its module is
329 # gone it might cause weird errors (imports and module variables 330 # gone it might cause weird errors (imports and module variables
330 # unavailable). We avoid this situation by explicitly referencing the 331 # unavailable). We avoid this situation by keeping a reference.
331 # module from the function so they can only be garbage collected 332 if callable(dictionary[name])
332 # together. 333 self._module_refs.append(module)
333 if callable(dictionary[name]):
334 dictionary[name].module_ref = module
335 334
336 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) 335 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True)
337 self._env.filters.update(filters) 336 self._env.filters.update(filters)
338 self._env.globals.update(globals) 337 self._env.globals.update(globals)
339 338
340 def get_html(self, source): 339 def get_html(self, source):
341 template = self._env.from_string(source) 340 template = self._env.from_string(source)
342 module = template.make_module(self._params) 341 module = template.make_module(self._params)
343 for key, value in module.__dict__.iteritems(): 342 for key, value in module.__dict__.iteritems():
344 if not key.startswith("_"): 343 if not key.startswith("_"):
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 stack.pop() 394 stack.pop()
396 stack[-1]["subitems"].append(item) 395 stack[-1]["subitems"].append(item)
397 stack.append(item) 396 stack.append(item)
398 return structured 397 return structured
399 398
400 converters = { 399 converters = {
401 "html": RawConverter, 400 "html": RawConverter,
402 "md": MarkdownConverter, 401 "md": MarkdownConverter,
403 "tmpl": TemplateConverter, 402 "tmpl": TemplateConverter,
404 } 403 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld