Skip to content

Commit

Permalink
Use BytesIO instead of StringIO and stability fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgross committed Oct 6, 2017
1 parent f42c46e commit d0ac602
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/pcloud/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from hashlib import sha1
from io import StringIO
from io import BytesIO
from os.path import basename
from pcloud.validate import RequiredParameterCheck

Expand Down Expand Up @@ -119,7 +119,7 @@ def uploadfile(self, **kwargs):
kwargs['filename'] = filename
else: # 'data' in kwargs:
filename = kwargs['filename']
files = {filename: StringIO(kwargs['data'])}
files = {filename: BytesIO(kwargs['data'])}
return self._upload('uploadfile', files, **kwargs)

@RequiredParameterCheck(('progresshash',))
Expand Down Expand Up @@ -205,7 +205,7 @@ def file_truncate(self, **kwargs):

@RequiredParameterCheck(('fd', 'data'))
def file_write(self, **kwargs):
files = {'filename': StringIO(kwargs['data'])}
files = {'filename': BytesIO(kwargs['data'])}
return self._upload('file_write', files, **kwargs)

@RequiredParameterCheck(('fd',))
Expand Down
26 changes: 19 additions & 7 deletions src/pcloud/pcloudfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from fs.enums import ResourceType
from io import BytesIO
from pcloud.api import PyCloud
from pcloud.api import O_CREAT
from pcloud.api import O_WRITE



class PCloudFile(BytesIO):
Expand All @@ -16,10 +19,13 @@ def __init__(self, pcloud, path, mode):
self.pcloud = pcloud
self.path = path
self.mode = mode
# initial_data = None
# self.fd = None
resp = self.pcloud.file_open(path=self.path)
self.fd = resp['fd']
# TODO: dependency mode and flags?
flags = O_CREAT
resp = self.pcloud.file_open(path=self.path, flags=flags)
if resp.get('result') == 0:
self.fd = resp['fd']
else:
raise OSError('pCloud error occured (%s) - %s', resp['result'], resp['error'])

def close(self):
self.pcloud.file_close(fd=self.fd)
Expand Down Expand Up @@ -130,11 +136,17 @@ def listdir(self, path):

def makedir(self, path, permissions=None, recreate=False):
self.check()
self.pcloud.createfolder(path=path)
result = self.pcloud.createfolder(path=path)
if result['result'] != 0:
raise errors.CreateFailed(
'Create of directory "{0}" failed with "{1}"'.format(
path, result['error'])
)
else: # everything is OK
return self.opendir(path)

def openbin(self, path, mode="r", buffering=-1, **options):
pass
# XXX
return PCloudFile(self.pcloud, path, mode)

def remove(self, path):
self.pcloud.deletefile(path=path)
Expand Down

0 comments on commit d0ac602

Please sign in to comment.