posativ / acrylamid

(unmaintained) static blog generator in python with incremental rendering ⛺
http://posativ.org/acrylamid/
Other
277 stars 40 forks source link

module discovery is slightly broken on Windows #142

Closed posativ closed 11 years ago

posativ commented 11 years ago

https://github.com/posativ/acrylamid/blob/master/acrylamid/helpers.py#L411 may not return the Acrylamid module but rather (if any) something else. This only applies to modules in a subdirectory (like the search view).

maphew commented 11 years ago

I don't understand much of what's going on in there, my python fu is weak, but I believe the error is related to replacing the dir separator /, which on Windows may be either '/' or '\', usually the latter. You could try .replace(os.sep, '.') instead(?)

#!/usr/bin/env python

import sys
import os
from os.path import join, dirname, isdir, isfile, commonprefix, normpath

PATH = 'b:/code/acrylamid'

for root, dirs, files in os.walk(PATH):
    for fname in files:
        if fname == '__init__.py':
            filename = os.path.join(root, fname)

            prefix = commonprefix((PATH, filename))
            if prefix:
                dname, x = os.path.split(filename[len(prefix):])
                dotname = dname.replace(os.sep, '.').lstrip('.')

            print('filename:\t', filename)
            print('dotname:\t', dotname)

results in Pyscripter interactive interpreter:

*** Remote Interpreter Reinitialized  ***
>>> 
[Dbg]>>> 
filename:    b:/code/acrylamid\acrylamid\__init__.py
dotname:     acrylamid
filename:    b:/code/acrylamid\acrylamid\defaults\__init__.py
dotname:     acrylamid.defaults
filename:    b:/code/acrylamid\acrylamid\defaults\html5\__init__.py
dotname:     acrylamid.defaults.html5
filename:    b:/code/acrylamid\acrylamid\defaults\shadowplay\jinja2\__init__.py
dotname:     acrylamid.defaults.shadowplay.jinja2
filename:    b:/code/acrylamid\acrylamid\filters\__init__.py
dotname:     acrylamid.filters
filename:    b:/code/acrylamid\acrylamid\lib\__init__.py
dotname:     acrylamid.lib
filename:    b:/code/acrylamid\acrylamid\specs\__init__.py
dotname:     acrylamid.specs
filename:    b:/code/acrylamid\acrylamid\tasks\__init__.py
dotname:     acrylamid.tasks
filename:    b:/code/acrylamid\acrylamid\templates\__init__.py
dotname:     acrylamid.templates
filename:    b:/code/acrylamid\acrylamid\views\__init__.py
dotname:     acrylamid.views
filename:    b:/code/acrylamid\acrylamid\views\search\__init__.py
dotname:     acrylamid.views.search

Note printed path differs from stored path:

>>> os.sep
'\\'
>>> root
'b:/code/acrylamid\\misc'
>>> print(root)
b:/code/acrylamid\misc
>>> filename
'b:/code/acrylamid\\acrylamid\\views\\search\\__init__.py'
>>> print(filename)
b:/code/acrylamid\acrylamid\views\search\__init__.py
>>> 
posativ commented 11 years ago

Basically I try to import a module by path using the imp module (compatible way for Python 2 and 3). Unfortunately you can't point imp to the filename. You have to extract the right module name (and make it possible to write your own filters/views in your blog).

If the filename is b:/code/acrylamid\acrylamid\views\searchinit.py, the module name is not search but acrylamid.views.search whereas b:/code/acrylamid\acrylamid\views\index.py is resolved to acrylamid.views.index but only if the module is inside of Acrylamid. This circumvents certain edge cases where a module is named like an already existing one (such as filters.html and html in the std library of Python 3).

My Windows-foo is still very limited, but I'll try to fix this as fast as possible.

posativ commented 11 years ago

Pushed to master and legacy/0.6 (as version 0.6.7). If you want to test the search with the provided command line:

$ acrylamid compile --search
$ python misc/search.py blog/output/search/p.js python
exact match: [0, 2, 3, 133, 8, 14, 15, 16, 19, 20, 22 ... ]
partial match: [19, 85, 158, 58, 39, 22, 23]
maphew commented 11 years ago

Many thanks Martin!

I confirm that with 0.6.7 the search files are created:

B:\www\acr>acr compile --search
...snip...
  ←[1;33m   update←[0m  [0.05s] output\acr\index.html
  ←[1;32m   create←[0m  [0.11s] output\acr\search\src\0.txt
  ←[1;32m   create←[0m  output\acr\search\src\1.txt
  ←[1;32m   create←[0m  output\acr\search\src\2.txt
...snip...

However misc/search.py doesn't exist in the pypi package:

...\Python27>dir /s/b search.py

...\Python27\Lib\site-packages\acrylamid\specs\search.py
...\Python27\Lib\site-packages\pip\commands\search.py

I found it in on the master branch, and can confirm that that does work on my system:

B:\code\acrylamid>python misc\search.py B:\www\acr\output\search\p.js python
exact match: [10, 5]
partial match: [5, 5]