Created
April 4, 2013 16:36
-
-
Save 1xch/5311922 to your computer and use it in GitHub Desktop.
deprecated InternalMacro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InternalMacro(object): | |
Manipulates or creates a macro given a name, macro text, and type of content to fill(is_tuple, is_dict, is_list, is_file). | |
Set content_is to 'passthrough' and kwarg 'to_pass' to move "something else" through this (useful where you | |
need to mix "something else" with internal macros for this a use case of this class, e.g. internally collating varied | |
aggregations of macros dependent on varied states or permissions | |
def __init__(self, name = None, | |
text = None, | |
content_is = None, | |
**kwargs): | |
self.macro_name = "{}_template".format(name) | |
self.macro_var = "{}_macro".format(name) | |
self.macro_text = text | |
self.content_is = content_is | |
self.macro = self.format_macro | |
if content_is is 'passthrough': | |
self.passthough = kwargs['to_pass'] or None | |
@property | |
def is_static(self): | |
takes no variables | |
return "{{% macro {0}() %}}{1}{{% endmacro %}}".format(self.macro_var, self.macro_text) | |
@property | |
def is_tuple(self): | |
Where tuple is a collections.namedtuple | |
return "{{% macro {0}(t) %}}{1}{{% endmacro %}}".format(self.macro_var, self.macro_text) | |
@property | |
def is_dict(self): | |
return "{{% macro {0}(items) %}}{{% for k,v in items.iteritems() %}}{1}{{% endfor %}}{{% endmacro %}}".format(self.macro_var, self.macro_text) | |
@property | |
def is_list(self): | |
return "{{% macro {0}(items) %}}{{% for i in items %}}{1}{{% endfor %}}{{% endmacro %}}".format(self.macro_var, self.macro_text) | |
@property | |
def is_file(self): | |
return self.macro_text | |
@property | |
def format_macro(self): | |
t = getattr(self, self.content_is) | |
if self.content_is is 'is_file': | |
return t | |
else: | |
return self.return_template(t) | |
def return_template(self, t): | |
return Template(t) | |
def renderable(self): | |
if hasattr(self, 'passthrough'): | |
return self.passthrough | |
else: | |
return get_template_attribute(self.macro, self.macro_var) | |
def render(self, content=None): | |
if hasattr(self, 'passthrough'): | |
return self.passthrough | |
elif content: | |
return self.renderable()(content) | |
else: | |
return self.renderable()() | |
#deprecate in favor of render/renderable as this class itself is being deprecated for MacroFor | |
@property | |
def return_callable(self): | |
if hasattr(self, 'passthrough'): | |
return self.passthrough | |
else: | |
return get_template_attribute(self.macro, self.macro_var) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment