blob: 0dd363d9d0ca6fbba5c76a9e490444fab3cc5148 [file] [log] [blame]
gstepanov5efd15a2015-01-29 17:26:24 +02001import gspread
2import argparse
3import json
4
gstepanov31bb8e42015-01-30 18:00:42 +02005from config import DEFAULT_FILE_PATH, PASSWORD, EMAIL, \
6 WORK_SHEET, DOCUMENT_ID, ROW_COUNT
7from gspread.exceptions import WorksheetNotFound
gstepanov5efd15a2015-01-29 17:26:24 +02008
9
10def load_data(file_name):
11 with open(file_name) as f:
12 data = f.read()
13 return json.loads(data)
14
15
gstepanov31bb8e42015-01-30 18:00:42 +020016#getting worksheet from sheet or create it with specified column names.
17def get_work_sheet(sheet, name, column_names):
18 try:
19 work_sheet = sheet.worksheet(name)
20 except WorksheetNotFound:
21 work_sheet = sheet.add_worksheet(title=name, rows=ROW_COUNT,
gstepanov71764e92015-02-02 16:33:11 +020022 cols=max(40, len(column_names)))
gstepanov31bb8e42015-01-30 18:00:42 +020023
24 for i in range(1, len(column_names) + 1):
25 work_sheet.update_cell(1, i, column_names[i - 1])
26
27 return work_sheet
28
29
30def get_row_number(work_sheet):
31 num = 2
32
33 while num < work_sheet.row_count and work_sheet.cell(num, 1).value != "":
34 num += 1
35
36 if num == work_sheet.row_count:
37 work_sheet.append_row(["" for x in range(work_sheet.col_count)])
38
39 return num
40
41
42def append_row(work_sheet, row):
43 row_number = get_row_number(work_sheet)
44
45 i = 1
46 for k in row.keys():
47 work_sheet.update_cell(row_number, i, row[k])
48 i += 1
49
50
gstepanov71764e92015-02-02 16:33:11 +020051def make_report(email, password, data):
52 gc = gspread.login(email, password)
gstepanov5efd15a2015-01-29 17:26:24 +020053 sh = gc.open_by_key(DOCUMENT_ID)
gstepanov5efd15a2015-01-29 17:26:24 +020054
gstepanov31bb8e42015-01-30 18:00:42 +020055 work_sheet = get_work_sheet(sh, WORK_SHEET, data.keys())
56 append_row(work_sheet, data)
gstepanov5efd15a2015-01-29 17:26:24 +020057
58
gstepanov71764e92015-02-02 16:33:11 +020059def main(file_name, email, password):
60 data = load_data(file_name)
61 make_report(email, password, data)
62
63
gstepanov5efd15a2015-01-29 17:26:24 +020064if __name__ == '__main__':
65 parser = argparse.ArgumentParser()
gstepanov31bb8e42015-01-30 18:00:42 +020066 parser.add_argument('-n', '--name', help='data file path',
67 default=DEFAULT_FILE_PATH)
gstepanov71764e92015-02-02 16:33:11 +020068 parser.add_argument('-e', '--email', help='user email',
69 default="aaa@gmail.com")
70 parser.add_argument('-p', '--password', help='user password',
71 default="1234")
72 parser.add_argument('-m', '--mode', help='mode type local or global',
73 default=DEFAULT_FILE_PATH)
gstepanov5efd15a2015-01-29 17:26:24 +020074 results = parser.parse_args()
gstepanov71764e92015-02-02 16:33:11 +020075 print results
76 # main(file_name, email, password)
77