From 0312a33115736f87f3c9669a785a15089ee42b4d Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Sat, 15 Dec 2018 17:40:10 +0200 Subject: [PATCH] help to print votes and questions --- manager.py | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.py | 15 ++++++-- 2 files changed, 120 insertions(+), 4 deletions(-) diff --git a/manager.py b/manager.py index bd91eed..8ef6196 100644 --- a/manager.py +++ b/manager.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +from collections import OrderedDict from datetime import datetime from utils import * import argparse @@ -123,6 +124,18 @@ def parse_options(database, questions): help = "List existing tokens, instead of generating more" ) + ## show question set + parser_show = subparsers.add_parser('show', help = "Show a question set") + + ## list votes + parser_list = subparsers.add_parser('list', help = "List all votes") + parser_list.add_argument( + '--tsv', + action="store_true", + dest="tsv", + default = False, + help = "TSV output" + ) ## summary of vote parser_summary = subparsers.add_parser('summary', help = "Vote results") @@ -169,6 +182,61 @@ def list_question_sets(options): print(f[0:-4]) +def list_votes(options): + if not is_key(options.name, options): + raise Exception("%s does not exist, or is not a valid question set name"%( options.name, )) + db = open_db(options.db) + votes = OrderedDict() + cur = db.cursor() + cur.execute( + "SELECT question, answer, answer_type FROM answers WHERE question_set = ?", + ( + options.name, + ) + ) + for row in cur: + if row[0] not in votes.keys(): + votes[row[0]] = { + 'answers': [], + 'answer_type': row[2] + } + votes[row[0]]['answers'].append(row[1]) + + if hasattr(options, 'tsv') and options.tsv: + out = votes_tsv(votes) + else: + out = votes_list(votes) + print(out) + + +def show(options): + if not is_key(options.name, options): + raise Exception("%s does not exist, or is not a valid question set name"%( options.name, )) + form = parse_form(options.name, options.questions, True) + + form['ends'] = time_to('expires', form) + form['starts'] = time_to('opens', form) + print(""" + Voting ends at: {ends} + Voting starts at: {starts} + Style (open/closed): {vote_style} + Show results after voting: {show_results} + Title: {title} + Draft: {draft} +""".format_map(form)) + + for question in form['questions']: + print("\n# {name}".format_map(question)) + for choice in question['choices']: + print("- " + choice) + for choice in question['multichoices']: + print("+ " + choice) + if question['open_question']: + print(",----------.") + print("| |") + print("`----------'") + + def summary(options): if not is_key(options.name, options): raise Exception("%s does not exist, or is not a valid question set name"%( options.name, )) @@ -238,6 +306,43 @@ def summary_tsv(questions, answers, tokens): return s +def votes_list(votes): + """ votes as list format """ + s = "" + + for q in votes: + s += "\n# %s (%s)\n"%( q, votes[q]['answer_type']) + + for answer in votes[q]['answers']: + prefix = "" + if votes[q]['answer_type'] == "single": + prefix = "- " + if votes[q]['answer_type'] == "multiple": + prefix = "+ " + if votes[q]['answer_type'] == "open": + prefix = "----\n> " + + s += "%s%s\n"%( + prefix, + answer, + ) + return s + + +def votes_tsv(votes): + s = '"Question"\t"Question type"\t"Answer"\n' + good_characters = dict.fromkeys(range(32)) + for q in votes: + for answer in votes[q]['answers']: + s += '"%s"\t"%s"\t"%s"\n'%( + q, + votes[q]['answer_type'], + answer.translate(good_characters), + ) + return s + + + def clear_votes(options): try: summary(options) @@ -277,6 +382,10 @@ def main(database, questions): options = parse_options(database, questions) if options.subparser_name == "token": manage_tokens(options) + if options.subparser_name == "show": + show(options) + if options.subparser_name == "list": + list_votes(options) if options.subparser_name == "summary": summary(options) if options.subparser_name == "clear-votes": diff --git a/utils.py b/utils.py index fca63c3..dff6dcf 100644 --- a/utils.py +++ b/utils.py @@ -173,7 +173,7 @@ def is_voter(key, token): ) return len(cur.fetchall()) > 0 -def parse_form(key): +def parse_form(key, questions = None, debug = None): form = { 'expires': None, 'opens': None, @@ -188,7 +188,9 @@ def parse_form(key): key = secure_filename(key) try: current_question = 0 - with open(os.path.join(app.config['QUESTIONS'], key + ".txt"), "rt", encoding = "UTF-8") as fp: + if questions == None: + questions = app.config['QUESTIONS'] + with open(os.path.join(questions, key + ".txt"), "rt", encoding = "UTF-8") as fp: for row in fp: if row.strip() == "": continue @@ -197,7 +199,10 @@ def parse_form(key): continue if rowsl.startswith("title: "): form['title'] = row[6:].strip() - g.title = row[6:].strip() + try: + g.title = row[6:].strip() + except RuntimeError: + pass continue if rowsl.startswith("expires: "): form['expires'] = parse_row_date(row) @@ -242,7 +247,9 @@ def parse_form(key): }) return form except Exception as err: - if app.config['DEBUG']: + if debug == None: + debug = app.config['DEBUG'] + if debug: raise err return False