summaryrefslogtreecommitdiff
path: root/fs/base.py
diff options
context:
space:
mode:
authorwillmcgugan <willmcgugan@67cdc799-7952-0410-af00-57a81ceafa0f>2011-12-26 20:52:33 +0000
committerwillmcgugan <willmcgugan@67cdc799-7952-0410-af00-57a81ceafa0f>2011-12-26 20:52:33 +0000
commitb0885a312f4f57c67758850be245db5a34e5c71f (patch)
treeb332b5ce21a5842eea21fa68a627c859d12234d2 /fs/base.py
parentfafea9afc1fc576062c5c8070ec46ace8183be49 (diff)
downloadpyfilesystem-b0885a312f4f57c67758850be245db5a34e5c71f.tar.gz
First stab at a Python3 port
git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@724 67cdc799-7952-0410-af00-57a81ceafa0f
Diffstat (limited to 'fs/base.py')
-rw-r--r--fs/base.py137
1 files changed, 85 insertions, 52 deletions
diff --git a/fs/base.py b/fs/base.py
index 9b4a0fa..4531be9 100644
--- a/fs/base.py
+++ b/fs/base.py
@@ -35,6 +35,10 @@ from fs.path import *
from fs.errors import *
from fs.local_functools import wraps
+import compatibility
+import six
+from six import PY3
+
class DummyLock(object):
"""A dummy lock object that doesn't do anything.
@@ -703,7 +707,7 @@ class FS(object):
return sys_path
- def getcontents(self, path):
+ def getcontents(self, path, mode="rb"):
"""Returns the contents of a file as a string.
:param path: A path of file to read
@@ -712,13 +716,13 @@ class FS(object):
"""
f = None
try:
- f = self.open(path, "rb")
+ f = self.open(path, mode)
contents = f.read()
return contents
finally:
if f is not None:
f.close()
-
+
def setcontents(self, path, data, chunk_size=1024 * 64):
"""A convenience method to create a new file from a string or file-like object
@@ -731,23 +735,7 @@ class FS(object):
if not data:
self.createfile(path)
else:
- f = None
- try:
- f = self.open(path, 'wb')
- if hasattr(data, "read"):
- read = data.read
- write = f.write
- chunk = read(chunk_size)
- while chunk:
- write(chunk)
- chunk = read(chunk_size)
- else:
- f.write(data)
- if hasattr(f, 'flush'):
- f.flush()
- finally:
- if f is not None:
- f.close()
+ compatibility.copy_file_to_fs(data, self, path, chunk_size=chunk_size)
def setcontents_async(self,
path,
@@ -777,42 +765,87 @@ class FS(object):
if progress_callback is None:
progress_callback = lambda bytes_written:None
+ finished_event = threading.Event()
def do_setcontents():
- try:
- f = None
+ if PY3:
try:
- f = self.open(path, 'wb')
- progress_callback(0)
-
- if hasattr(data, "read"):
- bytes_written = 0
- read = data.read
- write = f.write
- chunk = read(chunk_size)
- while chunk:
- write(chunk)
- bytes_written += len(chunk)
- progress_callback(bytes_written)
+ f = None
+ try:
+ progress_callback(0)
+
+ if hasattr(data, "read"):
+ bytes_written = 0
+ read = data.read
+ chunk = read(chunk_size)
+ if isinstance(chunk, six.text_type):
+ f = self.open(path, 'w')
+ else:
+ f = self.open(path, 'wb')
+ write = f.write
+
+ while chunk:
+ write(chunk)
+ bytes_written += len(chunk)
+ progress_callback(bytes_written)
+ chunk = read(chunk_size)
+ else:
+ if isinstance(data, six.text_type):
+ f = self.open(path, 'w')
+ else:
+ f = self.open(path, 'wb')
+ f.write(data)
+ progress_callback(len(data))
+
+ if finished_callback is not None:
+ finished_callback()
+
+ finally:
+ if f is not None:
+ f.close()
+
+ except Exception, e:
+ if error_callback is not None:
+ error_callback(e)
+ raise
+
+ finally:
+ finished_event.set()
+
+ else:
+ try:
+ f = None
+ try:
+ f = self.open(path, 'wb')
+ progress_callback(0)
+
+ if hasattr(data, "read"):
+ bytes_written = 0
+ read = data.read
+ write = f.write
chunk = read(chunk_size)
- else:
- f.write(data)
- progress_callback(len(data))
-
- if finished_callback is not None:
- finished_callback()
-
+ while chunk:
+ write(chunk)
+ bytes_written += len(chunk)
+ progress_callback(bytes_written)
+ chunk = read(chunk_size)
+ else:
+ f.write(data)
+ progress_callback(len(data))
+
+ if finished_callback is not None:
+ finished_callback()
+
+ finally:
+ if f is not None:
+ f.close()
+
+ except Exception, e:
+ if error_callback is not None:
+ error_callback(e)
+
finally:
- if f is not None:
- f.close()
-
- except Exception, e:
- if error_callback is not None:
- error_callback(e)
-
- finally:
- finished_event.set()
-
- finished_event = threading.Event()
+ finished_event.set()
+
threading.Thread(target=do_setcontents).start()
return finished_event