gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 1 | import gspread |
| 2 | import argparse |
| 3 | import json |
| 4 | |
gstepanov | 31bb8e4 | 2015-01-30 18:00:42 +0200 | [diff] [blame] | 5 | from config import DEFAULT_FILE_PATH, PASSWORD, EMAIL, \ |
| 6 | WORK_SHEET, DOCUMENT_ID, ROW_COUNT |
| 7 | from gspread.exceptions import WorksheetNotFound |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 8 | |
| 9 | |
| 10 | def load_data(file_name): |
| 11 | with open(file_name) as f: |
| 12 | data = f.read() |
| 13 | return json.loads(data) |
| 14 | |
| 15 | |
gstepanov | 31bb8e4 | 2015-01-30 18:00:42 +0200 | [diff] [blame] | 16 | #getting worksheet from sheet or create it with specified column names. |
| 17 | def 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, |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame^] | 22 | cols=max(40, len(column_names))) |
gstepanov | 31bb8e4 | 2015-01-30 18:00:42 +0200 | [diff] [blame] | 23 | |
| 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 | |
| 30 | def 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 | |
| 42 | def 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 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame^] | 51 | def make_report(email, password, data): |
| 52 | gc = gspread.login(email, password) |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 53 | sh = gc.open_by_key(DOCUMENT_ID) |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 54 | |
gstepanov | 31bb8e4 | 2015-01-30 18:00:42 +0200 | [diff] [blame] | 55 | work_sheet = get_work_sheet(sh, WORK_SHEET, data.keys()) |
| 56 | append_row(work_sheet, data) |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 57 | |
| 58 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame^] | 59 | def main(file_name, email, password): |
| 60 | data = load_data(file_name) |
| 61 | make_report(email, password, data) |
| 62 | |
| 63 | |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 64 | if __name__ == '__main__': |
| 65 | parser = argparse.ArgumentParser() |
gstepanov | 31bb8e4 | 2015-01-30 18:00:42 +0200 | [diff] [blame] | 66 | parser.add_argument('-n', '--name', help='data file path', |
| 67 | default=DEFAULT_FILE_PATH) |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame^] | 68 | 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) |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 74 | results = parser.parse_args() |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame^] | 75 | print results |
| 76 | # main(file_name, email, password) |
| 77 | |