Commit c1a96ac3 by Chris Jerdonek

Simplified call() even further.

parent b222ce48
...@@ -17,8 +17,10 @@ DEFAULT_TAG_CLOSING = '}}' ...@@ -17,8 +17,10 @@ DEFAULT_TAG_CLOSING = '}}'
END_OF_LINE_CHARACTERS = ['\r', '\n'] END_OF_LINE_CHARACTERS = ['\r', '\n']
def call(val, view): def call(val, context):
""" """
Returns: a string of type unicode.
Arguments: Arguments:
val: the argument val can be any of the following: val: the argument val can be any of the following:
...@@ -28,53 +30,48 @@ def call(val, view): ...@@ -28,53 +30,48 @@ def call(val, view):
* RenderEngine._make_get_literal(): * RenderEngine._make_get_literal():
Args: context Args: context
Returns: unicode
* RenderEngine._make_get_escaped(): * RenderEngine._make_get_escaped():
Args: context Args: context
Returns: unicode
* RenderEngine._make_get_partial() * RenderEngine._make_get_partial()
Args: context Args: context
Returns: unicode
* RenderEngine._make_get_section() * RenderEngine._make_get_section()
Args: context Args: context
Returns: unicode
* _make_get_inverse() * _make_get_inverse()
Args: context Args: context
Returns: unicode
""" """
if callable(val): if callable(val):
(args, _, _, _) = inspect.getargspec(val) val = val(context)
args_count = len(args)
if not isinstance(val, types.FunctionType):
# Then val is an instance method. Subtract one from the
# argument count because Python will automatically prepend
# self to the argument list when calling.
args_count -=1
if args_count is 0:
val = val()
else:
val = val(view)
if val is None:
val = ''
return unicode(val) return val
def render_parse_tree(parse_tree, context): def render_parse_tree(parse_tree, context):
""" """
Convert a parse-tree into a string. Returns: a string of type unicode.
""" """
get_string = lambda val: call(val, context) get_string = lambda val: call(val, context)
parts = map(get_string, parse_tree) parts = map(get_string, parse_tree)
return ''.join(parts) s = ''.join(parts)
return unicode(s)
def _make_get_inverse(name, parsed): def _make_get_inverse(name, parsed):
def get_inverse(context): def get_inverse(context):
"""
Returns a string with type unicode.
"""
data = context.get(name) data = context.get(name)
if data: if data:
return '' return u''
return render_parse_tree(parsed, context) return render_parse_tree(parsed, context)
return get_inverse return get_inverse
...@@ -166,6 +163,8 @@ class RenderEngine(object): ...@@ -166,6 +163,8 @@ class RenderEngine(object):
def _render_template(self, template, context): def _render_template(self, template, context):
""" """
Returns: a string of type unicode.
Arguments: Arguments:
template: template string template: template string
...@@ -256,6 +255,10 @@ class RenderEngine(object): ...@@ -256,6 +255,10 @@ class RenderEngine(object):
def _make_get_literal(self, name): def _make_get_literal(self, name):
def get_literal(context): def get_literal(context):
"""
Returns: a string of type unicode.
"""
s = self._get_string_value(context, name) s = self._get_string_value(context, name)
s = self.literal(s) s = self.literal(s)
return s return s
...@@ -266,6 +269,10 @@ class RenderEngine(object): ...@@ -266,6 +269,10 @@ class RenderEngine(object):
get_literal = self._make_get_literal(name) get_literal = self._make_get_literal(name)
def get_escaped(context): def get_escaped(context):
"""
Returns: a string of type unicode.
"""
s = self._get_string_value(context, name) s = self._get_string_value(context, name)
s = self.escape(s) s = self.escape(s)
return s return s
...@@ -274,6 +281,10 @@ class RenderEngine(object): ...@@ -274,6 +281,10 @@ class RenderEngine(object):
def _make_get_partial(self, name, indentation=''): def _make_get_partial(self, name, indentation=''):
def get_partial(context): def get_partial(context):
"""
Returns: a string of type unicode.
"""
nonblank = re.compile(r'^(.)', re.M) nonblank = re.compile(r'^(.)', re.M)
template = self.load_partial(name) template = self.load_partial(name)
# Indent before rendering. # Indent before rendering.
...@@ -284,6 +295,10 @@ class RenderEngine(object): ...@@ -284,6 +295,10 @@ class RenderEngine(object):
def _make_get_section(self, name, parse_tree_, template_, delims): def _make_get_section(self, name, parse_tree_, template_, delims):
def get_section(context): def get_section(context):
"""
Returns: a string of type unicode.
"""
template = template_ template = template_
parse_tree = parse_tree_ parse_tree = parse_tree_
data = context.get(name) data = context.get(name)
...@@ -303,7 +318,7 @@ class RenderEngine(object): ...@@ -303,7 +318,7 @@ class RenderEngine(object):
parts.append(render_parse_tree(parse_tree, context)) parts.append(render_parse_tree(parse_tree, context))
context.pop() context.pop()
return ''.join(parts) return unicode(''.join(parts))
return get_section return get_section
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment