Skip to content

Commit 0c3657a

Browse files
author
marinechaput
committed
fix bugs with the ls and write api
1 parent b308691 commit 0c3657a

File tree

6 files changed

+136
-61
lines changed

6 files changed

+136
-61
lines changed

Example.ipynb

+129-58
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
"outputs": [],
1515
"source": [
1616
"import dropboxdrivefs as dbx\n",
17-
"import logging"
17+
"import logging\n",
18+
"\n",
19+
"logging.basicConfig(format=\"%(levelname)s:%(message)s\", level=logging.INFO)"
1820
]
1921
},
2022
{
2123
"cell_type": "code",
22-
"execution_count": 14,
24+
"execution_count": 2,
2325
"metadata": {},
2426
"outputs": [],
2527
"source": [
26-
"fs = dbx.DropboxDriveFileSystem(token=\"****************\")"
28+
"fs = dbx.DropboxDriveFileSystem(token=\"*****************\")"
2729
]
2830
},
2931
{
@@ -35,45 +37,124 @@
3537
},
3638
{
3739
"cell_type": "code",
38-
"execution_count": 15,
40+
"execution_count": 16,
3941
"metadata": {},
4042
"outputs": [
4143
{
4244
"name": "stderr",
4345
"output_type": "stream",
4446
"text": [
4547
"INFO:Request to files/create_folder_v2\n",
46-
"INFO:The directory/test_dropbox has been created.\n"
48+
"INFO:The directory/Data/test_dropbox has been created.\n"
49+
]
50+
}
51+
],
52+
"source": [
53+
"fs.mkdir(\"/Data/test_dropbox\")"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 17,
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"name": "stderr",
63+
"output_type": "stream",
64+
"text": [
65+
"INFO:Request to files/list_folder\n"
66+
]
67+
},
68+
{
69+
"data": {
70+
"text/plain": [
71+
"[{'name': '/Data/test_dropbox', 'size': None, 'type': 'directory'}]"
72+
]
73+
},
74+
"execution_count": 17,
75+
"metadata": {},
76+
"output_type": "execute_result"
77+
}
78+
],
79+
"source": [
80+
"fs.ls(\"/Data/test_dropbox\")"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 18,
86+
"metadata": {},
87+
"outputs": [
88+
{
89+
"name": "stderr",
90+
"output_type": "stream",
91+
"text": [
92+
"INFO:Request to files/get_metadata\n"
4793
]
94+
},
95+
{
96+
"data": {
97+
"text/plain": [
98+
"{'name': '/Data/test_dropbox', 'size': None, 'type': 'directory'}"
99+
]
100+
},
101+
"execution_count": 18,
102+
"metadata": {},
103+
"output_type": "execute_result"
48104
}
49105
],
50106
"source": [
51-
"fs.mkdir(\"/test_dropbox\")"
107+
"fs.info(\"/Data/test_dropbox\")"
52108
]
53109
},
54110
{
55111
"cell_type": "markdown",
56112
"metadata": {},
57113
"source": [
58-
"## Upload a file"
114+
"## Upload"
59115
]
60116
},
61117
{
62118
"cell_type": "code",
63-
"execution_count": 16,
119+
"execution_count": 19,
120+
"metadata": {},
121+
"outputs": [
122+
{
123+
"name": "stderr",
124+
"output_type": "stream",
125+
"text": [
126+
"INFO:Request to files/upload_session/start\n",
127+
"INFO:Request to files/upload_session/finish\n"
128+
]
129+
}
130+
],
131+
"source": [
132+
"# Upload a file\n",
133+
"\n",
134+
"fs.put_file(\"example_file/test1.txt\", \"/Data/test_dropbox/text1.txt\")"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 23,
64140
"metadata": {},
65141
"outputs": [
66142
{
67143
"name": "stderr",
68144
"output_type": "stream",
69145
"text": [
146+
"INFO:Request to files/get_metadata\n",
147+
"INFO:Request to files/upload_session/start\n",
148+
"INFO:Request to files/upload_session/finish\n",
70149
"INFO:Request to files/upload_session/start\n",
71150
"INFO:Request to files/upload_session/finish\n"
72151
]
73152
}
74153
],
75154
"source": [
76-
"fs.upload(\"test1.txt\", \"/test_dropbox/test1.txt\")"
155+
"# Upload a folder with two files \n",
156+
"\n",
157+
"fs.upload(\"example_file/test_db\", \"/Data/test_dropbox/\", recursive=True)"
77158
]
78159
},
79160
{
@@ -85,20 +166,21 @@
85166
},
86167
{
87168
"cell_type": "code",
88-
"execution_count": 18,
169+
"execution_count": 8,
89170
"metadata": {},
90171
"outputs": [
91172
{
92173
"name": "stderr",
93174
"output_type": "stream",
94175
"text": [
176+
"INFO:Request to files/get_metadata\n",
95177
"INFO:Request to files/get_temporary_link\n",
96178
"INFO:Request to files/get_metadata\n"
97179
]
98180
}
99181
],
100182
"source": [
101-
"fs.download(\"/test_dropbox/test1.txt\", \"test1.txt\")"
183+
"fs.download(\"/Data/test_dropbox/text1.txt\", \"example_file/test.txt\")"
102184
]
103185
},
104186
{
@@ -110,7 +192,7 @@
110192
},
111193
{
112194
"cell_type": "code",
113-
"execution_count": 19,
195+
"execution_count": 9,
114196
"metadata": {},
115197
"outputs": [
116198
{
@@ -123,8 +205,8 @@
123205
}
124206
],
125207
"source": [
126-
"with fs.open(\"/test_dropbox/test.txt\", mode='w') as f:\n",
127-
" f.write('hello')"
208+
"with fs.open(\"/Data/test_dropbox/test.txt\", mode='w') as f:\n",
209+
" f.write('Hello')"
128210
]
129211
},
130212
{
@@ -136,12 +218,21 @@
136218
},
137219
{
138220
"cell_type": "code",
139-
"execution_count": null,
221+
"execution_count": 10,
140222
"metadata": {},
141-
"outputs": [],
223+
"outputs": [
224+
{
225+
"name": "stderr",
226+
"output_type": "stream",
227+
"text": [
228+
"INFO:Request to files/upload_session/start\n",
229+
"INFO:Request to files/upload_session/finish\n"
230+
]
231+
}
232+
],
142233
"source": [
143-
"with fs.open(\"/test_dropbox/test.txt\", mode='a') as f:\n",
144-
" f.write('hello')"
234+
"with fs.open(\"/Data/test_dropbox/test.txt\", mode='a') as f:\n",
235+
" f.write('world')"
145236
]
146237
},
147238
{
@@ -153,7 +244,7 @@
153244
},
154245
{
155246
"cell_type": "code",
156-
"execution_count": 20,
247+
"execution_count": 11,
157248
"metadata": {},
158249
"outputs": [
159250
{
@@ -163,30 +254,18 @@
163254
"INFO:Request to files/get_temporary_link\n",
164255
"INFO:Request to files/get_metadata\n"
165256
]
166-
}
167-
],
168-
"source": [
169-
"file = fs.open(\"/test_dropbox/test.txt\")"
170-
]
171-
},
172-
{
173-
"cell_type": "code",
174-
"execution_count": 21,
175-
"metadata": {},
176-
"outputs": [
257+
},
177258
{
178-
"data": {
179-
"text/plain": [
180-
"b'hellohello'"
181-
]
182-
},
183-
"execution_count": 21,
184-
"metadata": {},
185-
"output_type": "execute_result"
259+
"name": "stdout",
260+
"output_type": "stream",
261+
"text": [
262+
"b'Hello'\n"
263+
]
186264
}
187265
],
188266
"source": [
189-
"file.read()"
267+
"with fs.open(\"/Data/test_dropbox/test.txt\", mode=\"rb\") as f:\n",
268+
" print(f.read())"
190269
]
191270
},
192271
{
@@ -198,7 +277,7 @@
198277
},
199278
{
200279
"cell_type": "code",
201-
"execution_count": 22,
280+
"execution_count": 12,
202281
"metadata": {},
203282
"outputs": [],
204283
"source": [
@@ -207,33 +286,25 @@
207286
},
208287
{
209288
"cell_type": "code",
210-
"execution_count": 23,
289+
"execution_count": 13,
211290
"metadata": {},
212291
"outputs": [],
213292
"source": [
214-
"file_intake = intake.open_textfiles(\"dropbox:///test_dropbox/test.txt\",storage_options={\"token\":db.load_token()})\n"
293+
"file_intake = intake.open_textfiles(\"dropbox:///Data/test_dropbox/test.txt\",storage_options={\"token\":\"**********\"})\n"
215294
]
216295
},
217296
{
218297
"cell_type": "code",
219-
"execution_count": 24,
298+
"execution_count": 14,
220299
"metadata": {},
221300
"outputs": [
222-
{
223-
"name": "stderr",
224-
"output_type": "stream",
225-
"text": [
226-
"INFO:Request to files/get_temporary_link\n",
227-
"INFO:Request to files/get_metadata\n"
228-
]
229-
},
230301
{
231302
"data": {
232303
"text/plain": [
233-
"['hellohello']"
304+
"['Hello']"
234305
]
235306
},
236-
"execution_count": 24,
307+
"execution_count": 14,
237308
"metadata": {},
238309
"output_type": "execute_result"
239310
}
@@ -251,26 +322,26 @@
251322
},
252323
{
253324
"cell_type": "code",
254-
"execution_count": 26,
325+
"execution_count": 15,
255326
"metadata": {},
256327
"outputs": [
257328
{
258329
"name": "stderr",
259330
"output_type": "stream",
260331
"text": [
261332
"INFO:Request to files/delete_v2\n",
262-
"INFO:The directory/test_dropbox has been erased.\n"
333+
"INFO:The directory/Data/test_dropbox has been erased.\n"
263334
]
264335
}
265336
],
266337
"source": [
267-
"fs.rm(\"/test_dropbox\")"
338+
"fs.rm(\"/Data/test_dropbox\")"
268339
]
269340
}
270341
],
271342
"metadata": {
272343
"kernelspec": {
273-
"display_name": "Python 3",
344+
"display_name": "Python 3 (ipykernel)",
274345
"language": "python",
275346
"name": "python3"
276347
},
@@ -284,7 +355,7 @@
284355
"name": "python",
285356
"nbconvert_exporter": "python",
286357
"pygments_lexer": "ipython3",
287-
"version": "3.7.7"
358+
"version": "3.7.11"
288359
}
289360
},
290361
"nbformat": 4,

dropboxdrivefs/core.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os.path
23

34
import dropbox.files
45
import requests
@@ -66,7 +67,7 @@ def ls(self, path, detail=True, **kwargs):
6667
list_item = self.dbx.files_list_folder_continue(list_item.cursor)
6768
items = list_item.entries + items
6869

69-
for metadata in list_item.entries:
70+
for metadata in items:
7071
list_file.append(self._refactor_metadata(metadata, detail=detail))
7172
return list_file
7273

@@ -203,11 +204,11 @@ def _initiate_upload(self):
203204

204205
if "w" in self.mode:
205206
self.commit = dropbox.files.CommitInfo(
206-
path=self.path, mode=dropbox.files.WriteMode("overwrite", None)
207+
path=self.path, mode=dropbox.files.WriteMode("overwrite", None), autorename=True
207208
)
208209
elif "a" in self.mode:
209210
self.commit = dropbox.files.CommitInfo(
210-
path=self.path, mode=dropbox.files.WriteMode("add")
211+
path=self.path, mode=dropbox.files.WriteMode("add", None), autorename=True
211212
)
212213

213214
self.cursor = dropbox.files.UploadSessionCursor(

example_file/test1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

example_file/test_db/test1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fdsgaedsq

example_file/test_db/test2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dsqf

test/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)