-
Notifications
You must be signed in to change notification settings - Fork 3
/
google_utils.py
74 lines (56 loc) · 1.86 KB
/
google_utils.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2013-11-25
@author: Martin H. Bramwell
'''
from persistence import store, close_store, make_store, reopen_store
from gspread import login, authorize
store = None
if not store:
store = make_store()
expr = "Missing or misformed configuration file(s)."
msg = "Either a file named 'creds_oa.py' or a file named 'creds_up.py' must be"
msg = " present in order to authenticate your connection to Google."
credentials = {}
try:
credentials = store['credentials']
print "Will connect with shelved credentials"
except KeyError:
try:
from creds_oa import credentials
print "Will try with OAuth credentials"
except ImportError:
try:
from creds_up import credentials
print "Will try with UID/PWD credentials"
except ImportError:
print "Configuration error: {} {}".format(expr, msg)
exit(-1)
store['credentials'] = credentials
reopen_store()
print "The creds_*.py file(s) may now be deleted."
OAUTH = "oauth"
UID_PWD = "uidpwd"
def getPumpCredentials(wkbk):
shtCreds = wkbk.worksheet("Creds")
lstlstCreds = shtCreds.get_all_values()
creds = {t[0]: t[1] for t in lstlstCreds}
return creds
class Google(object):
connection = None
def __init__(self):
global connection
print "Getting connection."
if credentials['cred_type'] == OAUTH:
connection = authorize(
credentials['access_token'], credentials['key_ring'])
print "Will authorize."
elif credentials['cred_type'] == UID_PWD:
connection = login(credentials['uid'], credentials['pwd'])
print "Will log in."
else:
print "Configuration error: {} {}".format(expr, msg)
exit(-1)
def connect(self):
return connection