koder aka kdanilov | 56ee3ed | 2015-02-02 19:00:31 +0200 | [diff] [blame^] | 1 | import sys |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 2 | import json |
koder aka kdanilov | 56ee3ed | 2015-02-02 19:00:31 +0200 | [diff] [blame^] | 3 | import argparse |
| 4 | |
| 5 | |
| 6 | import gspread |
| 7 | |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 8 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 9 | from config import DEFAULT_FILE_PATH, \ |
koder aka kdanilov | 56ee3ed | 2015-02-02 19:00:31 +0200 | [diff] [blame^] | 10 | WORK_SHEET, DOCUMENT_ID, OUTPUT_FILE |
| 11 | from storage_api import DiskStorage, GoogleDocsStorage, \ |
| 12 | get_work_sheet, append_row |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 13 | |
| 14 | |
| 15 | def load_data(file_name): |
| 16 | with open(file_name) as f: |
| 17 | data = f.read() |
| 18 | return json.loads(data) |
| 19 | |
| 20 | |
koder aka kdanilov | 56ee3ed | 2015-02-02 19:00:31 +0200 | [diff] [blame^] | 21 | # getting worksheet from sheet or create it with specified column names. |
| 22 | |
gstepanov | 31bb8e4 | 2015-01-30 18:00:42 +0200 | [diff] [blame] | 23 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 24 | def make_report(email, password, data): |
| 25 | gc = gspread.login(email, password) |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 26 | sh = gc.open_by_key(DOCUMENT_ID) |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 27 | |
gstepanov | 31bb8e4 | 2015-01-30 18:00:42 +0200 | [diff] [blame] | 28 | work_sheet = get_work_sheet(sh, WORK_SHEET, data.keys()) |
| 29 | append_row(work_sheet, data) |
gstepanov | 5efd15a | 2015-01-29 17:26:24 +0200 | [diff] [blame] | 30 | |
| 31 | |
koder aka kdanilov | 56ee3ed | 2015-02-02 19:00:31 +0200 | [diff] [blame^] | 32 | def parse_args(argv): |
| 33 | parser = argparse.ArgumentParser() |
| 34 | parser.add_argument('-n', '--name', help='data file path', |
| 35 | default=DEFAULT_FILE_PATH) |
| 36 | parser.add_argument('-e', '--email', help='user email', |
| 37 | default="aaa@gmail.com") |
| 38 | parser.add_argument('-p', '--password', help='user password', |
| 39 | default="1234") |
| 40 | parser.add_argument('-m', '--mode', help='mode type local or global', |
| 41 | default='local') |
| 42 | return parser.parse_args(argv) |
| 43 | |
| 44 | |
| 45 | def process_results(file_name, email, password, mode): |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 46 | data = load_data(file_name) |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 47 | |
| 48 | if mode == 'local': |
| 49 | storage = DiskStorage(OUTPUT_FILE) |
| 50 | else: |
| 51 | storage = GoogleDocsStorage(DOCUMENT_ID, WORK_SHEET, email, password) |
| 52 | |
| 53 | storage.store(data) |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 54 | |
| 55 | |
koder aka kdanilov | 56ee3ed | 2015-02-02 19:00:31 +0200 | [diff] [blame^] | 56 | def main(argv): |
| 57 | opts = parse_args(argv) |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 58 | |
koder aka kdanilov | 56ee3ed | 2015-02-02 19:00:31 +0200 | [diff] [blame^] | 59 | process_results(opts.name, |
| 60 | opts.email, |
| 61 | opts.password, |
| 62 | opts.mode) |
| 63 | return 0 |
| 64 | |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | exit(main(sys.argv[1:])) |