daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import get_template_attribute
  2. from jinja2 import Template
  3.  
  4.     class InternalMacro(object):
  5.         """
  6.         Creates a macro given a name, internal macro text, and content to fill(as namedtuple(t.var), dict(k,v), list(i), or other)
  7.         """
  8.         def __init__(self, name = None,
  9.                            text = None,
  10.                            content_is = None):
  11.             self.name = name
  12.             self.macro_name = "{}_template".format(self.name)
  13.             self.macro_var = "{}_macro".format(self.name)
  14.             self.text = text
  15.             self.content_is = content_is
  16.             self.macro_txt = self.format_text
  17.  
  18.         @property
  19.         def is_tuple(self):
  20.             return "{{% macro {0}(t) %}}{1}{{% endmacro %}}".format(self.macro_var, self.text)
  21.  
  22.         @property
  23.         def is_dict(self):
  24.             return "{{% macro {0}(items) %}}{{% for k,v in items.iteritems() %}}{1}{{% endfor %}}{{% endmacro %}}".format(self.macro_var, self.text)
  25.  
  26.         @property
  27.         def is_list(self):
  28.             return "{{% macro {0}(items) %}}{{% for i in items %}}{1}{{% endfor %}}{{% endmacro %}}".format(self.macro_var, self.text)
  29.  
  30.         @property
  31.         def format_text(self):
  32.             return getattr(self, self.content_is)
  33.  
  34.         @property
  35.         def return_template(self):
  36.             return Template(self.macro_txt)
  37.  
  38.         @property
  39.         def return_callable(self):
  40.             return get_template_attribute(self.return_template, self.macro_var)
  41.    
  42. test_macro = InternalMacro('test', '{{ t }} <div id="divvy">{{ t.var }}</div>', 'is_tuple')
  43.  
  44. test_macro.return_callable(Anamedtuple)
  45.    
  46. u'Anamedtuple(var="A VAR VALUE") <div id="divvy"></div>'
  47.    
  48. u'Anamedtuple(var="A VAR VALUE")' <div id="divvy">A VAR VALUE</div>
  49.    
  50. test_macro = InternalMacro('test', '{{ t }} <div id="divvy">{{ t.var }}</div>', 'is_tuple')
  51.    
  52. u'Anamedtuple(var="A VAR VALUE") <div id="divvy"></div>'
  53.    
  54. test_macro = InternalMacro('test', '<div id="divvy">{{ t.var }}</div>', 'is_tuple')
  55.    
  56. '<div id="divvy">A VAR VALUE</div>'
  57.    
  58. from collections import namedtuple
  59.  
  60. x = namedtuple("Foo", "var")
  61. z = x(var = 123)
  62. with app.app_context():
  63.     test_macro = InternalMacro('test', "'{{ t }}' <div id="divvy">{{ t.var }}</div>", 'is_tuple')
  64.     returnVal = test_macro.return_callable(z)
  65.  
  66. print returnVal
  67. #'Foo(var=123)' <div id="divvy">123</div>
  68.    
  69. repr(returnVal)
  70. 'u'\'Foo(var=123)\' <div id="divvy">123</div>''
  71.    
  72. elif token.type == 'block_begin':
  73.   flush_data()
  74.   next(self.stream)
  75.   if end_tokens is not None and
  76.                    self.stream.current.test_any(*end_tokens):
  77.      return body
  78.   rv = self.parse_statement()
  79.   if isinstance(rv, list):
  80.      body.extend(rv)
  81.   else:
  82.      body.append(rv)
  83.   self.stream.expect('block_end')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top