Skip to content
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

Pep8 and clean up #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 23 additions & 22 deletions django_mustache/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,66 @@
from django.template.loaders import app_directories


# Note: cannot be a list of extensions because pystache.Renderer accepts only one.
MUSTACHE_FILE_EXTENSION = getattr(settings,
'MUSTACHE_FILE_EXTENSION',
'.mustache')
MUSTACHE_TEMPLATE_DIRS = getattr(settings,
'MUSTACHE_TEMPLATE_DIRS',
[os.path.join(settings.STATIC_ROOT, 'js', 'templates')])
# Note: cannot be a list of extensions
# because pystache.Renderer accepts only one.
MUSTACHE_FILE_EXTENSION = getattr(
settings, 'MUSTACHE_FILE_EXTENSION', '.mustache')
MUSTACHE_TEMPLATE_DIRS = getattr(
settings, 'MUSTACHE_TEMPLATE_DIRS',
[os.path.join(settings.STATIC_ROOT, 'js', 'templates')])


class MustacheTemplate(Template):
def __init__(self, source, origin=None, name=u"We will never know."):
def __init__(self, source, origin=None, name='We will never know.'):
# Note: source is parsed
self.source = source
self.name = name

@property
def nodelist(self):
# nice hack bro, makes it work with @inclusion_tag
# Nice hack bro, makes it work with @inclusion_tag
return self

def render(self, context):
context_dict = {}
for d in context.dicts: # flatten
for d in context.dicts: # Flatten
context_dict.update(d)

renderer = pystache.Renderer(search_dirs=MUSTACHE_TEMPLATE_DIRS,
file_extension=MUSTACHE_FILE_EXTENSION[1:])
renderer = pystache.Renderer(
search_dirs=MUSTACHE_TEMPLATE_DIRS,
file_extension=MUSTACHE_FILE_EXTENSION[1:])
return renderer.render(self.source, context_dict)


class MustacheLoader(app_directories.Loader):
is_usable = True
_parsed = {} # immutable! cache
_parsed = {} # Immutable! cache

def parse_mustache_template(self, origin, source):
if origin not in self._parsed or settings.DEBUG: # no cache in DEBUG mode
# No cache in DEBUG mode
if origin not in self._parsed or settings.DEBUG:
try:
self._parsed[origin] = pystache.parse(smart_unicode(source))
except pystache.parser.ParsingError, e:
# better error
raise pystache.parser.ParsingError("Mustache couldn't parse {0}, reason : '{1}'.".format(origin, e))
# Better error
raise pystache.parser.ParsingError(
"Mustache couldn't parse {0}, "
"reason : '{1}'.".format(origin, e))
return self._parsed[origin]

def load_template(self, template_name, template_dirs=None):
dummy, extension = os.path.splitext(template_name)

if extension == MUSTACHE_FILE_EXTENSION:
source, origin = self.load_template_source(template_name,
MUSTACHE_TEMPLATE_DIRS)



source, origin = self.load_template_source(
template_name, MUSTACHE_TEMPLATE_DIRS)
try:
parsed = self.parse_mustache_template(origin, source)
template = MustacheTemplate(parsed, origin=origin,
name=template_name)
return template, None
except TemplateDoesNotExist:
return source, origin

return super(MustacheLoader, self).load_template(
template_name, template_dirs)