Skip to content

Commit

Permalink
Refactor by moving the circular import loop control at the right level
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Jul 23, 2014
1 parent 71522ab commit d071dc8
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions app_namespace/loader.py
Expand Up @@ -25,13 +25,19 @@ class Loader(BaseLoader):

def __init__(self, *args, **kwargs):
super(Loader, self).__init__(self, *args, **kwargs)
self.path_already_used = []
self._already_used = []

def reset(self):
"""
Empty the cache of paths already used.
"""
self.path_already_used = []
self._already_used = []

def get_app_template_path(self, app, template_path):
"""
Return the full path of a template located in an app.
"""
return safe_join(self.app_templates_dirs[app], template_path)

@cached_property
def app_templates_dirs(self):
Expand Down Expand Up @@ -72,31 +78,29 @@ def load_template_source(self, template_name, template_dirs=None):

if app == '':
for app in self.app_templates_dirs:
file_path = self.get_app_template_path(app, template_path)
if file_path in self._already_used:
continue
try:
return self.load_template_source_inner(
template_name, app, template_path, True)
template = self.load_template_source_inner(
template_name, app, template_path)
self._already_used.append(file_path)
return template
except TemplateDoesNotExist:
pass
raise TemplateDoesNotExist(template_name)
else:
return self.load_template_source_inner(

return self.load_template_source_inner(
template_name, app, template_path)

def load_template_source_inner(self, template_name, app, template_path,
empty_namespace=False):
def load_template_source_inner(self, template_name, app, template_path):
"""
Try to load 'template_path' in the templates directory of 'app'.
"""
try:
file_path = safe_join(self.app_templates_dirs[app],
template_path)
if empty_namespace and file_path in self.path_already_used:
raise TemplateDoesNotExist(template_name)
file_path = self.get_app_template_path(app, template_path)
with open(file_path, 'rb') as fp:
template = fp.read().decode(settings.FILE_CHARSET)
if empty_namespace:
self.path_already_used.append(file_path)
return (template, 'app_namespace:%s:%s' % (app, file_path))

except (IOError, KeyError, ValueError):
raise TemplateDoesNotExist(template_name)

0 comments on commit d071dc8

Please sign in to comment.