lesscode.org


'Python' Archives

Wanted: eglob.py  5

Cat.: Wanted, Python
01. July 2005

I’d really like to see an enhanced glob module. Nothing too crazy, just support for recursive wildcards and maybe a nice filtering API. Here’s your test case:

>>> import eglob

… or whatever.

A find function should return an iterator over all matching files and directories. Note that it should be possible to do recursive searches as the iterator is moving. yield kicks so much ass right here.

>>> eglob.find('/etc/**')
<generator object at ...>

Being able to filter the initial glob with such operations as exclude and include (needed?) would be nice. Designing this will be fun - try to abuse chaining generators as much as possible. :)

>>> list(eglob.find('/etc/**').exclude('passwd', 'group', 'init.d/*'))
['/etc/hosts', '/etc/httpd', '/etc/httpd/conf/httpd.conf']

I should be able to pass a extended glob (str, unicode) or a compiled regular expression (sre.SRE_Pattern) to any finding or filtering functions:

>>> list(eglob.find(re.compile(r'^/tmp/.*')))
['/tmp/mysql.sock', '/tmp/foo/bling']

I’d like to filter for directories only or files only:

>>> list(eglob.find('/home/*', directories=1))
['/home/hurly', '/home/curly', '/home/moe']
>>> eglob.find('**/.cvsignore', files=1)

This would be hugely useful in about four projects I’m currently working on.

An Emacs kid-mode  3

Cat.: Python
01. July 2005

I’ve been meaning to take a look at using mmm-mode to get support for < ?python?> blocks in kid templates.

After playing around with wordpress for a few hours, I grabbed php-mode and saw just how easy it is to do sub-modes in emacs. Here’s a simple variation on php-mode’s suggested emacs setup that enables python block support in templates (note that you’ll also need python-mode.el):

;; kid-html-mode
(mmm-add-mode-ext-class nil "\\.kid?\\'" 'html-kid)
(mmm-add-classes
 '((html-kid
    :submode python-mode
    :front "< \\?\\(python\\)?"
    :back "\\?>")))
(add-to-list 'auto-mode-alist '("\\.kid?\\'" . html-mode))

Add that to your .emacs file and then load up a kid template.

Not only do you get syntax coloring but all the other great python-mode.

What sucks about this is that I can’t seem to get it to work reliably with James Clark’s excellent nxml-mode, which I prefer when working with kid templates. I’m trying something like this but it seems flaky:

;; kid-nxml-mode
(mmm-add-mode-ext-class nil "\\.kid?\\'" 'nxml-kid)
(mmm-add-classes
 '((nxml-kid
    :submode python-mode
    :front "< \\?\\(python\\)?"
    :back "\\?>")))
(add-to-list 'auto-mode-alist '("\\.kid?\\'" . nxml-mode))

My suspicion is that this has something to do with the way nxml-mode is loaded, which is kind of weird because I believe it sniffs for a DOCTYPE and/or < ?xml?> declaration.