1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
"""
fs.wrapfs.subfs
===============
An FS wrapper class for accessing just a subdirectory for an FS.
"""
from fs.wrapfs import WrapFS
from fs.errors import *
from fs.path import *
class SubFS(WrapFS):
"""A SubFS represents a sub directory of another filesystem object.
SubFS objects are returned by opendir, which effectively creates a
'sandbox' filesystem that can only access files/dirs under a root path
within its 'parent' dir.
"""
def __init__(self, wrapped_fs, sub_dir):
self.sub_dir = abspath(normpath(sub_dir))
super(SubFS,self).__init__(wrapped_fs)
def _encode(self, path):
return pathjoin(self.sub_dir, relpath(normpath(path)))
def _decode(self, path):
return abspath(normpath(path))[len(self.sub_dir):]
def __str__(self):
return "<SubFS: %s in %s>" % (self.sub_dir, self.wrapped_fs)
def __unicode__(self):
return u"<SubFS: %s in %s>" % (self.sub_dir, self.wrapped_fs)
def __repr__(self):
return str(self)
def desc(self, path):
if self.isdir(path):
return "Sub dir of %s" % str(self.wrapped_fs)
else:
return "File in sub dir of %s" % str(self.wrapped_fs)
def opendir(self, path):
if not self.exists(path):
raise ResourceNotFoundError(path)
path = self._encode(path)
return self.wrapped_fs.opendir(path)
def close(self):
self.closed = True
def removedir(self, path, recursive=False, force=False):
# Careful not to recurse outside the subdir
path = normpath(path)
if path in ("","/"):
if not force:
for path2 in self.listdir(path):
raise DirectoryNotEmptyError(path)
else:
for path2 in self.listdir(path,absolute=True,files_only=True):
try:
self.remove(path2)
except ResourceNotFoundError:
pass
for path2 in self.listdir(path,absolute=True,dirs_only=True):
try:
self.removedir(path2,force=True)
except ResourceNotFoundError:
pass
else:
super(SubFS,self).removedir(path,force=force)
if recursive:
try:
self.removedir(dirname(path),recursive=True)
except DirectoryNotEmptyError:
pass
|