-
Notifications
You must be signed in to change notification settings - Fork 0
/
providerDB.py
84 lines (73 loc) · 2.8 KB
/
providerDB.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
import ConfigParser
import dropbox
from provider import Provider
import requests
from dropbox.client import ErrorResponse
from execo_engine import logger
class ProviderDB(Provider):
""" Object to store the provider ids """
def upload_file_sdk(self, client, filePath, fileName):
"""Upload a file's content.
Args:
service: Drive API service instance.
filePath : Path to the file you want to upload.
fileName : Name of the new file in the drive.
"""
f = open(filePath, 'rb')
try:
client.put_file(fileName, f)
except ErrorResponse as e:
logger.error('Error in Upload ' + str(e.status) + ' ' + e.reason + ' : ' + e.error_msg)
pass
return True
def download_file_sdk(self, client, fileName, filePath):
"""Download a file's content.
Args:
client: Dropbox client instance.
fileName: Name of the file you want to download.
filePath: Name of the new local file.
"""
try:
f, _ = client.get_file_and_metadata(fileName)
out = open(filePath, 'wb')
out.write(f.read())
out.close()
except ErrorResponse as e:
logger.error('Error in Download ' + str(e.status) + ' ' + e.reason + ' : ' + e.error_msg)
pass
return True
def getNewToken(self):
""" Get a new token for a new app """
Provider.__init__(self, 'dropbox')
print 'test de connexion a ' + self.provider_name
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(self.app_key, self.app_secret)
r = requests.get(flow.start())
if r.status_code == 200:
print "url:", flow.start()
print "Please authorize in the browser. After you're done, press enter."
auth_code = raw_input().strip()
access_token, _ = flow.finish(auth_code)
config = ConfigParser.ConfigParser()
config.readfp(open('conf.ini'))
config.set('dropbox', 'token', access_token)
else:
return False
return True
def delete_file(self, client, fname):
try:
client.file_delete(fname)
except ErrorResponse as e:
logger.error('Error in Delete ' + str(e.status) + ' ' + e.reason + ' : ' + e.error_msg)
pass
return True
def getToken(self):
if not self.token:
self.getNewToken()
return dropbox.client.DropboxClient(self.token)
def __init__(self):
"""Init the connexion to Dropbox.
Returns:
The Dropbox client.
"""
Provider.__init__(self, 'dropbox')
self.getToken()