def test_actually_parsing_works(self): from django.template import builtins from django.template import Library from django.template import Template def test_parse(context, thing1, thing2, asvar=None): return u' '.join([str(thing1), thing2, str(asvar)]) templates = [ "{% test_parse 1 obj.val as var %}", "{% test_parse obj.fn 'string' %}", ] class test_object(object): val = 'string' def fn(self): return 1 register = Library() register.tag('test_parse', ParsedNode('test_parse', '<thing1:int> <thing2:string>( as <asvar:var>)', test_parse)) builtins.append(register) context = { 'obj':test_object() } templates_cmp = [Template(template) for template in templates] results = [t.render(context) for t in templates_cmp] self.assertEqual(results[0], '1 string var') self.assertEqual(results[1], '1 string None')
def test_fail_on_parse(self): from django.template import builtins from django.template import Library from django.template import Template def test_not_enough_args(context): return 'this should never ever run' templates = [ "{% test_parse 1 obj.val as var %}", "{% test_parse obj.fn 'string' %}", ] class test_object(object): val = 'string' def fn(self): return 1 register = Library() register.tag('test_parse', ParsedNode('test_parse', '<thing1:int> <thing2:string>( as <asvar:var>)', test_not_enough_args)) builtins.append(register) context = { 'obj':test_object() } templates_cmp = [Template(template) for template in templates] for i in templates_cmp: self.assertRaises(TemplateSyntaxError, i.render, context)
def main(): """Main entry point for the script. This invokes the generator with the given parameters.""" # Initialize empty django environment FILTERS_MODULE = 'filters' if os.path.exists(os.path.join(os.getcwd(), 'filters/__init__.py')): sys.path += [os.getcwd(),] __import__(FILTERS_MODULE) from django.template import builtins builtins.append(sys.modules[FILTERS_MODULE].register) from django.conf import settings settings.configure(DEBUG=True, TEMPLATE_DEBUG=True) logging.basicConfig(level=logging.DEBUG) parser = init_parser() (options, args) = parser.parse_args() if len(args) < 1: parser.print_help() sys.exit(-1) try: Generator(createdir=options.createdir, outputdir=options.outputdir).generate(args) except GenerationException, e: for file, error in e.errors: logging.debug("Error processing %s: %s" % (file, ' '.join(str(error).strip('\t').split('\n'))))
def test_empty_does_not_break(self): from django.template import builtins from django.template import Library from django.template import Template def test_empty_values_are_okay(context, *args, **kwargs): self.assertTrue(True) return u'' template = "{% test_parse dne_int dne_str dne_any dne_var %}" register = Library() register.tag('test_parse', ParsedNode('test_parse', '<arg1:int> <arg2:string> <arg3:any> <arg4:var>', test_empty_values_are_okay)) builtins.append(register) context = { } template_obj = Template(template) try: template_obj.render(context) except Exception, e: self.fail("%s was raised when rendering a parsed tag with empty arguments." % str(e))
from BeautifulSoup import BeautifulSoup from collections import defaultdict from django.conf import settings from django.template import Template, Context, Library, builtins from django.utils.encoding import force_unicode from xhtml2pdf import pisa from ngs import fastq #=================================# # Custom template filters register = Library() @register.filter def keyval(d, key): return d.get(key,'') builtins.append(register) @register.filter(is_safe=True) def intcomma(value, use_l10n=True): """ Converts an integer to a string containing commas every three digits. For example, 3000 becomes '3,000' and 45000 becomes '45,000'. """ if settings.USE_L10N and use_l10n: try: if not isinstance(value, float): value = int(value) except (TypeError, ValueError): return intcomma(value, False) else: return number_format(value, force_grouping=True)