Skip to content

PEP 8 for source #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified pystache/__init__.py
100644 → 100755
Empty file.
Empty file modified pystache/commands/__init__.py
100644 → 100755
Empty file.
Empty file modified pystache/commands/render.py
100644 → 100755
Empty file.
Empty file modified pystache/commands/test.py
100644 → 100755
Empty file.
6 changes: 4 additions & 2 deletions pystache/common.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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')))


Expand Down
17 changes: 9 additions & 8 deletions pystache/context.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))

Expand Down
Empty file modified pystache/defaults.py
100644 → 100755
Empty file.
Empty file modified pystache/init.py
100644 → 100755
Empty file.
Empty file modified pystache/loader.py
100644 → 100755
Empty file.
Empty file modified pystache/locator.py
100644 → 100755
Empty file.
Empty file modified pystache/parsed.py
100644 → 100755
Empty file.
64 changes: 44 additions & 20 deletions pystache/parser.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -64,7 +66,11 @@ def _compile_template_re(delimiters):
(?P<tag>[%(tag_types)s]?) \s* (?P<tag_key>[\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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 = ''
Expand All @@ -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)

Expand All @@ -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()

Expand All @@ -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):
Expand All @@ -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)
Expand Down
Empty file modified pystache/renderengine.py
100644 → 100755
Empty file.
Empty file modified pystache/renderer.py
100644 → 100755
Empty file.
Empty file modified pystache/specloader.py
100644 → 100755
Empty file.
Empty file modified pystache/template_spec.py
100644 → 100755
Empty file.