diff --git a/pystache/__init__.py b/pystache/__init__.py old mode 100644 new mode 100755 diff --git a/pystache/commands/__init__.py b/pystache/commands/__init__.py old mode 100644 new mode 100755 diff --git a/pystache/commands/render.py b/pystache/commands/render.py old mode 100644 new mode 100755 diff --git a/pystache/commands/test.py b/pystache/commands/test.py old mode 100644 new mode 100755 diff --git a/pystache/common.py b/pystache/common.py old mode 100644 new mode 100755 index fb266dd8..da9633f5 --- a/pystache/common.py +++ b/pystache/common.py @@ -7,14 +7,16 @@ from sys import version_info + def _get_string_types(): # TODO: come up with a better solution for this. One of the issues here # is that in Python 3 there is no common base class for unicode strings # 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 - # The latter evaluates to "bytes" in Python 3 -- even after conversion by 2to3. + return basestring + # The latter evaluates to "bytes" in Python 3 + # even after conversion by 2to3. return (unicode, type(u"a".encode('utf-8'))) diff --git a/pystache/context.py b/pystache/context.py old mode 100644 new mode 100755 index 67159160..6a3590c6 --- a/pystache/context.py +++ b/pystache/context.py @@ -27,8 +27,8 @@ # without having to rely on exceptions (e.g. KeyError) for flow control. # # TODO: eliminate the need for a private global variable, e.g. by using the -# preferred Python approach of "easier to ask for forgiveness than permission": -# http://docs.python.org/glossary.html#term-eafp +# preferred Python approach of "easier to ask for forgiveness than +# permission": http://docs.python.org/glossary.html#term-eafp class NotFound(object): pass _NOT_FOUND = NotFound() @@ -40,7 +40,8 @@ def _get_value(context, key): Returns _NOT_FOUND if the key does not exist. - The ContextStack.get() docstring documents this function's intended behavior. + The ContextStack.get() docstring documents this function's intended + behavior. """ if isinstance(context, dict): @@ -153,7 +154,7 @@ def create(*context, **kwargs): Here is an example illustrating various aspects of this method: - >>> obj1 = {'animal': 'cat', 'vegetable': 'carrot', 'mineral': 'copper'} + >>> obj1 = {'animal': 'cat', 'vegetable': 'carrot', 'mineral': 'topaz'} >>> obj2 = ContextStack({'vegetable': 'spinach', 'mineral': 'silver'}) >>> >>> context = ContextStack.create(obj1, None, obj2, mineral='gold') @@ -167,8 +168,8 @@ def create(*context, **kwargs): Arguments: - *context: zero or more dictionaries, ContextStack instances, or objects - with which to populate the initial context stack. None + *context: zero or more dictionaries, ContextStack instances, or + objects with which to populate the initial context stack. None arguments will be skipped. Items in the *context list are added to the stack in order so that later items in the argument list take precedence over earlier items. This behavior is the @@ -283,7 +284,7 @@ def get(self, name): raise KeyNotFoundError(name, "first part") for part in parts[1:]: - # The full context stack is not used to resolve the remaining parts. + # The full context stack isn't used to resolve the remaining parts. # From the spec-- # # 5) If any name parts were retained in step 1, each should be @@ -295,7 +296,7 @@ def get(self, name): # TODO: make sure we have a test case for the above point. result = _get_value(result, part) # TODO: consider using EAFP here instead. - # http://docs.python.org/glossary.html#term-eafp + # http://docs.python.org/glossary.html#term-eafp if result is _NOT_FOUND: raise KeyNotFoundError(name, "missing %s" % repr(part)) diff --git a/pystache/defaults.py b/pystache/defaults.py old mode 100644 new mode 100755 diff --git a/pystache/init.py b/pystache/init.py old mode 100644 new mode 100755 diff --git a/pystache/loader.py b/pystache/loader.py old mode 100644 new mode 100755 diff --git a/pystache/locator.py b/pystache/locator.py old mode 100644 new mode 100755 diff --git a/pystache/parsed.py b/pystache/parsed.py old mode 100644 new mode 100755 diff --git a/pystache/parser.py b/pystache/parser.py old mode 100644 new mode 100755 index c6a171f0..81a79f8a --- a/pystache/parser.py +++ b/pystache/parser.py @@ -31,8 +31,10 @@ 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. - ['Hey ', _SectionNode(key='who', index_begin=12, index_end=21, parsed=[_EscapeNode(key='name'), '!'])] + >>> # This is a hack to get the test to pass both in Python 2 and 3. + >>> print str(parsed).replace('u', '') + ['Hey ', _SectionNode(key='who', index_begin=12, + index_end=21, parsed=[_EscapeNode(key='name'), '!'])] """ if type(template) is not unicode: @@ -64,7 +66,11 @@ def _compile_template_re(delimiters): (?P[%(tag_types)s]?) \s* (?P[\s\S]+?) ) \s* %(ctag)s - """ % {'tag_types': tag_types, 'otag': re.escape(delimiters[0]), 'ctag': re.escape(delimiters[1])} + """ % { + 'tag_types': tag_types, + 'otag': re.escape(delimiters[0]), + 'ctag': re.escape(delimiters[1]) + } return re.compile(tag, re.VERBOSE) @@ -177,7 +183,8 @@ class _SectionNode(object): # TODO: the template_ and parsed_template_ arguments don't both seem # to be necessary. Can we remove one of them? For example, if # callable(data) is True, then the initial parsed_template isn't used. - def __init__(self, key, parsed, delimiters, template, index_begin, index_end): + def __init__(self, key, parsed, delimiters, template, index_begin, + index_end): self.delimiters = delimiters self.key = key self.parsed = parsed @@ -210,7 +217,9 @@ def render(self, engine, context): # # TODO: should we check the arity? val = val(self.template[self.index_begin:self.index_end]) - val = engine._render_value(val, context, delimiters=self.delimiters) + val = engine._render_value( + val, context, delimiters=self.delimiters + ) parts.append(val) continue @@ -287,15 +296,18 @@ def parse(self, template): # Standalone (non-interpolation) tags consume the entire line, # both leading whitespace and trailing newline. - did_tag_begin_line = match_index == 0 or template[match_index - 1] in END_OF_LINE_CHARACTERS - did_tag_end_line = end_index == len(template) or template[end_index] in END_OF_LINE_CHARACTERS + did_tag_begin_line = match_index == 0 or\ + template[match_index - 1] in END_OF_LINE_CHARACTERS + did_tag_end_line = end_index == len(template) or\ + template[end_index] in END_OF_LINE_CHARACTERS is_tag_interpolating = tag_type in ['', '&'] - if did_tag_begin_line and did_tag_end_line and not is_tag_interpolating: - if end_index < len(template): - end_index += template[end_index] == '\r' and 1 or 0 - if end_index < len(template): - end_index += template[end_index] == '\n' and 1 or 0 + if did_tag_begin_line and did_tag_end_line: + if not is_tag_interpolating: + if end_index < len(template): + end_index += template[end_index] == '\r' and 1 or 0 + if end_index < len(template): + end_index += template[end_index] == '\n' and 1 or 0 elif leading_whitespace: match_index += len(leading_whitespace) leading_whitespace = '' @@ -317,17 +329,26 @@ def parse(self, template): if tag_type == '/': if tag_key != section_key: - raise ParsingError("Section end tag mismatch: %s != %s" % (tag_key, section_key)) + raise ParsingError( + "Section end tag mismatch: %s != %s" % ( + tag_key, + section_key + ) + ) # Restore previous state with newly found section data. parsed_section = parsed_template - (tag_type, section_start_index, section_key, parsed_template) = states.pop() - node = self._make_section_node(template, tag_type, tag_key, parsed_section, - section_start_index, match_index) + (tag_type, section_start_index, + section_key, parsed_template) = states.pop() + node = self._make_section_node(template, tag_type, tag_key, + parsed_section, + section_start_index, + match_index) else: - node = self._make_interpolation_node(tag_type, tag_key, leading_whitespace) + node = self._make_interpolation_node(tag_type, tag_key, + leading_whitespace) parsed_template.add(node) @@ -342,7 +363,7 @@ def _make_interpolation_node(self, tag_type, tag_key, leading_whitespace): Create and return a non-section node for the parse tree. """ - # TODO: switch to using a dictionary instead of a bunch of ifs and elifs. + # TODO: switch to using a dictionary instead of a bunch of ifs/elifs. if tag_type == '!': return _CommentNode() @@ -360,7 +381,9 @@ def _make_interpolation_node(self, tag_type, tag_key, leading_whitespace): if tag_type == '>': return _PartialNode(tag_key, leading_whitespace) - raise Exception("Invalid symbol for interpolation tag: %s" % repr(tag_type)) + raise Exception( + "Invalid symbol for interpolation tag: %s" % repr(tag_type) + ) def _make_section_node(self, template, tag_type, tag_key, parsed_section, section_start_index, section_end_index): @@ -370,7 +393,8 @@ def _make_section_node(self, template, tag_type, tag_key, parsed_section, """ if tag_type == '#': return _SectionNode(tag_key, parsed_section, self._delimiters, - template, section_start_index, section_end_index) + template, section_start_index, + section_end_index) if tag_type == '^': return _InvertedNode(tag_key, parsed_section) diff --git a/pystache/renderengine.py b/pystache/renderengine.py old mode 100644 new mode 100755 diff --git a/pystache/renderer.py b/pystache/renderer.py old mode 100644 new mode 100755 diff --git a/pystache/specloader.py b/pystache/specloader.py old mode 100644 new mode 100755 diff --git a/pystache/template_spec.py b/pystache/template_spec.py old mode 100644 new mode 100755