-
Notifications
You must be signed in to change notification settings - Fork 2
/
pairwise-add-member.py
executable file
·70 lines (58 loc) · 2.14 KB
/
pairwise-add-member.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# from __future__ import print_function, division, absolute_import, unicode_literals
from __future__ import print_function, division, absolute_import
import argparse
import json
import os.path
import sys
HISTORY = "./pairwise_history.json"
# pairwise_history.json is a dictionary with keys of ISO8601 timestamps
# and value of a list of pair_lists
COWORKERS = "./pairwise_coworkers.json"
# list of lists of mutual coworkers. If more than 2 people work together,
# put them in the same list. All pairwise combinations will be created.
NAMES = "./pairwise_names.json"
# pairwise_names.json is a list of all the names in the rota
RELEVANT_HISTORY = 8
# RELEVANT_HISTORY controls how much recent history is used for ensuring
# matches are not repeated. Set to zero (0) to ignore
def parse_cli(test_args=None):
parser = argparse.ArgumentParser()
parser.add_argument("--names", default=NAMES,
help="File for the list participant names")
parser.add_argument("member", help="Member to add to names list")
if test_args is None:
args = parser.parse_args()
else:
args = parser.parse_args(test_args)
return args
def load_names(args):
"""Loads the names from JSON"""
# NAMES is a json document which is just a list of names
if os.path.isfile(args.names):
with open(args.names, 'r') as n:
try:
names = json.load(n)
except:
sys.exit("ERROR: {0} is invalid JSON".format(args.names))
else:
names = []
return names
def save_names(args, names):
"""Saves the names to JSON"""
# NAMES is a json document which is just a list of names
with open(args.names, 'w') as n:
try:
json.dump(names, n, sort_keys=True, indent=4)
except:
sys.exit("ERROR! Failed to overwrite args.names")
def main():
args = parse_cli()
names = load_names(args)
if args.member in names:
sys.exit("ERROR: {0} already exists in list of names.".format(args.member))
names.append(args.member)
names.sort()
save_names(args, names)
if __name__ == "__main__":
main()