-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
183 lines (137 loc) · 5.97 KB
/
cli.py
File metadata and controls
183 lines (137 loc) · 5.97 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from waitress_server import run, application
from _thread import start_new_thread
from datetime import datetime
from copy import deepcopy
import os
#commands
commands = {} #for help command functioning
def help_command(*args): #print help for all commands or a single command
"""View help info - syntax: 'help (command)'"""
if len(args[0])>0:
if args[0][0] in commands:
if args[0][0] == "exit":
print("exit - Exit the program")
else:
print(args[0][0],"-",commands[args[0][0]].__doc__)
else:
print("That command doesn't exist, to get a list of all the commands, type 'help'")
else:
for i in commands:
if i == "exit":
print("exit - Exit the program")
else:
print(i,"-",commands[i].__doc__)
def start_server(*args): #start the webserver (the webserver will close if the program closes)
"""Start the webserver"""
start_new_thread(run,(False,))
def get_start(*args):
"""Get starting time of the election"""
stamp = application.open_time
print("Timestamp:",stamp,"Converted value:",datetime.fromtimestamp(stamp).strftime("%d-%m-%Y %H:%M:%S "))
def get_end(*args):
"""Get ending time of the election"""
stamp = application.close_time
print("Timestamp:",stamp,"Converted value:",datetime.fromtimestamp(stamp).strftime("%d-%m-%Y %H:%M:%S "))
def get_finns(*args):
"""Get currently stored finns on the running server, won't work if server isn't running"""
print("Listed members of Finland:",application.finns)
def get_candidates(*args):
"""Get election candidates"""
print("Candidates:",application.candidates)
def get_names(*args):
"""Get names of people who've already voted"""
print("Players who have currently voted:",application.voted_names)
def get_ips(*args):
"""Get ip hashes of people who've already voted"""
print("Ip hashses which have been used to vote:",application.voted_ips)
def get_ballots(*args):
"""Get currently submitted ballots"""
print("Current ballots:\n","\n".join([i+": "+str(application.ballots[i]) for i in application.ballots]))
def get_results(*args):
"""Get currently calculated results"""
print("Current results:",application.voting_results)
def get_past_results(*args):
"""Get past results"""
print(application.past_results)
def archive_election(*args): #long archival process thingy
"""Clear IP addresses from the table and move it to the archive"""
print("Beginning archival process...")
if not os.path.exists("results.csv"):
print("Results file doesn't exist, can't continue archival process...")
else:
print("Results file found, removing IP hashes...")
final_lines = []
with open("results.csv","r") as file:
lines = file.readlines()
for i in range(2,len(lines)):
line = lines[i].split(",")
line[1] = "-"
lines[i] = ",".join(line)
final_lines = deepcopy(lines)
if not os.path.exists("results"):
print("Results directory not found, creating...")
os.makedirs("results")
folder_name = ""
folder_name += datetime.fromtimestamp(application.open_time).strftime("%d-%m-%Y")
folder_name += datetime.fromtimestamp(application.close_time).strftime("_%d-%m-%Y")
if not os.path.exists("results/"+folder_name): #in case something goes wrong it can write to the same election's folder
print("Creating results entry directory...")
os.makedirs("results/"+folder_name)
print("Writing file...")
with open("results/"+folder_name+"/results.csv","w") as file:
for i in final_lines:
file.write(i)
print("Copying settings.json...")
lines = []
with open("settings.json","r") as file:
lines = deepcopy(file.readlines())
with open("results/"+folder_name+"/settings.json","w") as file:
for i in lines:
file.write(i)
print("Calculating results...")
application.verbose = False
application.get_settings()
application.get_ballots()
application.calculate_results()
results = application.voting_results
print(results)
print("Writing results to file...")
with open("results/"+folder_name+"/results.txt","w") as file:
for i in results:
file.write(i+" - "+str(results[i])+"\n")
if os.path.exists("results.csv"):
print("Removing results.csv...")
os.remove("results.csv")
print("Success!")
#init and mainloop logic
commands = {"exit":exit,
"help":help_command,
"start":start_server,
"getstart":get_start,
"getend":get_end,
"getfinns":get_finns,
"getcandidates":get_candidates,
"getnames":get_names,
"getips":get_ips,
"getballots":get_ballots,
"getresults":get_results,
"pastresults":get_past_results,
"archive":archive_election,} #keyword:function
def main_loop():
while True:
a = input("> ")
cmd = a.split()[0].lower() #idc about case sensitivity
args = [i.lower() for i in a.split()[1:]] #still dno't care about case sensitivity
if cmd not in commands:
print("Invalid command, to get a list of all the commands, type 'help'")
else:
commands[cmd](args)
def init_console():
print("===========================================================================")
print("Welcome to the RCV-CLI!")
print(len(commands),"commands available")
print("To get a list of all the commands, type 'help'")
print("Note: Some commands will not work when the webserver is ran outside the CLI")
main_loop()
if __name__ == "__main__":
init_console()