Skip to content

Commit e5fd643

Browse files
committed
service account constructor added
1 parent 325638d commit e5fd643

File tree

5 files changed

+27
-37
lines changed

5 files changed

+27
-37
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Google Objects
22
Thin, pythonic OO wrapper around Google's "google-api-python-client" library.
3-
Currently supports Python 2.7 :snake::snake::snake:
3+
Currently supports Python 3+ :snake::snake::snake:
44

55
## Installation
66
```bash
@@ -20,11 +20,11 @@ from google_objects import DriveClient
2020
gdrive = DriveClient(OAUTH2LIB_CREDS)
2121
about = gdrive.get_about()
2222

23-
print about.email
24-
print about.name
23+
print(about.email)
24+
print(about.name)
2525

2626
# prints link to profile photo
27-
print about.photo
27+
print(about.photo)
2828

2929
# ...
3030
```
@@ -39,12 +39,12 @@ files_by_type = {
3939
}
4040

4141
for file in files_by_type['folders']:
42-
print file.id
43-
print file.name
42+
print(file.id)
43+
print(file.name)
4444

4545
for file in files_by_type['spreadsheets']:
4646
# prints list of parent folder IDs
47-
print file.parents
47+
print(file.parents)
4848

4949
# ...
5050
```
@@ -59,7 +59,7 @@ new_file = file.copy('NEW_FILE_NAME', ['PARENT_FOLDER_1', 'PARENT_FOLDER_2'])
5959
permission = new_file.add_permission('myfriend@hotmail.com')
6060

6161
# print newly created permission information
62-
print permission.role, permission.type, permission.email
62+
print(permission.role, permission.type, permission.email)
6363
```
6464

6565
### Google Slides v1
@@ -74,19 +74,19 @@ presentation = gslides.get_presentation('PRESENTATION_ID')
7474

7575
# print slides attributes
7676
for slide in presentation:
77-
print slide.id
77+
print(slide.id)
7878

7979
for element in slide: # equivalent to 'for element in presentation.elements()'
80-
print element.type
81-
# Shape, Table, etc
80+
print(element.type)
81+
# Shape, Table, etc
8282
```
8383

8484
- [x] Check text in shape:
8585

8686
```python
8787
shape = presentation.get_element_by_id('SHAPE_ID')
8888
for segment in shape.text:
89-
print segment.text
89+
print(segment.text)
9090
```
9191

9292
- [x] Batch update every cell in table:
@@ -96,7 +96,7 @@ for segment in shape.text:
9696
with presentation as pres:
9797
table = pres.get_element_by_id('TABLE_ID')
9898
for cell in table:
99-
print cell.location # tuple containing cell location
99+
print(cell.location) # tuple containing cell location
100100
for segment in cell.text:
101101
# update cell
102102
segment.text = 'UPDATED_VALUE'
@@ -113,7 +113,7 @@ gsheets = SheetsClient(OAUTHLIB_CREDS)
113113
spreadsheet = gsheets.get_spreadsheet('SPREADSHEET_ID')
114114

115115
for sheet in spreadsheet:
116-
print sheet.id, sheet.title
116+
print(sheet.id, sheet.title)
117117
```
118118

119119
- [x] Get sheet by name and return its full block of values:
@@ -138,7 +138,7 @@ values = spreadsheet.get_range('SHEET_NAME!A:C')
138138
# loop through rows
139139
for i, row in enumerate(values):
140140
values[i] = [1, 2, 3]
141-
print row
141+
print(row)
142142
values.update()
143143

144144
# you can also use the slice syntax for updating..

google_objects/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ def build(self, service, version, **kwargs):
5454
return discovery.build(service, version, http=http, **kwargs)
5555

5656
@classmethod
57-
def from_service_account(cls, creds_path, scope=SCOPES, user=None):
57+
def from_service_account(cls, creds_path=None, user=None, scope=SCOPES):
5858
from oauth2client.service_account import ServiceAccountCredentials as S
5959

60+
if not creds_path:
61+
creds_path = os.getenv('GOOGLE_SERVICE_ACCOUNT_CREDENTIALS')
62+
63+
if not user:
64+
user = os.getenv('GOOGLE_DELEGATED_USER')
65+
6066
creds_path = os.path.expanduser(creds_path)
6167
creds = S.from_json_keyfile_name(creds_path, _gen_scopes(scope))
6268

google_objects/drive/core.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ def create_permission(self, file_id, permission, message=None, notification=True
144144
return Permission(**data)
145145

146146

147-
# objects
148-
149147
class About(GoogleObject):
150148

151149
"""Docstring for User Resource, this is READ ONLY"""

google_objects/sheets/core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def __init__(self, credentials=None, api_key=None, callback=None):
7575
super(self.__class__, self).__init__(credentials, api_key)
7676
self._resource = self.build('sheets', 'v4')
7777

78+
@classmethod
79+
def from_service_account(cls, **kwargs):
80+
kwargs['scope'] = ['spreadsheets']
81+
return super().from_service_account(**kwargs)
82+
7883
def get_spreadsheet(self, id):
7984
"""Returns a Spreadsheet Object
8085

google_objects/utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66
"""
77

88
import re
9-
from functools import wraps
10-
11-
12-
def handle_key_error(func):
13-
@wraps(func)
14-
def func_wrapper(self):
15-
try:
16-
return func(self)
17-
except KeyError:
18-
return
19-
return func_wrapper
209

2110

2211
def set_private_attrs(instance, dt):
@@ -92,11 +81,3 @@ def keys_to_camel(dt):
9281
dt[new_key] = dt.pop(key)
9382

9483
return dt
95-
96-
if __name__ == '__main__':
97-
dt = {
98-
'pageElements': 'test',
99-
'otherPresentation': 3
100-
}
101-
102-
print(keys_to_snake(dt))

0 commit comments

Comments
 (0)