nic's tumblr blog

I maintain a blog for techy stuff and ramble on twitter a bit.
May 2 '10

a fileystem abstraction

I was looking for a simple filesystem abstraction for python since I have a few filing system things to test… I found a couple:

Both these look good.

But I wanted something lighter. My effort is on github

Here’s an example usage:

def show_conf_files(dirtolist, filesystem=None):
    from pyproxyfs import Filesystem
    if not filesystem:
       filesystem = Filesystem()
    files = filesystem.listdir(dirtolist)
    import re
    cfgpat = re.compile(".*\\.cfg$")
    cfg_files = [fn for fn in files if cfgpat.match(fn)]
    return cfg_files

this might be your application code; you could then test it relatively simply:

def test_show_conf_files():
    """
    >>> test_show_conf_files()
    ['a.cfg', 'b.cfg']
    """
    from pyproxyfs import TestFS
    fs = TestFS({
        "somedir/a.cfg": "",
        "somedir/a.txt": "",
        "somedir/b.cfg": "",
        "somedir/run.py": ""
        })
    return show_conf_files("somedir", filesystem=fs)

This will obviously be a whole lot better with dictionary protocol to explode the keys automatically. That’ll be something I’ll do next.

I’m using this code right now in my maildir client to prove that it does the relatively complex filesystem things (maildir does all sorts of interesting file renaming for flags and such).

My maildir test looks like this so far:

def test_maildirs(*args):
     """
     >>> test_maildirs().keys()
     ['1270028940']

     >>> test_maildirs()["1270028940"]
     MdMessage____1270028940

     >>> test_maildirs("set_seen","1270028940")["1270028940"].is_seen
     True
     """
     
     folder = MdFolder(
         "testmaildir", 
         filesystem = TestFS({
                 "testmaildir/cur/": "",
                 "testmaildir/new/1270028940.V801Ie8c95dM583793": "mail content",
                 })
         )
     if "set_seen" in args:
         folder[args[-1]].is_seen = True
     return folder

Pretty simple but it’s already found a bunch of problems. I’m pleased.

update…

PyProxyFS is now on pypi.