[py-dev] py.path.dict and py.path.proxy patch
Matthew Scott
mscott at goldenspud.com
Sat Jan 29 06:04:43 CET 2005
Round 2!
The attached patch provides the new classes py.path.dict and
py.path.proxy, as well as py.__impl__.path.virtual.virtual.Path which is
the basis for them.
This is an attempt at taking into account some of the suggestions that
holger made regarding generalization of the py.path.embed patch I posted
in the last couple of days. This patch supersedes the embed1.patch and
embed1-1.patch files, so don't use those anymore.
What py.path.proxy allows you to do is to take an arbitrary path and
create a proxy filesystem into a limited area of that path's filesystem.
Maybe a better word would be sandbox, but I found proxy to be easier
to type. Naming suggestions are welcome. This code is set in fresh
mud, not in stone. :)
I haven't yet tried py.path.proxy using anything but py.path.local root
paths.
Here's an example of using py.path.proxy to wrap a directory specified
by py.path.local, and of using py.path.dict to create a dictionary-based
virtual file system that contains a copy of that directory:
I started IPython in a directory underneath my home directory that
contains pictures of a model of laptop that I owned years ago, the
Toshiba T1200XE. I created a 'here' variable to contain that path.
>>> import py
>>> here = py.path.local()
>>> here
--- local('/home/gldnspud/personal/www/crap/t1200xe')
Listing the directory, you can see that it has three files inside it:
>>> here.listdir()
---
[local('/home/gldnspud/personal/www/crap/t1200xe/1.jpg'),
local('/home/gldnspud/personal/www/crap/t1200xe/2.jpg'),
local('/home/gldnspud/personal/www/crap/t1200xe/3.jpg')]
Now for the fun part. I created a 'prox' variable which is a
py.path.proxy whose root is the 'here' path.
>>> prox = py.path.proxy(root=here)
>>> prox
--- proxypath('/', local('/home/gldnspud/personal/www/crap/t1200xe'))
If you list its directory, it obviously contains the same files:
>>> prox.listdir()
---
[proxypath('/1.jpg', local('/home/gldnspud/personal/www/crap/t1200xe')),
proxypath('/2.jpg', local('/home/gldnspud/personal/www/crap/t1200xe')),
proxypath('/3.jpg', local('/home/gldnspud/personal/www/crap/t1200xe'))]
If you traverse to parent directories with 'here', it goes up and up and up.
>>> here.join('..', '..')
--- local('/home/gldnspud/personal/www')
However, if you attempt the same thing using 'prox', you run into the
boundary that was defined when prox's virtual filesystem was created.
>>> prox.join('..', '..')
--- proxypath('/', local('/home/gldnspud/personal/www/crap/t1200xe'))
Now I'll create a 'virt' variable which will contain a py.path.dict
starting from an empty dictionary.
>>> virt = py.path.dict()
>>> virt
--- dictpath('/', <dict at 4064ed74>)
>>> virt.listdir()
--- []
I'll populate it with the contents of 'prox'.
>>> prox.copy(target=virt)
>>> virt.listdir()
---
[dictpath('/2.jpg', <dict at 4064ed74>),
dictpath('/3.jpg', <dict at 4064ed74>),
dictpath('/1.jpg', <dict at 4064ed74>)]
What's fun (but maybe pointless other than to show that when it isn't
pointless to do so, it can be done) is creating a proxy for a dict path,
and then a proxy for that proxy.
>>> virt.join('a', 'b', 'c').ensure(dir=True)
--- dictpath('/a/b/c', <dict at 4064ed74>)
>>> virt.listdir()
---
[dictpath('/2.jpg', <dict at 4064ed74>),
dictpath('/a', <dict at 4064ed74>),
dictpath('/3.jpg', <dict at 4064ed74>),
dictpath('/1.jpg', <dict at 4064ed74>)]
>>> prox2 = py.path.proxy(root=virt.join('a'))
>>> prox2.listdir()
--- [proxypath('/b', dictpath('/a', <dict at 4064ed74>))]
>>> prox3 = py.path.proxy(root=prox2.join('b'))
>>> prox3.listdir()
--- [proxypath('/c', proxypath('/b', dictpath('/a', <dict at 4064ed74>)))]
Removing the "c" directory in prox3 of course will result in a/b/c being
removed from virt.
>>> prox3.join('c').remove()
>>> virt.join('a', 'b').listdir()
--- []
Let's delete the jpg files from virt and create a small text file, and
see what the underlying dictionary representation of virt is. (Hint: a
'c' key stands for contents. A directory's contents is a dictionary,
and a file's contents is a string).
>>> for path in virt.listdir(fil='*.jpg'): path.remove()
...
>>> virt.listdir()
--- [dictpath('/a', <dict at 4064ed74>)]
>>> virt.join('test.txt').ensure().write('hello world')
>>> virt.join('test.txt').read()
--- 'hello world'
>>> virt.fs.dict
--- {'c': {'a': {'c': {'b': {'c': {}}}}, 'test.txt': {'c': 'hello world'}}}
Fun stuff!
- Matthew
-------------- next part --------------
A non-text attachment was scrubbed...
Name: virtual1.patch
Type: text/x-patch
Size: 31082 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20050128/d9595512/attachment.bin>
More information about the Pytest-dev
mailing list