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

HamlPy cannot run from library.zip #149

Open
a1s opened this issue Oct 2, 2013 · 3 comments
Open

HamlPy cannot run from library.zip #149

a1s opened this issue Oct 2, 2013 · 3 comments

Comments

@a1s
Copy link

a1s commented Oct 2, 2013

HamlPy fails to load when an application is run from a packed Python library, such as one produced by py2exe:

Traceback (most recent call last):
  File "django\core\handlers\base.pyo", line 140, in get_response
  File "django\template\response.pyo", line 105, in render
  File "django\template\response.pyo", line 80, in rendered_content
  File "django\template\response.pyo", line 58, in resolve_template
  File "django\template\loader.pyo", line 146, in get_template
  File "django\template\loader.pyo", line 129, in find_template
  File "django\template\loader.pyo", line 96, in find_template_loader
  File "django\utils\importlib.pyo", line 35, in import_module
  File "hamlpy\template\__init__.pyo", line 1, in <module>
  File "hamlpy\template\loaders.pyo", line 64, in <module>
  File "hamlpy\template\utils.pyo", line 17, in get_django_template_loaders
  File "hamlpy\template\utils.pyo", line 22, in get_submodules
  File "hamlpy\template\utils.pyo", line 29, in package_contents
WindowsError: [Error 3] The system cannot find the path specified: 'X:\\path\\build\\dist.win32\\bin\\library.zip\\django\\template\\loaders/*.*'
@a1s
Copy link
Author

a1s commented Oct 2, 2013

This patch effectively disables wrapping for all Django template loaders at HamlPy module load time when Django modules do not reside on file system:

=== modified file 'hamlpy/template/utils.py'
--- hamlpy/template/utils.py    2013-05-09 01:59:39 +0000
+++ hamlpy/template/utils.py    2013-10-02 08:18:29 +0000
@@ -1,6 +1,6 @@
 import imp
 from os import listdir
-from os.path import dirname, splitext
+from os.path import dirname, isdir, splitext

 try:
   from django.template import loaders
@@ -25,7 +25,10 @@

 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)])
+    if isdir(package_path):
+        contents = set([splitext(module)[0]
+                for module in listdir(package_path)
+                if module.endswith(MODULE_EXTENSIONS)])
+    else:
+        contents = set()
     return contents

With this change, one cannot import an arbitrary Django loader from hamlpy.template.

Loaders hamlpy.template.loaders.HamlPyFilesystemLoader and hamlpy.template.loaders.HamlPyAppDirectoriesLoader are not affected by this patch and work as expected as soon as module loading error is fixed.

@a1s
Copy link
Author

a1s commented Oct 2, 2013

By the way, I think more Pythonic approach would be to provide a way for explicit wrapping of needed loaders (in a way similar to what Django cached loader does) and not automatically create wrappers for everything we can find on a file system.

a1s added a commit to a1s/HamlPy that referenced this issue Mar 10, 2015
@a1s
Copy link
Author

a1s commented Mar 10, 2015

This is fixed in 84abeed (courtesy of @KACAH).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant