Path tracking and node unlinking

master
Sven Slootweg 11 years ago
parent 0f470754e6
commit 6a97992294

@ -70,7 +70,7 @@ class Filesystem(object):
def __repr__(self): def __repr__(self):
return "<pytahoe.Filesystem %s>" % self.url return "<pytahoe.Filesystem %s>" % self.url
def Directory(self, uri, data=None): def Directory(self, uri, data=None, path=None):
"""Create and return a new Directory object for the specified URI for this filesystem. """Create and return a new Directory object for the specified URI for this filesystem.
uri -- The URI to represent uri -- The URI to represent
@ -85,9 +85,12 @@ class Filesystem(object):
except: except:
raise FilesystemException("Could not reach the WAPI or did not receive a valid response.") raise FilesystemException("Could not reach the WAPI or did not receive a valid response.")
return Directory(self, uri, data) if path is None:
path = uri.split("/")
def File(self, uri, data=None): return Directory(self, path, uri, data)
def File(self, uri, data=None, path=None):
"""Create and return a new File object for the specified URI for this filesystem. """Create and return a new File object for the specified URI for this filesystem.
uri -- The URI to represent uri -- The URI to represent
@ -102,9 +105,12 @@ class Filesystem(object):
except: except:
raise FilesystemException("Could not reach the WAPI or did not receive a valid response.") raise FilesystemException("Could not reach the WAPI or did not receive a valid response.")
return File(self, uri, data) if path is None:
path = uri.split("/")
return File(self, path, uri, data)
def Object(self, uri, data=None): def Object(self, uri, data=None, path=None):
"""Create and return a new Directory or File object for this filesystem, depending on what the URI represents. """Create and return a new Directory or File object for this filesystem, depending on what the URI represents.
uri -- The URI to represent uri -- The URI to represent
@ -120,9 +126,9 @@ class Filesystem(object):
raise FilesystemException("Could not reach the WAPI or did not receive a valid response.") raise FilesystemException("Could not reach the WAPI or did not receive a valid response.")
if "filenode" in data: if "filenode" in data:
return self.File(uri, data) return self.File(uri, data, path=path)
elif "dirnode" in data: elif "dirnode" in data:
return self.Directory(uri, data) return self.Directory(uri, data, path=path)
else: else:
raise ObjectException("The specified object does not appear to exist.") raise ObjectException("The specified object does not appear to exist.")
@ -130,13 +136,13 @@ class Filesystem(object):
"""Create a new directory node in the filesystem, and return a Directory object representing it.""" """Create a new directory node in the filesystem, and return a Directory object representing it."""
result = requests.post("%s/uri?t=mkdir" % self.url, {}).text result = requests.post("%s/uri?t=mkdir" % self.url, {}).text
return self.Directory(result) return self.Directory(result, path=[result])
def _sanitize_filename(self, name): def _sanitize_filename(self, name):
"""Strip all potentially unsafe characters from the given filename.""" """Strip all potentially unsafe characters from the given filename."""
return re.sub("[^a-zA-Z0-9 $_.+!*'(),-]+", "", name) return re.sub("[^a-zA-Z0-9 $_.+!*'(),-]+", "", name)
def upload(self, filedata): def upload(self, filedata, path=None):
"""Uploads a file to the storage grid and returns a File object representing it. """Uploads a file to the storage grid and returns a File object representing it.
filedata -- Either a file-like object, or the path to a file. filedata -- Either a file-like object, or the path to a file.
@ -151,7 +157,7 @@ class Filesystem(object):
raise UploadException("Cannot upload the file because the given file is not a valid file object or path.") raise UploadException("Cannot upload the file because the given file is not a valid file object or path.")
file_uri = requests.put("%s/uri" % self.url, data=filedata.read()).text file_uri = requests.put("%s/uri" % self.url, data=filedata.read()).text
return self.File(file_uri) return self.File(file_uri, path=path)
def attach(self, obj, directory, filename, **kwargs): def attach(self, obj, directory, filename, **kwargs):
"""Attaches an object to a file node in the filesystem. """Attaches an object to a file node in the filesystem.
@ -198,7 +204,11 @@ class Filesystem(object):
raise ObjectException("Could not attach object - the request failed with code %d." % result.status_code) raise ObjectException("Could not attach object - the request failed with code %d." % result.status_code)
class Node(object): class Node(object):
pass def unlink(self):
if len(self.path) < 2:
raise ObjectException("Cannot delete (unlink) object; does not have a known parent URI.")
requests.post("%s/uri/%s?t=delete&name=%s" % (self.filesystem.url, "/".join(self.path[:-1]), self.path[-1]))
class Directory(Node): class Directory(Node):
"""Represents a directory node in a Tahoe-LAFS grid. """Represents a directory node in a Tahoe-LAFS grid.
@ -210,7 +220,7 @@ class Directory(Node):
writeable = False writeable = False
children = {} children = {}
def __init__(self, filesystem, uri, data=None): def __init__(self, filesystem, path, uri, data=None):
"""Creates a new Directory object. """Creates a new Directory object.
filesystem -- The Filesystem this Directory belongs to. filesystem -- The Filesystem this Directory belongs to.
@ -222,6 +232,7 @@ class Directory(Node):
self.filesystem = filesystem self.filesystem = filesystem
self.uri = uri self.uri = uri
self.path = path
# We always need to retrieve the data for a directory. Why? Because otherwise we have no data about the children. # We always need to retrieve the data for a directory. Why? Because otherwise we have no data about the children.
self._get_data() self._get_data()
@ -266,7 +277,7 @@ class Directory(Node):
else: else:
child_uri = child_data[1]['ro_uri'] child_uri = child_data[1]['ro_uri']
self.children[child_name] = self.filesystem.Object(child_uri, child_data) self.children[child_name] = self.filesystem.Object(child_uri, child_data, path=self.path+[child_name])
elif "unknown" in data: elif "unknown" in data:
raise ObjectException("The specified object does not appear to exist.") raise ObjectException("The specified object does not appear to exist.")
@ -328,7 +339,7 @@ class Directory(Node):
else: else:
raise UploadException("The given file is not a valid string or file object.") raise UploadException("The given file is not a valid string or file object.")
new_file = self.filesystem.upload(filedata) new_file = self.filesystem.upload(filedata, path=self.path+[filename])
new_file.attach(self, filename) new_file.attach(self, filename)
return new_file return new_file
@ -355,6 +366,7 @@ class Directory(Node):
new_dir = self.filesystem.create_directory() new_dir = self.filesystem.create_directory()
new_dir.attach(self, self.filesystem._sanitize_filename(name), writable=True) new_dir.attach(self, self.filesystem._sanitize_filename(name), writable=True)
new_dir.path = self.path + [name]
return new_dir return new_dir
@ -368,7 +380,7 @@ class File(Node):
writable = False writable = False
request = None request = None
def __init__(self, filesystem, uri, data=None): def __init__(self, filesystem, path, uri, data=None):
"""Create a new File object. """Create a new File object.
filesystem -- The filesystem this File belongs to. filesystem -- The filesystem this File belongs to.
@ -380,6 +392,7 @@ class File(Node):
self.filesystem = filesystem self.filesystem = filesystem
self.uri = uri self.uri = uri
self.path = path
if data is None: if data is None:
try: try:

@ -27,15 +27,18 @@ print "Get filesystem"
#tfile = fs.upload("test.py") #tfile = fs.upload("test.py")
#print tfile #print tfile
# #
tdir = fs.Directory("URI:DIR2:73a5pbgk2wpgtjylw4x6d3p4ne:ygfn7uzmpyzrat7u2yigalhqlf77xq5uavc3fvaphsnlncoy5hnq") tdir = fs.Directory("URI:DIR2:5fe3oenfxnhgtdflhpemevse44:rz7u4gq56kxtu5toodzbm433gkaitxpdlpeoamvpygs6pxw2jzsq")
print "Get dir" #print "Get dir"
print tdir.create_directory("testing subdir4") subdir = tdir.create_directory("herp")
print "Create dir" #print "Create dir"
tdir.refresh() #tdir.refresh()
print "Refresh" #print "Refresh"
print tdir.children #print tdir.children
print "Children" #print "Children"
#print tdir.upload("test.py") f = subdir.upload("test.py")
print f
#f.unlink()
subdir.unlink()
#print tdir #print tdir
# #

Loading…
Cancel
Save