-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecestat.py
executable file
·65 lines (58 loc) · 2.23 KB
/
ecestat.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
#!/usr/bin/env python
import sys
import os.path
import json
import argparse
if(sys.version_info < (2,7)):
from ordereddict import OrderedDict
else:
from collections import OrderedDict
statusfile = os.path.join(os.path.dirname(os.path.realpath(__file__)),".ecestat.json")
def load_stats():
if(not os.path.exists(statusfile)):
return {"status" : [],"legs" : OrderedDict}
statustext = open(statusfile).read()
return json.loads(statustext,object_pairs_hook = OrderedDict)
def write(data):
with open(statusfile,'w') as ofile:
json.dump(data,ofile,indent = 4,separators = (',',':'))
def main(args):
parser = argparse.ArgumentParser(description = "status retriever/updater")
parser.add_argument("--upgrade", metavar = "leg", dest = "row", type = str, default = None, help = "Upgrade the status of a leg")
parser.add_argument("--addstat", metavar = "stat", dest = "col", type = str, default = None, help = "Add a new status to the stat file")
args = parser.parse_args()
newstat,leg = args.col,args.row
data = load_stats()
if(newstat):
stattuple = newstat.split(':')
stat = stattuple[0]
if(len(stattuple) > 1):
index = int(stattuple[1])
data["status"].insert(index,stat)
if(len(stattuple) > 2):
print "Ignoring argument substrings",stattuple[2:]
else:
data["status"].append(stat)
if(leg): print "Ignoring extra arguments --update",leg
write(data)
elif(leg):
if(leg in data["legs"]):
curstat = data["legs"][leg]
index = data["status"].index(curstat)
if(index < len(data["status"]) - 1):
data["legs"][leg] = data["status"][index + 1]
write(data)
else:
print "Ignored final status update"
else:
if(len(data["status"]) > 0):
data["legs"][leg] = data["status"][0]
write(data)
else:
print "No statuses were defined in the stat file"
else:
print "defined statuses:",' '.join(data["status"])
for leg in data["legs"]:
print leg,":",data["legs"][leg]
if __name__ == "__main__":
main(sys.argv[1:])