Add functionality for attaching objects to directories

master
Sven Slootweg 12 years ago
parent a89b36c34a
commit b5f013ddd0

@ -107,6 +107,40 @@ class Filesystem:
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)
def attach(self, obj, directory, filename, **kwargs):
try:
obj.readcap
except KeyError:
raise ObjectException("No valid object was specified.")
try:
directory.readcap
except KeyError:
raise ObjectException("No valid tahoepy.Directory was specified.")
if directory.writable == False:
raise ObjectException("The specified directory is not writable.")
filename = self._sanitize_filename(filename)
if "writable" in kwargs:
if kwargs["writable"] == True:
if obj.writable == True:
filecap = obj.writecap
else:
raise ObjectException("Cannot attach object as writable file; the object is not writable.")
else:
filecap = obj.readcap
else:
filecap = obj.readcap
result = requests.put("%s/uri/%s/%s?t=uri&replace=false" % (self.url, directory.writecap, filename), data=filecap)
if result.status_code == 200:
return filename
else:
raise ObjectException("Could not attach object - the request failed with code %d." % result.status_code)
class Directory: class Directory:
mutable = False mutable = False
@ -188,6 +222,9 @@ class Directory:
filename = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(15)) filename = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(15))
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.")
def attach(self, obj, filename, **kwargs):
return self.filesystem.attach(obj, self, filename, **kwargs)
class File: class File:
mutable = False mutable = False
@ -237,3 +274,6 @@ class File:
mutable = "immutable" mutable = "immutable"
return "<pytahoe.File %s (%s, %s)>" % (self.uri, mutable, state) return "<pytahoe.File %s (%s, %s)>" % (self.uri, mutable, state)
def attach(self, directory, filename, **kwargs):
return self.filesystem.attach(self, directory, filename, **kwargs)

@ -1,5 +1,6 @@
import pytahoe import pytahoe
fs = pytahoe.Filesystem("http://tahoe.ccnmtl.columbia.edu/")
#fs = pytahoe.Filesystem("http://localhost:3456/") #fs = pytahoe.Filesystem("http://localhost:3456/")
#print fs #print fs
#tdir = fs.Directory("URI:DIR2:jjw572jvowd473fo2n7rw6uiai:hloglrouhwgjpubcpyq5nrb4ezyijdfiboe3hquadgzjrmkdikxa") #tdir = fs.Directory("URI:DIR2:jjw572jvowd473fo2n7rw6uiai:hloglrouhwgjpubcpyq5nrb4ezyijdfiboe3hquadgzjrmkdikxa")
@ -10,5 +11,7 @@ import pytahoe
#print fs.create_directory().mount("test") #print fs.create_directory().mount("test")
#print fs.upload("test.py") #print fs.upload("test.py")
fs = pytahoe.Filesystem("http://tahoe.ccnmtl.columbia.edu/") tdir = fs.Directory("URI:DIR2:cbk47f5lybaj5qh6bm6eedezwe:m525plntx47u44xvf44r6rliec3gp6yeyio7olndibtke75zb6fa")
print fs.Directory("URI:DIR2:cbk47f5lybaj5qh6bm6eedezwe:m525plntx47u44xvf44r6rliec3gp6yeyio7olndibtke75zb6fa").children
#print tdir.attach(fs.File("URI:CHK:j3jkuy73tnkj7glyatasluq2xe:hcluqizbrhdpqa7fugmiwtaxf42f2ssvk7emyyat7wm4xr3ehgfa:3:10:534"), "derp.py")
print tdir.attach(fs.File("URI:CHK:j3jkuy73tnkj7glyatasluq2xe:hcluqizbrhdpqa7fugmiwtaxf42f2ssvk7emyyat7wm4xr3ehgfa:3:10:534"), "This is just a (*@Y#%)()@#&%)(*@&#% test to see how well it sanitizes a filename..py")

Loading…
Cancel
Save