From 84abeed87310daa9cbb4b704dccc149a2dd53be5 Mon Sep 17 00:00:00 2001 From: Alexander Smishlajev Date: Tue, 10 Mar 2015 12:59:24 +0200 Subject: [PATCH] Fix https://github.com/jessemiller/HamlPy/issues/149 --- hamlpy/template/utils.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) mode change 100644 => 100755 hamlpy/template/utils.py diff --git a/hamlpy/template/utils.py b/hamlpy/template/utils.py old mode 100644 new mode 100755 index c9187e8..cb3d399 --- a/hamlpy/template/utils.py +++ b/hamlpy/template/utils.py @@ -1,6 +1,5 @@ -import imp -from os import listdir -from os.path import dirname, splitext +from os.path import dirname +from pkgutil import iter_modules try: from django.template import loaders @@ -8,24 +7,19 @@ except ImportError, e: _django_available = False -MODULE_EXTENSIONS = tuple([suffix[0] for suffix in imp.get_suffixes()]) - def get_django_template_loaders(): if not _django_available: return [] - return [(loader.__name__.rsplit('.',1)[1], loader) + return [(loader.__name__.rsplit('.',1)[1], loader) for loader in get_submodules(loaders) if hasattr(loader, 'Loader')] - + def get_submodules(package): submodules = ("%s.%s" % (package.__name__, module) for module in package_contents(package)) - return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) + return [__import__(module, {}, {}, [module.rsplit(".", 1)[-1]]) for module in submodules] def package_contents(package): - package_path = dirname(loaders.__file__) - contents = set([splitext(module)[0] - for module in listdir(package_path) - if module.endswith(MODULE_EXTENSIONS)]) - return contents + package_path = dirname(package.__file__) + return set([name for (ldr, name, ispkg) in iter_modules([package_path])])