Skip to content

Commit

Permalink
fix the write in double bug
Browse files Browse the repository at this point in the history
  • Loading branch information
marinechaput committed May 3, 2020
1 parent c93e5e4 commit 564d08c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
21 changes: 18 additions & 3 deletions Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"outputs": [],
"source": [
"import dropboxdrivefs as dbx\n",
"from fklab.io.cloud import dropboxAPI\n",
"import logging"
]
},
Expand All @@ -24,8 +23,7 @@
"metadata": {},
"outputs": [],
"source": [
"db = dropboxAPI()\n",
"fs = dbx.DropboxDriveFileSystem(token=db.load_token())"
"fs = dbx.DropboxDriveFileSystem(token=\"****************\")"
]
},
{
Expand Down Expand Up @@ -129,6 +127,23 @@
" f.write('hello')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Be careful, the mode \"append\" does not append the existing file. It creates a new file with the text wrotten by adding (2) in the path name. If this new file is identical with the existing file, nothing is uploaded. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with fs.open(\"/test_dropbox/test.txt\", mode='a') as f:\n",
" f.write('hello')"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
24 changes: 19 additions & 5 deletions dropboxdrivefs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(
path : str
file path to inspect in dropbox
mode: str
Normal file modes.'rb' or 'wb'
Normal file modes.'rb', 'wb' or 'ab'
block_size: int or None
The amount of read-ahead to do, in bytes. Default is 5MB, or the value
configured for the FileSystem creating this file
Expand All @@ -171,21 +171,35 @@ def __init__(
# return self.httpfile.read(length=length)

def _upload_chunk(self, final=False):
self.cursor.offset += self.buffer.seek(0, 2)
if final:

self.dbx.files_upload_session_finish(
self.buffer.getvalue(), self.cursor, self.commit
)
else:
self.dbx.files_upload_session_append(

self.dbx.files_upload_session_append_v2(
self.buffer.getvalue(), self.cursor.session_id, self.cursor.offset
)

self.cursor.offset += self.buffer.seek(0, 2)

def _initiate_upload(self):
""" Initiate the upload session
"""
session = self.dbx.files_upload_session_start(self.buffer.getvalue())
self.commit = dropbox.files.CommitInfo(path=self.path)

session = self.dbx.files_upload_session_start(b"")

if "w" in self.mode:
self.commit = dropbox.files.CommitInfo(
path=self.path, mode=dropbox.files.WriteMode("overwrite", None)
)
elif "a" in self.mode:
self.commit = dropbox.files.CommitInfo(
path=self.path, mode=dropbox.files.WriteMode("add")
)

self.cursor = dropbox.files.UploadSessionCursor(
session_id=session.session_id, offset=self.offset
)
print(self.offset)

0 comments on commit 564d08c

Please sign in to comment.