From ba25546a6cedf0627ec953fef7fb7cf17d2eb93f Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Wed, 7 Nov 2012 20:49:01 +0100 Subject: [PATCH] Add File.read() --- pytahoe/__init__.py | 16 ++++++++++++++++ test.py | 21 ++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pytahoe/__init__.py b/pytahoe/__init__.py index 76f7daf..c9dc299 100644 --- a/pytahoe/__init__.py +++ b/pytahoe/__init__.py @@ -316,6 +316,7 @@ class File: """Represents a file node in a Tahoe-LAFS grid.""" mutable = False writable = False + request = None def __init__(self, filesystem, uri, data=None): """Create a new File object. @@ -384,3 +385,18 @@ class File: """ return self.filesystem.attach(self, directory, filename, **kwargs) + + def read(self, length=None): + """Read from the File and return the output. + + Keyword arguments: + length -- The amount of bytes to read. + """ + if self.request is None: + self.request = requests.get("%s/uri/%s" % (self.filesystem.url, self.uri), prefetch=False) + + if length is None: + return self.request.content + else: + return self.request.raw.read(amt=length) + diff --git a/test.py b/test.py index 2967a59..4971d79 100644 --- a/test.py +++ b/test.py @@ -1,6 +1,6 @@ import pytahoe -fs = pytahoe.Filesystem("http://tahoe.ccnmtl.columbia.edu/") + #fs = pytahoe.Filesystem("http://localhost:3456/") #print fs #tdir = fs.Directory("URI:DIR2:jjw572jvowd473fo2n7rw6uiai:hloglrouhwgjpubcpyq5nrb4ezyijdfiboe3hquadgzjrmkdikxa") @@ -11,7 +11,22 @@ fs = pytahoe.Filesystem("http://tahoe.ccnmtl.columbia.edu/") #print fs.create_directory().mount("test") #print fs.upload("test.py") -tdir = fs.Directory("URI:DIR2:cbk47f5lybaj5qh6bm6eedezwe:m525plntx47u44xvf44r6rliec3gp6yeyio7olndibtke75zb6fa") + #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") +#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") + +fs = pytahoe.Filesystem("http://tahoe.ccnmtl.columbia.edu/") + +tfile = fs.File("URI:CHK:iyq5houj2fqavmnqccqezcssle:5px5tdctmmvci4al2r3vskpblhrepuugfe6ghiuutpe6cllvo36a:3:10:997") +print tfile.read(65) + +#print fs +# +#tfile = fs.upload("test.py") +#print tfile +# +#tdir = fs.Directory("URI:DIR2:cbk47f5lybaj5qh6bm6eedezwe:m525plntx47u44xvf44r6rliec3gp6yeyio7olndibtke75zb6fa") +#print tdir +# +#print tfile.attach(tdir, "sample // file 2.py")