def render(value, system): value.update(system) fragment = value.pop('breve_fragment', self.fragment) template = Template(self.tags, xmlns=self.xmlns, doctype=self.doctype) return template.render(info.name, vars=value, fragment=fragment, loader=self.loader)
def test_custom_renderer ( self ): '''custom renderer''' def render_row ( tag, data ): T = html tag.clear ( ) return tag [ [ T.td [ _i ] for _i in data ] ] register_global ( 'render_row', render_row ) vars = dict ( title = my_name ( ), my_data = [ range ( 5 ), range ( 5, 10 ), range ( 10, 15 ) ] ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_let_directive_scope(self): '''test let directive's scope''' vars = dict(message='hello, from breve', title=my_name(), do_fail=False) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise # do fail - try to use the variable out of scope vars['do_fail'] = True t = Template(html, root=template_root()) self.failUnlessRaises(NameError, t.render, 'index', vars, namespace='v')
def test_let_directive_scope ( self ): '''test let directive's scope''' vars = dict ( message = 'hello, from breve', title = my_name ( ), do_fail = False ) # don't fail - use variable in scope t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise # do fail - try to use the variable out of scope vars [ 'do_fail' ] = True t = Template ( html, root = template_root ( ) ) self.failUnlessRaises ( NameError, t.render, 'index', vars, namespace = 'v' )
def test_encoding(): """encoding comments""" params = dict( title=my_name() ) t = Template(html, root=template_root()) expected = expected_output() actual = t.render('correct', params, namespace='v') assert actual == expected
def test_simple_template(): """simple template""" params = dict( message='hello, from breve', title=my_name() ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_nested_include(): """nested include() directives""" params = dict( message='hello, from breve', title=my_name() ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_loop_include(): """looping over include() with listcomp""" params = dict( message='hello, from breve', title=my_name() ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_assign_scope(): """test assign directive's scope""" params = dict( message='hello, from breve', title=my_name() ) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_loop_include(self): '''looping over include() with listcomp''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_stacks_template(): """test stacks in template""" params = dict( title=my_name(), message='hello, from breve' ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected # stack should be empty when we finish assert not get_stacks()
def test_macros_inside_inherits(): """test macros inside inherits(): scope issues""" # note: I'm not convinced this is the desired behaviour, but # it's *compatible* behaviour. params = dict( title=my_name(), message='Hello, from breve' ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_register_global(): """register_global() function""" params = dict( title=my_name() ) register_global('global_message', 'This is a global variable') _test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_macro_includes(self): '''include() directive inside macro''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_simple_template(self): '''simple template''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def render(template_name, tmpl_params=None, loader=None, fragment=False): if tmpl_params is None: tmpl_params = {} g = pylons_globals() tmpl_params.update(g) try: opts = g['app_globals'].breve_opts except AttributeError: opts = {} t = Template(tags, **opts) return t.render(template_name, params=tmpl_params, loader=loader, fragment=fragment)
def test_encoding(self): '''encoding comments''' vars = dict(title=my_name()) t = Template(html, root=template_root()) actual = t.render('wrong', vars, namespace='v') expected = expected_output() self.assertNotEqual(actual, expected) actual = t.render('correct', vars, namespace='v') try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_render_parameters(self): '''test render-time parameters''' vars = dict(message='hello, from breve', title=my_name()) args = { 'tidy': True, 'debug': True, 'namespace': 'v', 'extension': '.breve' } t = Template(html, root=template_root()) t.render('index', vars, **args) for k, v in args.items(): self.failUnless(getattr(t, k) == v)
def test_assign_scope(self): '''test assign directive's scope''' vars = dict(message='hello, from breve', title=my_name()) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_custom_renderer_notag(): """custom renderer returning non-Tag type""" def render_text(tag, data): tag.clear() return data register_global('render_text', render_text) params = dict( title=my_name(), message='hello, world' ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_simple_template ( self ): '''simple template''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_loop_include ( self ): '''looping over include() with listcomp''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_loop_macros(): """loop using macro""" params = dict( message='hello, from breve', title=my_name(), url_data=[ dict(url='http://www.google.com', label='Google'), dict(url='http://www.yahoo.com', label='Yahoo!'), dict(url='http://www.amazon.com', label='Amazon') ] ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_register_global(self): '''register_global() function''' vars = dict(title=my_name()) register_global('global_message', 'This is a global variable') test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macros_inside_inherits(self): '''test macros inside inherits(): scope issues''' # note: I'm not convinced this is the desired behaviour, but # it's *compatible* behaviour. vars = dict(title=my_name(), message='Hello, from breve') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macro_includes ( self ): '''include() directive inside macro''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_stacks_template(self): '''test stacks in template''' vars = dict(title=my_name(), message='hello, from breve') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise # stack should be empty when we finish self.failUnless(not get_stacks())
def test_encoding ( self ): '''encoding comments''' vars = dict ( title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'wrong', vars, namespace = 'v' ) expected = expected_output ( ) self.assertNotEqual ( actual, expected ) actual = t.render ( 'correct', vars, namespace = 'v' ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_register_flattener(): """register_flattener() function""" def flatten_date(o): return escape(o.strftime('%Y/%m/%d')) register_flattener(datetime, flatten_date) register_global('flatten_date', flatten_date) params = dict( title=my_name(), today=datetime(2008, 4, 17) ) _test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def render(self, info, format="html", fragment=False, template=None): """ info == dict of variables to stick into the template namespace format == output format if applicable fragment == special rules about rendering part of a page template == dotted.path.to.template (without .ext) """ vars = info # check to see if we were passed a function get extra vars if callable(self.get_extra_vars): vars.update(self.get_extra_vars()) self.breve_opts.update(self.get_config(vars)) template_path, template_filename, args = self.load_template(template) # self.breve_opts.update ( args ) template_root = self.breve_opts['root'] format = args.get('format', format) if template_root and template_path.startswith(template_root): # this feels mildly brittle template_path = template_path[len(template_root) + 1:] if format not in self.tag_defs: # this seems weak (concerns about path). Should perhaps # find a better way, but getting only a string for format # makes it difficult to do too much self.tag_defs[format] = __import__(format, {}, {}) self.breve_opts['doctype'] = self.breve_opts.get( 'doctype', self.tag_defs[format].doctype) template_obj = Template(tags=self.tag_defs[format].tags, xmlns=self.tag_defs[format].xmlns, **self.breve_opts) if fragment: return template_obj.render_partial(os.path.join( template_path, template_filename), vars=vars, **self.breve_opts) return template_obj.render(os.path.join(template_path, template_filename), vars=vars, **self.breve_opts)
def test_render_parameters(): """test render-time parameters""" params = dict( message='hello, from breve', title=my_name() ) args = { 'tidy': True, 'debug': True, 'namespace': 'v', 'extension': '.breve' } t = Template(html, root=template_root()) t.render('index', params, **args) actual = [getattr(t, k, 'not_set') for k in sorted(args)] expected = [v for k, v in sorted(args.items())] assert actual == expected
def test_custom_renderer_notag(self): '''custom renderer returning non-Tag type''' def render_text(tag, data): tag.clear() return data register_global('render_text', render_text) vars = dict(title=my_name(), message='hello, world') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def render(template_name, tmpl_vars=None, loader=None, fragment=False): if tmpl_vars is None: tmpl_vars = {} g = pylons_globals() tmpl_vars.update(g) try: opts = g['app_globals'].breve_opts except AttributeError: opts = {} t = Template(tags, **opts) return t.render(template_name, vars=tmpl_vars, loader=loader, fragment=fragment)
def test_render_parameters ( self ): '''test render-time parameters''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) args = { 'tidy': True, 'debug': True, 'namespace': 'v', 'extension': '.breve' } t = Template ( html, root = template_root ( ) ) t.render ( 'index', vars, **args ) for k, v in args.items ( ): self.failUnless ( getattr ( t, k ) == v )
def test_let_memory_freed(self): '''test that let() objects are freed''' # is this even meaningful? import gc vars = dict(title=my_name(), message="memory test", biglist=['hello'] * 1000) collection_count = gc.get_count() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') del vars gc.collect() self.assertEqual(gc.get_count(), (0, 0, 0))
def test_register_global ( self ): '''register_global() function''' vars = dict ( title = my_name ( ) ) register_global ( 'global_message', 'This is a global variable' ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_assign_scope ( self ): '''test assign directive's scope''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) # don't fail - use variable in scope t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_register_flattener(self): '''register_flattener() function''' def flatten_date(o): return escape(o.strftime('%Y/%m/%d')) register_flattener(datetime, flatten_date) register_global('flatten_date', flatten_date) vars = dict(title=my_name(), today=datetime(2008, 4, 17)) test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macros_inside_inherits ( self ): '''test macros inside inherits(): scope issues''' # note: I'm not convinced this is the desired behaviour, but # it's *compatible* behaviour. vars = dict ( title = my_name ( ), message = 'Hello, from breve' ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_loop_macros(self): '''loop using macro''' vars = dict(message='hello, from breve', title=my_name(), url_data=[ dict(url='http://www.google.com', label='Google'), dict(url='http://www.yahoo.com', label='Yahoo!'), dict(url='http://www.amazon.com', label='Amazon') ]) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_let_memory_freed ( self ): '''test that let() objects are freed''' # is this even meaningful? import gc vars = dict ( title = my_name ( ), message = "memory test", biglist = [ 'hello' ] * 1000 ) collection_count = gc.get_count ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) del vars gc.collect ( ) self.assertEqual ( gc.get_count ( ), ( 0, 0, 0 ) )
def test_stacks_template ( self ): '''test stacks in template''' vars = dict ( title = my_name ( ), message = 'hello, from breve' ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise # stack should be empty when we finish self.failUnless ( not get_stacks ( ) )
def test_custom_loader_stack ( self ): '''custom loader stack''' class PathLoader ( object ): __slots__ = [ 'paths' ] def __init__ ( self, *paths ): self.paths = paths def stat ( self, template, root ): for p in self.paths: f = os.path.join ( root, p, template ) if os.path.isfile ( f ): timestamp = long ( os.stat ( f ).st_mtime ) uid = f return uid, timestamp raise OSError, 'No such file or directory %s' % template def load ( self, uid ): return file ( uid, 'U' ).read ( ) loader = PathLoader ( template_root ( ), os.path.join ( template_root ( ), 'path1' ), os.path.join ( template_root ( ), 'path2' ), os.path.join ( template_root ( ), 'path3' ), ) register_global ( 'path_loader', loader ) vars = dict ( title = my_name ( ), message = 'hello, world' ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def render ( self, info, format = "html", fragment = False, template = None ): """ info == dict of variables to stick into the template namespace format == output format if applicable fragment == special rules about rendering part of a page template == dotted.path.to.template (without .ext) """ vars = info # check to see if we were passed a function get extra vars if callable ( self.get_extra_vars ): vars.update ( self.get_extra_vars ( ) ) self.breve_opts.update ( self.get_config ( vars ) ) template_path, template_filename, args = self.load_template ( template ) # self.breve_opts.update ( args ) template_root = self.breve_opts [ 'root' ] format = args.get ( 'format', format ) if template_root and template_path.startswith ( template_root ): # this feels mildly brittle template_path = template_path [ len ( template_root ) + 1: ] if format not in self.tag_defs: # this seems weak (concerns about path). Should perhaps # find a better way, but getting only a string for format # makes it difficult to do too much self.tag_defs [ format ] = __import__ ( format, { }, { } ) self.breve_opts [ 'doctype' ] = self.breve_opts.get ( 'doctype', self.tag_defs [ format ].doctype ) template_obj = Template ( tags = self.tag_defs [ format ].tags, xmlns = self.tag_defs [ format ].xmlns, **self.breve_opts ) if fragment: return template_obj.render_partial ( os.path.join ( template_path, template_filename ), vars = vars, **self.breve_opts ) return template_obj.render ( os.path.join ( template_path, template_filename ), vars = vars, **self.breve_opts )
def test_custom_renderer(self): '''custom renderer''' def render_row(tag, data): T = html tag.clear() return tag[[T.td[_i] for _i in data]] register_global('render_row', render_row) vars = dict(title=my_name(), my_data=[range(5), range(5, 10), range(10, 15)]) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_custom_renderer_notag ( self ): '''custom renderer returning non-Tag type''' def render_text ( tag, data ): tag.clear ( ) return data register_global ( 'render_text', render_text ) vars = dict ( title = my_name ( ), message = 'hello, world' ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def _blml_tree(name, data, fragments={}): # {{{ """ See templates in var/breve/xml, using nodes from tags.bluelines. """ tpl = Template(bluelines.tags, root=_conf.TPL_ROOT, xmlns=bluelines.xmlns) auth = api.get_current_user() brevetree = tpl._evaluate( _conf.BREVE_TPL('xml', name), fragments=fragments, vars={ '__'+name+'__': data, 'API': _conf.API, '__auth__': auth, }, ) return bluelines.tags.blueLines( version=_conf.VERSION, base=_conf.BASE_URL, auth=auth.email, )[brevetree]
def test_instantiation_parameters(self): '''test instantiation parameters''' # change the defaults to something else args = { 'tidy': True, 'debug': True, 'namespace': 'v', 'mashup_entities': True, 'extension': '.breve' } t = Template(html, root=template_root(), **args) for k, v in args.items(): self.failUnless(getattr(t, k) == v)
def test_custom_loader_stack(self): '''custom loader stack''' class PathLoader(object): __slots__ = ['paths'] def __init__(self, *paths): self.paths = paths def stat(self, template, root): for p in self.paths: f = os.path.join(root, p, template) if os.path.isfile(f): timestamp = long(os.stat(f).st_mtime) uid = f return uid, timestamp raise OSError, 'No such file or directory %s' % template def load(self, uid): return file(uid, 'U').read() loader = PathLoader( template_root(), os.path.join(template_root(), 'path1'), os.path.join(template_root(), 'path2'), os.path.join(template_root(), 'path3'), ) register_global('path_loader', loader) vars = dict(title=my_name(), message='hello, world') test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
class TemplateAdapter ( object ): """ Takes the root and a list of filenames of a breve templates in the constructor and returns an object that behaves as a django.templates.Template in the sense that the render (vars) method returns the rendered string. It tries the filenames in order and the first one for which a corresponding file exists is used. If none of the filenames matches, render raises django.template.TemplateDoesNotExist. """ def __init__ ( self, names, root = BREVE_ROOT, breve_opts = { } ): self.template = Template ( tags = html.tags, root = root, **breve_opts ) self.names = names self.breve_opts = breve_opts def render ( self, vars = None ): import os if vars == None: vars = { } elif isinstance ( vars, Context ): result = { } for d in [ d for d in vars ].__reversed__ ( ): result.update ( d ) vars = result for name in self.names: try: return flatten_string ( self.template.render ( template = name, vars = vars, **self.breve_opts ) ) except OSError: pass if len ( self.names ) == 0: raise TemplateDoesNotExist ( _( "No templates given." ) ) elif len ( self.names ) == 1: raise TemplateDoesNotExist ( _( "The template %s does not exist." ) % os.path.join ( self.template.root, self.names [ 0 ] ) ) else: raise TemplateDoesNotExist ( _( "None of the following templates exists: %s." ) % ", ".join ( [ os.path.join (self.template.root, name ) for name in self.names ] ) )
def __init__ ( self, names, root = BREVE_ROOT, breve_opts = { } ): self.template = Template ( tags = html.tags, root = root, **breve_opts ) self.names = names self.breve_opts = breve_opts
def _render ( *args, **kw ): t = Template ( tags, **template_kw ) values = f ( *args, **kw ) return t.render ( template, values )
host = 'localhost', port = 53306, user = '******', passwd = 'xxxxxxx', db = 'minecraft' ) lwc = sqlite3.connect('/home/mc/backup/plugins/LWC/lwc.db') f = open('/home/mc/backup/plugins/PermissionsBukkit/config.yml', 'r') data = yaml.safe_load(f) f.close() modaccounts = [u[0] for u in dict.items(data['users']) if 'modaccount' in u[1]['groups'] ] moderators = [u[0] for u in dict.items(data['users']) if 'moderator' in u[1]['groups'] ] day = sys.argv[1] t = Template ( tags, root = '.' ) t.doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' vars = dict ( day = day, players = map(collectActivity, modaccounts), ) logblock.close() lwc.close() print t.render ( 'index', vars )
def build_game_message(game): t = Template(tags, root='./templates/') content = t.render('game', game) return content