From 9b745cabc7243cc6b4ce929f26386617e1b8a57c Mon Sep 17 00:00:00 2001 From: Brian Wiborg Date: Mon, 3 Apr 2023 13:39:51 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Add=20CLI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voting/cli/voting.py | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 voting/cli/voting.py diff --git a/voting/cli/voting.py b/voting/cli/voting.py new file mode 100644 index 0000000..95c2d51 --- /dev/null +++ b/voting/cli/voting.py @@ -0,0 +1,84 @@ +import typer + +from arrow import get as arrow_get +from voting import Quorum, QuorumKind, Result, Voting +from voting.storage import Storage + +app = typer.Typer() + + +@app.command() +def new(title: str, + quorum: str = typer.Option( + "N", + "--quorum", + "-Q", + help="Quorum string in the format `Q[:V]`, for exampel `N`, `A:42` or P:42", + ), + days: int = typer.Option( + 7, + "--days", + "-D", + help="Number of days the voting will be open." + ), + voters: str = typer.Option(..., "--voters", "-V"), + ): + """Create a new voting.""" + + if ':' not in quorum: + quorum = f"{quorum}:" + _q, _v = quorum.split(':') + + q = Quorum() + if _q.upper() == 'A': + q = Quorum(QuorumKind.ABSOLUTE, _v) + elif _q.upper() == 'P': + q = Quorum(QuorumKind.PERCENT, _v) + + voting = Voting( + title=title, + quorum=q, + voters=voters.split(','), + days=days, + ) + + with Storage() as s: + s.push(voting) + + +@app.command() +def rm(title: str): + """Delete a voting.""" + with Storage() as store: + store.pop(title) + + +@app.command() +def ls(verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose output"), + only_active: bool = typer.Option(False, "--active", '-A', help="Only list active votings"), +): + """List all votings.""" + with Storage() as store: + for v in store.votings: + if only_active: + if Result(v).outcome != 'LAUFEND': + continue + if verbose: + res = Result(v).result() + print(f"{v.title} ({arrow_get(v.start).format('YYYY-MM-DD HH:mm:ss')}) {res['state']} {res['votes']}") + else: + print(v) + + +@app.command() +def show(title: str, + voters: bool = typer.Option(False, "--voters", "-v", help="Display the votes.") +): + """Display the results of a voting.""" + store = Storage() + voting = store.pop(title) + if voters: + for vote in voting.votes: + print(f"{vote} => {voting.votes[vote].name}") + else: + print(Result(voting))