?Tcl's embedded virtual filesystem and Scripted documents
jepler at unpythonic.net
jepler at unpythonic.net
Mon Jun 3 22:32:46 EDT 2002
On Mon, Jun 03, 2002 at 03:44:03PM -0700, Norman Shelley wrote:
> Tcl now has an embedded virtual filesystem that looks very useful.
> http://mini.net/tcl/2138.html
>
> Is there any motivation in the Python camp to have something similiar?
Since files in Python are objects with methods (read, write & friends), all
you need to do is have a factory for file-like objects. __builtin__.open()
uses only the real filesystem namespace, of course, but it could
potentially be replaced by another sort of factory...
I don't know if this new tcl gimmick supports stuff like 'chdir
ftp://....', but if so then chdir() would have to be replaced as well.
The following assumes posix-style names, and uses "//XXX/path..." to call
the XXX factory with the argument "path...". Names like "/XXX/path/" still
have their normal meaning.
import __builtin__
builtin_open = __builtin__.open
filetype_registry = {}
def register(name, factory):
filetype_registry[name] = factory
def open(path):
initial_slashes = path.startswith('/')
if (path.startswith('//') and not path.startswith('///')):
initial_slashes = 2
if initial_slashes != 2: return builtin_open(path)
nextslash = path.find("/", 2)
if nextslash == -1: nextslash = len(path)
factory_name = path[2:nextslash]
arg = path[nextslash+1:]
factory = filetype_registry[factory_name]
return factory(arg)
def _test():
import StringIO
def factory(path):
print "in test factory"
return StringIO.StringIO(path)
register("test", factory)
f = open("//test/this is a test")
print "the contents of the file:", `f.read()`
f.close()
f = open("/etc/issue")
print "the contents of the file:", `f.read()`
f.close()
if __name__ == '__main__': _test()
More information about the Python-list
mailing list