68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
import json
|
|
import sys
|
|
import csv
|
|
|
|
# using non modified nextcloud standard text poll
|
|
nom_mentions=['yes','maybe','','no']
|
|
|
|
if len(sys.argv) > 1:
|
|
csv_file=sys.argv[1]
|
|
if len(sys.argv) > 2:
|
|
nom_mentions=[]
|
|
for i in range(2,len(sys.argv)):
|
|
nom_mentions.append(sys.argv[i])
|
|
else:
|
|
print('missing file argument')
|
|
exit(1)
|
|
|
|
def mention_index(mention):
|
|
return nom_mentions.index(mention)
|
|
|
|
verbose=False
|
|
|
|
mentions=len(nom_mentions)
|
|
default_mention=mentions-1
|
|
|
|
warnings=[]
|
|
candidats={}
|
|
candidats_alias=[]
|
|
a_vote={}
|
|
votes=[]
|
|
with open(csv_file,'r') as csv_fd:
|
|
vote_reader = csv.reader(csv_fd, delimiter=',')
|
|
for row in vote_reader:
|
|
if len(candidats_alias) == 0:
|
|
for i in range(2,len(row)):
|
|
candidat_alias=row[i]
|
|
candidats_alias.append(candidat_alias)
|
|
# no other name given
|
|
if not candidat_alias in candidats:
|
|
candidats[candidat_alias]=candidat_alias
|
|
else:
|
|
# check if not duplicated
|
|
participant=row[0]
|
|
if participant in a_vote:
|
|
warnings.append("Mutliple vote " + participant + " no record")
|
|
continue
|
|
a_vote[participant]=row[1]
|
|
vote={}
|
|
for i in range(2,len(row)):
|
|
candidat_index=i-2
|
|
candidat_alias=candidats_alias[candidat_index]
|
|
mention=row[i]
|
|
vote[candidat_alias]=mention_index(mention)
|
|
votes.append(vote)
|
|
|
|
|
|
default_vote={}
|
|
|
|
if verbose:
|
|
print(','.join(candidats))
|
|
print(','.join(nom_mentions))
|
|
|
|
result = {'candidats':candidats,'votants':{'decompte':len(a_vote)},'mentions':nom_mentions,'votation':{'votes':votes}}
|
|
print(json.dumps(result))
|
|
|
|
sys.exit(0)
|
|
|