posativ / acrylamid

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

os.path name in Jinja filters #168

Closed hooli closed 11 years ago

hooli commented 11 years ago

Hi @posativ

The os.path name changes depending on platform ( posixpath , ntpath, riscospath ), so had to use posixpath.basename. Also unsure if modules are much using in the filters, since referencing them in a template will just cause a stack trace.

How about:

        import types
        ...

        import time, datetime, os.path as ospath
        modules = [time, datetime, ospath]
        ...

        # swap out platform specific os.path name (posixpath , ntpath, riscospath)
        ospathmodname = ospath.__name__
        ospath.__name__ = 'ospath'
        for mod in modules:
            for name in dir(mod):
                if name.startswith('_') or isinstance(getattr(mod, name), types.ModuleType):
                    continue

                self.jinja2_env.filters[mod.__name__ + '.' + name] = getattr(mod, name)

        # restore original os.path module name
        ospath.__name__ = ospathmodname
posativ commented 11 years ago

Didn't know that about os.path.__name__. I agree that modules are quite useless in the filters. But instead of renaming os.path.__name__ to ospath I set it to os.path (with a dot) just as the real module.

hooli commented 11 years ago

Cheers. Yep probably best to keep the same namespace. Considering that, and the fact you've made it dynamic, could have a config setting to let the user define extra imports. Probably on the todo list #166