Used proper 2to3-converted pystache lib.
This commit is contained in:
parent
a335f7e702
commit
3239f7be34
@ -22,7 +22,7 @@ except:
|
||||
from sys import exc_info
|
||||
ex_type, ex_value, tb = exc_info()
|
||||
new_ex = Exception("%s: %s" % (ex_type.__name__, ex_value))
|
||||
raise new_ex.__class__, new_ex, tb
|
||||
raise new_ex.__class__(new_ex).with_traceback(tb)
|
||||
|
||||
# The optparse module is deprecated in Python 2.7 in favor of argparse.
|
||||
# However, argparse is not available in Python 2.6 and earlier.
|
||||
@ -88,7 +88,7 @@ def main(sys_argv=sys.argv):
|
||||
context = json.loads(context)
|
||||
|
||||
rendered = renderer.render(template, context)
|
||||
print rendered
|
||||
print(rendered)
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
|
@ -13,9 +13,9 @@ def _get_string_types():
|
||||
# and byte strings, and 2to3 seems to convert all of "str", "unicode",
|
||||
# and "basestring" to Python 3's "str".
|
||||
if version_info < (3, ):
|
||||
return basestring
|
||||
return str
|
||||
# The latter evaluates to "bytes" in Python 3 -- even after conversion by 2to3.
|
||||
return (str, type(u"a".encode('utf-8')))
|
||||
return (str, type("a".encode('utf-8')))
|
||||
|
||||
|
||||
_STRING_TYPES = _get_string_types()
|
||||
|
@ -15,6 +15,7 @@ spec, we define these categories mutually exclusively as follows:
|
||||
"""
|
||||
|
||||
from pystache.common import PystacheError
|
||||
import collections
|
||||
|
||||
|
||||
# This equals '__builtin__' in Python 2 and 'builtins' in Python 3.
|
||||
@ -69,7 +70,7 @@ def _get_value(context, key):
|
||||
else:
|
||||
# TODO: consider using EAFP here instead.
|
||||
# http://docs.python.org/glossary.html#term-eafp
|
||||
if callable(attr):
|
||||
if isinstance(attr, collections.Callable):
|
||||
return attr()
|
||||
return attr
|
||||
|
||||
|
@ -39,7 +39,7 @@ STRING_ENCODING = sys.getdefaultencoding()
|
||||
FILE_ENCODING = sys.getdefaultencoding()
|
||||
|
||||
# The delimiters to start with when parsing.
|
||||
DELIMITERS = (u'{{', u'}}')
|
||||
DELIMITERS = ('{{', '}}')
|
||||
|
||||
# How to handle missing tags when rendering a template.
|
||||
MISSING_TAGS = MissingTags.ignore
|
||||
|
@ -86,7 +86,7 @@ class Loader(object):
|
||||
def _make_locator(self):
|
||||
return Locator(extension=self.extension)
|
||||
|
||||
def unicode(self, s, encoding=None):
|
||||
def str(self, s, encoding=None):
|
||||
"""
|
||||
Convert a string to unicode using the given encoding, and return it.
|
||||
|
||||
@ -119,7 +119,7 @@ class Loader(object):
|
||||
if encoding is None:
|
||||
encoding = self.file_encoding
|
||||
|
||||
return self.unicode(b, encoding)
|
||||
return self.str(b, encoding)
|
||||
|
||||
def load_file(self, file_name):
|
||||
"""
|
||||
|
@ -44,7 +44,7 @@ class ParsedTemplate(object):
|
||||
if type(node) is str:
|
||||
return node
|
||||
return node.render(engine, context)
|
||||
parts = map(get_unicode, self._parse_tree)
|
||||
parts = list(map(get_unicode, self._parse_tree))
|
||||
s = ''.join(parts)
|
||||
|
||||
return str(s)
|
||||
|
@ -9,9 +9,10 @@ import re
|
||||
|
||||
from pystache import defaults
|
||||
from pystache.parsed import ParsedTemplate
|
||||
import collections
|
||||
|
||||
|
||||
END_OF_LINE_CHARACTERS = [u'\r', u'\n']
|
||||
END_OF_LINE_CHARACTERS = ['\r', '\n']
|
||||
NON_BLANK_RE = re.compile(r'^(.)', re.M)
|
||||
|
||||
|
||||
@ -30,8 +31,8 @@ def parse(template, delimiters=None):
|
||||
|
||||
Examples:
|
||||
|
||||
>>> parsed = parse(u"Hey {{#who}}{{name}}!{{/who}}")
|
||||
>>> print str(parsed).replace('u', '') # This is a hack to get the test to pass both in Python 2 and 3.
|
||||
>>> parsed = parse("Hey {{#who}}{{name}}!{{/who}}")
|
||||
>>> print(str(parsed).replace('u', '')) # This is a hack to get the test to pass both in Python 2 and 3.
|
||||
['Hey ', _SectionNode(key='who', index_begin=12, index_end=21, parsed=[_EscapeNode(key='name'), '!'])]
|
||||
|
||||
"""
|
||||
@ -94,7 +95,7 @@ class _CommentNode(object):
|
||||
return _format(self)
|
||||
|
||||
def render(self, engine, context):
|
||||
return u''
|
||||
return ''
|
||||
|
||||
|
||||
class _ChangeNode(object):
|
||||
@ -106,7 +107,7 @@ class _ChangeNode(object):
|
||||
return _format(self)
|
||||
|
||||
def render(self, engine, context):
|
||||
return u''
|
||||
return ''
|
||||
|
||||
|
||||
class _EscapeNode(object):
|
||||
@ -168,7 +169,7 @@ class _InvertedNode(object):
|
||||
# Note that lambdas are considered truthy for inverted sections
|
||||
# per the spec.
|
||||
if data:
|
||||
return u''
|
||||
return ''
|
||||
return self.parsed_section.render(engine, context)
|
||||
|
||||
|
||||
@ -193,7 +194,7 @@ class _SectionNode(object):
|
||||
|
||||
parts = []
|
||||
for val in values:
|
||||
if callable(val):
|
||||
if isinstance(val, collections.Callable):
|
||||
# Lambdas special case section rendering and bypass pushing
|
||||
# the data value onto the context stack. From the spec--
|
||||
#
|
||||
@ -376,3 +377,4 @@ class _Parser(object):
|
||||
return _InvertedNode(tag_key, parsed_section)
|
||||
|
||||
raise Exception("Invalid symbol for section tag: %s" % repr(tag_type))
|
||||
|
||||
|
@ -9,6 +9,7 @@ import re
|
||||
|
||||
from pystache.common import is_string
|
||||
from pystache.parser import parse
|
||||
import collections
|
||||
|
||||
|
||||
def context_get(stack, name):
|
||||
@ -104,7 +105,7 @@ class RenderEngine(object):
|
||||
"""
|
||||
val = self.resolve_context(context, name)
|
||||
|
||||
if callable(val):
|
||||
if isinstance(val, collections.Callable):
|
||||
# Return because _render_value() is already a string.
|
||||
return self._render_value(val(), context)
|
||||
|
||||
@ -160,7 +161,7 @@ class RenderEngine(object):
|
||||
if not is_string(val):
|
||||
# In case the template is an integer, for example.
|
||||
val = self.to_str(val)
|
||||
if type(val) is not unicode:
|
||||
if type(val) is not str:
|
||||
val = self.literal(val)
|
||||
return self.render(val, context, delimiters)
|
||||
|
||||
|
@ -32,7 +32,7 @@ class Renderer(object):
|
||||
>>> partials = {'partial': 'Hello, {{thing}}!'}
|
||||
>>> renderer = Renderer(partials=partials)
|
||||
>>> # We apply print to make the test work in Python 3 after 2to3.
|
||||
>>> print renderer.render('{{>partial}}', {'thing': 'world'})
|
||||
>>> print(renderer.render('{{>partial}}', {'thing': 'world'}))
|
||||
Hello, world!
|
||||
|
||||
To customize string coercion (e.g. to render False values as ''), one can
|
||||
@ -179,7 +179,7 @@ class Renderer(object):
|
||||
# We avoid the Python ternary operator for Python 2.4 support.
|
||||
if isinstance(s, str):
|
||||
return s
|
||||
return self.unicode(s)
|
||||
return self.str(s)
|
||||
|
||||
def _to_unicode_hard(self, s):
|
||||
"""
|
||||
@ -197,7 +197,7 @@ class Renderer(object):
|
||||
"""
|
||||
return str(self.escape(self._to_unicode_soft(s)))
|
||||
|
||||
def unicode(self, b, encoding=None):
|
||||
def str(self, b, encoding=None):
|
||||
"""
|
||||
Convert a byte string to unicode, using string_encoding and decode_errors.
|
||||
|
||||
@ -230,7 +230,7 @@ class Renderer(object):
|
||||
|
||||
"""
|
||||
return Loader(file_encoding=self.file_encoding, extension=self.file_extension,
|
||||
to_unicode=self.unicode, search_dirs=self.search_dirs)
|
||||
to_unicode=self.str, search_dirs=self.search_dirs)
|
||||
|
||||
def _make_load_template(self):
|
||||
"""
|
||||
@ -299,7 +299,7 @@ class Renderer(object):
|
||||
try:
|
||||
return load_partial(name)
|
||||
except TemplateNotFoundError:
|
||||
return u''
|
||||
return ''
|
||||
|
||||
return resolve_partial
|
||||
|
||||
@ -316,7 +316,7 @@ class Renderer(object):
|
||||
try:
|
||||
return context_get(stack, name)
|
||||
except KeyNotFoundError:
|
||||
return u''
|
||||
return ''
|
||||
|
||||
return resolve_context
|
||||
|
||||
@ -458,3 +458,4 @@ class Renderer(object):
|
||||
# Otherwise, we assume the template is an object.
|
||||
|
||||
return self._render_object(template, *context, **kwargs)
|
||||
|
||||
|
@ -83,7 +83,7 @@ class SpecLoader(object):
|
||||
|
||||
"""
|
||||
if spec.template is not None:
|
||||
return self.loader.unicode(spec.template, spec.template_encoding)
|
||||
return self.loader.str(spec.template, spec.template_encoding)
|
||||
|
||||
path = self._find(spec)
|
||||
|
||||
|
Reference in New Issue
Block a user