1 | """A sample CLI with API interaction.""" |
||
2 | |||
3 | import logging |
||
4 | |||
5 | from getpass import getpass |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
6 | import argparse, sys, datetime |
||
0 ignored issues
–
show
|
|||
7 | from jsonjuggler import * |
||
0 ignored issues
–
show
|
|||
8 | import juggler |
||
9 | |||
10 | import dateutil.parser |
||
0 ignored issues
–
show
|
|||
11 | |||
12 | from airtable import Airtable |
||
0 ignored issues
–
show
|
|||
13 | |||
14 | DEFAULT_LOGLEVEL = 'warning' |
||
15 | # DEFAULT_OUTPUT = 'export.tjp' |
||
16 | log = logging.getLogger(__name__) |
||
17 | |||
18 | def main(): |
||
19 | logging.basicConfig(level=logging.WARN) |
||
20 | |||
21 | ARGPARSER = argparse.ArgumentParser() |
||
22 | ARGPARSER.add_argument('-l', '--loglevel', dest='loglevel', default=DEFAULT_LOGLEVEL, |
||
23 | action='store', required=False, choices=["debug", "info", "warn", "error"], |
||
0 ignored issues
–
show
|
|||
24 | help='Level for logging (strings from logging python package: "warn", "info", "debug")') |
||
0 ignored issues
–
show
|
|||
25 | ARGPARSER.add_argument('-a', '--api', dest='api', default=None, |
||
26 | action='store', required=True, choices=['airtable'], |
||
0 ignored issues
–
show
|
|||
27 | help='Execute specified API: only "airtable" is currently supported') |
||
0 ignored issues
–
show
|
|||
28 | ARGPARSER.add_argument('-k', '--api-key', dest='apikey', default="", |
||
29 | action='store', required=True, |
||
0 ignored issues
–
show
|
|||
30 | help='Specify API key where appropriate (e.g. -k keyAnIuYcufa3dD)') |
||
0 ignored issues
–
show
|
|||
31 | ARGPARSER.add_argument('-b', '--base', dest='base', default="", |
||
32 | action='store', required=True, |
||
0 ignored issues
–
show
|
|||
33 | help='Specify Base ID where appropriate (e.g. -b appA8ZuLosBV4GDSd)') |
||
0 ignored issues
–
show
|
|||
34 | ARGPARSER.add_argument('-t', '--table', dest='table', default="", |
||
35 | action='store', required=True, |
||
0 ignored issues
–
show
|
|||
36 | help='Specify Table ID where appropriate (e.g. -t Tasks)') |
||
0 ignored issues
–
show
|
|||
37 | ARGPARSER.add_argument('-v', '--view', dest='view', default="", |
||
38 | action='store', required=True, |
||
0 ignored issues
–
show
|
|||
39 | help='Specify Table View where appropriate (e.g. -v Work)') |
||
0 ignored issues
–
show
|
|||
40 | ARGPARSER.add_argument('--dry-run', dest='dryrun', default=False, |
||
41 | action='store_true', required=False, |
||
0 ignored issues
–
show
|
|||
42 | help='Do not commit calculation results') |
||
0 ignored issues
–
show
|
|||
43 | # ARGPARSER.add_argument('-o', '--output', dest='output', default=DEFAULT_OUTPUT, |
||
44 | # action='store', required=False, |
||
45 | # help='Output .tjp file for task-juggler') |
||
46 | ARGS = ARGPARSER.parse_args() |
||
47 | |||
48 | set_logging_level(ARGS.loglevel) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
49 | |||
50 | # PASSWORD = getpass('Enter generic password for {user}: '.format(user=ARGS.username)) |
||
51 | |||
0 ignored issues
–
show
|
|||
52 | airtable = Airtable(ARGS.base, ARGS.table, api_key=ARGS.apikey) |
||
53 | |||
0 ignored issues
–
show
|
|||
54 | data = [x["fields"] for x in airtable.get_all(view=ARGS.view)] |
||
0 ignored issues
–
show
|
|||
55 | for rec in data: |
||
56 | preference = 0 |
||
57 | if "preference" in rec: |
||
58 | preference = int(rec['preference']) |
||
59 | if "priority" in rec: |
||
60 | if rec["priority"].lower() == "low": |
||
61 | pri = preference + 100 |
||
62 | elif rec["priority"].lower() == "high": |
||
63 | pri = preference + 200 |
||
64 | elif rec["priority"].lower() == "critical": |
||
65 | pri = preference + 300 |
||
66 | else: |
||
67 | pri = 1 |
||
68 | else: |
||
69 | pri = preference + 100 # low |
||
70 | rec["priority"] = pri |
||
71 | if 'appointment' in rec: |
||
72 | rec['start'] = rec['appointment'] |
||
73 | del rec["priority"] # tasks scheduling is not guaranteed if priority is set |
||
74 | if 'depends' in rec: |
||
75 | rec['depends'] = [int(x) for x in re.findall(r"[\w']+", rec["depends"])] |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
76 | if "priority" in rec and "deadline" in rec and not rec["priority"] >= 300: |
||
77 | diff_days = (datetime.datetime.now() - dateutil.parser.parse(rec["deadline"])).days |
||
78 | if diff_days < 0: diff_days = 0 |
||
0 ignored issues
–
show
|
|||
79 | rec["priority"] = rec["priority"] + diff_days * 3 |
||
80 | if rec["priority"] >= 250: rec["priority"] = 250 |
||
0 ignored issues
–
show
|
|||
81 | |||
0 ignored issues
–
show
|
|||
82 | JUGGLER = DictJuggler(data) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
83 | JUGGLER.run() |
||
84 | |||
0 ignored issues
–
show
|
|||
85 | if ARGS.dryrun: return |
||
0 ignored issues
–
show
|
|||
86 | |||
0 ignored issues
–
show
|
|||
87 | for t in JUGGLER.walk(juggler.JugglerTask): |
||
88 | airtable.update_by_field("id", t.get_id(), {"booking": t.walk(juggler.JugglerBooking)[0].decode()[0].isoformat()}) |
||
0 ignored issues
–
show
|
|||
89 | |||
0 ignored issues
–
show
|
|||
90 | if __name__ == '__main__': # pragma: no cover |
||
91 | main() |
||
92 |