-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_wrapper.py
71 lines (55 loc) · 2.07 KB
/
db_wrapper.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
import cmd
import locale
import os
import pprint
import shlex
from dropbox import client, rest, session
# XXX Fill in your consumer key and secret below
# You can find these at http://www.dropbox.com/developers/apps
APP_KEY = 'y42yz1pultu5sxv'
APP_SECRET = '7jjkbatb0fphh8e'
ACCESS_TYPE = 'dropbox' # should be 'dropbox' or 'app_folder' as configured for your app
class DropboxTerm():
def __init__(self, app_key = APP_KEY, app_secret = APP_SECRET):
self.sess = StoredSession(app_key, app_secret, access_type=ACCESS_TYPE)
try:
self.sess.load_creds()
except:
self.sess.link()
self.api_client = client.DropboxClient(self.sess)
self.current_path = ''
self.sess.load_creds()
def do_account_info(self):
"""display account information"""
f = self.api_client.account_info()
pprint.PrettyPrinter(indent=2).pprint(f)
def do_put(self, from_path, to_path):
"""
upload to dropbox
"""
from_file = open(os.path.expanduser(from_path), "rb")
self.api_client.put_file(self.current_path + "/" + to_path, from_file)
class StoredSession(session.DropboxSession):
"""a wrapper around DropboxSession that stores a token to a file on disk"""
TOKEN_FILE = "token_store.txt"
def load_creds(self):
stored_creds = open(self.TOKEN_FILE).read()
self.set_token(*stored_creds.split('|'))
print "[loaded access token]"
def write_creds(self, token):
f = open(self.TOKEN_FILE, 'w')
f.write("|".join([token.key, token.secret]))
f.close()
def delete_creds(self):
os.unlink(self.TOKEN_FILE)
def link(self):
request_token = self.obtain_request_token()
url = self.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()
self.obtain_access_token(request_token)
self.write_creds(self.token)
def unlink(self):
self.delete_creds()
session.DropboxSession.unlink(self)