blob: e7bc946cb33fbd85f23f1fd516e7c89b7b97a13e [file] [log] [blame]
koder aka kdanilov56ee3ed2015-02-02 19:00:31 +02001import sys
gstepanov5efd15a2015-01-29 17:26:24 +02002import json
koder aka kdanilov56ee3ed2015-02-02 19:00:31 +02003import argparse
4
5
6import gspread
7
gstepanov5efd15a2015-01-29 17:26:24 +02008
gstepanove0f26ab2015-02-02 18:09:37 +02009from config import DEFAULT_FILE_PATH, \
koder aka kdanilov56ee3ed2015-02-02 19:00:31 +020010 WORK_SHEET, DOCUMENT_ID, OUTPUT_FILE
11from storage_api import DiskStorage, GoogleDocsStorage, \
12 get_work_sheet, append_row
gstepanov5efd15a2015-01-29 17:26:24 +020013
14
15def load_data(file_name):
16 with open(file_name) as f:
17 data = f.read()
18 return json.loads(data)
19
20
koder aka kdanilov56ee3ed2015-02-02 19:00:31 +020021# getting worksheet from sheet or create it with specified column names.
22
gstepanov31bb8e42015-01-30 18:00:42 +020023
gstepanov71764e92015-02-02 16:33:11 +020024def make_report(email, password, data):
25 gc = gspread.login(email, password)
gstepanov5efd15a2015-01-29 17:26:24 +020026 sh = gc.open_by_key(DOCUMENT_ID)
gstepanov5efd15a2015-01-29 17:26:24 +020027
gstepanov31bb8e42015-01-30 18:00:42 +020028 work_sheet = get_work_sheet(sh, WORK_SHEET, data.keys())
29 append_row(work_sheet, data)
gstepanov5efd15a2015-01-29 17:26:24 +020030
31
koder aka kdanilov56ee3ed2015-02-02 19:00:31 +020032def 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
45def process_results(file_name, email, password, mode):
gstepanov71764e92015-02-02 16:33:11 +020046 data = load_data(file_name)
gstepanove0f26ab2015-02-02 18:09:37 +020047
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)
gstepanov71764e92015-02-02 16:33:11 +020054
55
koder aka kdanilov56ee3ed2015-02-02 19:00:31 +020056def main(argv):
57 opts = parse_args(argv)
gstepanov71764e92015-02-02 16:33:11 +020058
koder aka kdanilov56ee3ed2015-02-02 19:00:31 +020059 process_results(opts.name,
60 opts.email,
61 opts.password,
62 opts.mode)
63 return 0
64
65
66if __name__ == '__main__':
67 exit(main(sys.argv[1:]))