Skip to content

Commit 2b10728

Browse files
committed
removed service credentials CLI arg
1 parent eda8de4 commit 2b10728

1 file changed

Lines changed: 10 additions & 19 deletions

File tree

google_objects/cli.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,13 @@ class SheetsCLI(object):
1818
"""Command line tool for fetching tabular data
1919
and redirecting it to STDOUT."""
2020

21-
def __init__(self, key=None, service_account=False, user=None):
22-
"""Authenticate the Google API Client and loads the Specified Spreadsheet"""
23-
24-
try:
25-
if service_account:
26-
self.client = SheetsClient.from_service_account(user=user)
27-
else:
28-
self.client = SheetsClient.from_api_key(key)
29-
30-
except ValueError as exception:
31-
sys.stderr.write(str(exception))
32-
sys.exit(1)
33-
34-
def get_spreadsheet(self, spreadsheet_id):
21+
def get_spreadsheet(self, spreadsheet_id, key=None):
3522
"""Return a Google Sheet as a list of dictionaries in the 'records' attribute
3623
of the outputted json"""
3724

3825
try:
39-
spreadsheet = self.client.get_spreadsheet(spreadsheet_id)
26+
client = SheetsClient.from_api_key(key)
27+
spreadsheet = client.get_spreadsheet(spreadsheet_id)
4028
output = {
4129
'title': spreadsheet.title,
4230
'id': spreadsheet.id,
@@ -48,18 +36,20 @@ def get_spreadsheet(self, spreadsheet_id):
4836

4937
except ValueError as exception:
5038
sys.stderr.write(str(exception))
51-
sys.exit(2)
39+
sys.exit(1)
5240

5341
# sys.stdout.write(dataframe.to_json(orient='records'))
5442
sys.stdout.write(json.dumps(output))
5543

56-
def create_spreadsheet(self, file_path=None):
57-
"""Create a new Google Spreadsheet.
44+
def create_spreadsheet(self, file_path=None, user=None):
45+
"""Create a new Google Spreadsheet.
5846
5947
:file_name: JSON File name
6048
:returns: URL of newly created Google Sheet.
6149
6250
"""
51+
client = SheetsClient.from_service_account(user=user)
52+
6353
if file_path:
6454
with open(file_path, 'r') as f:
6555
json_data = f.read()
@@ -72,10 +62,11 @@ def create_spreadsheet(self, file_path=None):
7262

7363
df = pd.DataFrame(input_data)
7464
try:
75-
spreadsheet = self.client.create_spreadsheet_from_dataframe(df)
65+
spreadsheet = client.create_spreadsheet_from_dataframe(df)
7666
sys.stdout.write(spreadsheet.url)
7767
except Exception as e:
7868
sys.stdout.write(e)
69+
sys.exit(1)
7970

8071

8172
def main():

0 commit comments

Comments
 (0)