1. pombredanne NA
  2. regexpfs



= RegExpFS =

Version : 0.2.0 Author : Thomas Weholt <thomas@weholt.org> License : GPL v3.0 Status : Alpha/Experimental/Proof-of-concept Url : https://bitbucket.org/weholt/regexpfs

== Install ==

pip install regexpfs


hg clone http://bitbucket.org/weholt/regexpfs

== Usage ==

from regexpfs.regexpfs import *

def date_producer(**kwargs):
# return something related to date based on what arguments are provided in kwargs
def tag_producer(**kwargs):
# return something related to a tag based on what arguments are provided in kwargs
def root_producer(**kwargs):
# return whatever you want to be in the root of the filesystem, in this example by_date and by_tag

class TestProvider(RegExpContentProvider):

patterns = (
(r'^/by_date/(?P<year>d+)/(?P<month>w+)/(?P<day>d+)/(?P<filename>(w|[.,!#%{}()@])+)$', file_producer), (r'^/by_date/(?P<year>d+)/(?P<month>w+)/(?P<day>d+)$', date_producer), (r'^/by_date/(?P<year>d+)/(?P<month>w+)$', date_producer), (r'^/by_date/(?P<year>d+)$', date_producer), (r'^/by_date$', date_producer), (r'^/by_tag/(?P<tag>w+)/(?P<file_id>d+).w+$', file_producer), (r'^/by_tag/(?P<tag>w+)$', tag_producer), (r'^/by_tag$', tag_producer), (r'^/$', root_producer), )

fs = RegExpFS(providers = [TestProvider()]) fs.printtree()

The patterns are defined as a regular expression and what method you want to produce content if a match occurr. The regular expression matches groups and pass these on to the method. For instance, the path


will produce these params {'year': '2010', 'month': 'April', 'day': '4', ... } and those will be passed on to the handler as arguments.

If the path matches a file pattern the generated params will have either a key called filename or file_id and the virtual filesystem will treat that match as a file, not a directory. Examples of file matching patterns:

(r'^/by_tag/(?P<tag>w+)/(?P<file_id>d+).w+$', file_producer), (r'^/by_date/(?P<year>d+)/(?P<month>w+)/(?P<day>d+)/(?P<filename>(w|[.,!#%{}()@])+)$', file_producer),

Everything else will be treated as directories. When matched with a filepattern the file_producer-method ( in this example ) will be called with arguments extracted from the regular expression, mode, path and **kwargs. It is suppose to return a file-like object or raise an ResourceNotFoundError. So when you call open on your filesystem and provide a path the code will look for a pattern that matches and call your handler, here called file_producer. You`ll have to check in the handler method for mode ( r = read access, w = write access ) and return a fileobject or raise an exception if write access for instance is not allowed.