import typer from arrow import get as arrow_get from voting import Quorum, QuorumKind, Result, Voting, Vote 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)) @app.command() def vote(title: str, vote: str = None, voter: str = typer.Option("", "--voter", "-V", help="Name of the voter"), ): """Place a vote.""" with Storage() as store: voting = store.pop(title) if not Voting: return voting.vote(voter, Vote[vote]) store.push(voting)