From 3239f7be3451b08a1c5d892deeb051817223727f Mon Sep 17 00:00:00 2001 From: sv-ncp-3 root Date: Mon, 10 Nov 2014 16:45:56 +0100 Subject: [PATCH] Used proper 2to3-converted pystache lib. --- pystache/commands/render.py | 4 ++-- pystache/common.py | 4 ++-- pystache/context.py | 3 ++- pystache/defaults.py | 2 +- pystache/loader.py | 4 ++-- pystache/parsed.py | 2 +- pystache/parser.py | 16 +++++++++------- pystache/renderengine.py | 5 +++-- pystache/renderer.py | 13 +++++++------ pystache/specloader.py | 2 +- 10 files changed, 30 insertions(+), 25 deletions(-) diff --git a/pystache/commands/render.py b/pystache/commands/render.py index 1a9c309..9c913e7 100644 --- a/pystache/commands/render.py +++ b/pystache/commands/render.py @@ -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__': diff --git a/pystache/common.py b/pystache/common.py index ad43215..762f689 100644 --- a/pystache/common.py +++ b/pystache/common.py @@ -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() diff --git a/pystache/context.py b/pystache/context.py index 6715916..7ef9ceb 100644 --- a/pystache/context.py +++ b/pystache/context.py @@ -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 diff --git a/pystache/defaults.py b/pystache/defaults.py index bcfdf4c..2fab0e0 100644 --- a/pystache/defaults.py +++ b/pystache/defaults.py @@ -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 diff --git a/pystache/loader.py b/pystache/loader.py index b591efb..54b5ff1 100644 --- a/pystache/loader.py +++ b/pystache/loader.py @@ -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): """ diff --git a/pystache/parsed.py b/pystache/parsed.py index 7cbad13..75d417d 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -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) diff --git a/pystache/parser.py b/pystache/parser.py index 91f1460..716fc1a 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -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)) + diff --git a/pystache/renderengine.py b/pystache/renderengine.py index c797b17..a88d8c1 100644 --- a/pystache/renderengine.py +++ b/pystache/renderengine.py @@ -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) diff --git a/pystache/renderer.py b/pystache/renderer.py index 5127f8a..c39d3da 100644 --- a/pystache/renderer.py +++ b/pystache/renderer.py @@ -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) + diff --git a/pystache/specloader.py b/pystache/specloader.py index 3a77d4c..a82d52a 100644 --- a/pystache/specloader.py +++ b/pystache/specloader.py @@ -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)