-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbox.py
103 lines (85 loc) · 2.75 KB
/
dbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
import contextlib
import datetime
import os
import six
import sys
import time
import unicodedata
import dropbox
from stopwatch import Stopwatch
def normalize_path(path):
while '//' in path:
path = path.replace('//', '/')
return path
def list_folder(dbx, logger, folder, subfolder):
"""List a folder.
Return a dict mapping unicode filenames to
FileMetadata|FolderMetadata entries.
"""
path = '/%s/%s' % (folder, subfolder.replace(os.path.sep, '/'))
path = normalize_path(path)
path = path.rstrip('/')
try:
with Stopwatch.stopwatch('list_folder'):
res = dbx.files_list_folder(path)
except dropbox.exceptions.ApiError as err:
logger.error('Folder listing failed for',
path, '-- assumed empty:', err)
return {}
else:
rv = {}
for entry in res.entries:
rv[entry.name] = entry
return rv
def download(dbx, logger, path):
"""Download a file.
Return the bytes of the file, or None if it doesn't exist.
"""
path = '/%s' % path.replace(os.path.sep, '/')
path = normalize_path(path)
with Stopwatch.stopwatch('download'):
try:
md, res = dbx.files_download(path)
except dropbox.exceptions.HttpError as err:
logger.error('*** HTTP error', err)
return None
data = res.content
logger.debug("{} bytes; md: {}".format(len(data), md))
return data
def upload(dbx, logger, src, dst, overwrite=False):
"""Upload a file.
Return the request response, or None in case of error.
"""
path = '/%s' % dst.replace(os.path.sep, '/')
path = normalize_path(path)
mode = (dropbox.files.WriteMode.overwrite
if overwrite
else dropbox.files.WriteMode.add)
mtime = os.path.getmtime(src)
with open(src, 'rb') as f:
data = f.read()
with Stopwatch.stopwatch('upload %d bytes' % len(data)):
try:
res = dbx.files_upload(
data, path, mode,
client_modified=datetime.datetime(*time.gmtime(mtime)[:6]),
mute=True)
except dropbox.exceptions.ApiError as err:
logger.error('*** API error', err)
return None
logger.debug("uploaded as {}".format(res.name.encode('utf8')))
return res
def delete(dbx, logger, path):
"""Delete a file/folder.
Return True if successfully delete, otherwise False.
"""
path = '/%s' % path.replace(os.path.sep, '/')
path = normalize_path(path)
with Stopwatch.stopwatch('delete'):
try:
dbx.files_delete(path)
except dropbox.exceptions.HttpError as err:
logger.error('*** HTTP error', err)
return False
return True