From b46ff8e9a46dc78dcfe30d7c9bba248e309627b1 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Sun, 5 Jun 2022 16:43:00 +0200 Subject: [PATCH] [mypy] #163: Add type annotation on wot --- silkaj/wot.py | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/silkaj/wot.py b/silkaj/wot.py index 49d8fcc1..ba138f4e 100644 --- a/silkaj/wot.py +++ b/silkaj/wot.py @@ -15,6 +15,7 @@ import urllib from collections import OrderedDict +from typing import Dict, List, Tuple import click from duniterpy.api.bma import blockchain, wot @@ -29,7 +30,9 @@ from silkaj.network_tools import client_instance, exit_on_http_error from silkaj.tui import gen_pubkey_checksum -def get_sent_certifications(signed, time_first_block, params): +def get_sent_certifications( + signed: List, time_first_block: int, params: Dict +) -> Tuple[List[str], List[str]]: sent = list() expire = list() if signed: @@ -49,7 +52,7 @@ def get_sent_certifications(signed, time_first_block, params): consult the membership status of any given identity", ) @click.argument("uid_pubkey") -def received_sent_certifications(uid_pubkey): +def received_sent_certifications(uid_pubkey: str) -> None: """ get searched id get id of received and sent certifications @@ -61,10 +64,10 @@ def received_sent_certifications(uid_pubkey): checked_pubkey = is_pubkey_and_check(uid_pubkey) if checked_pubkey: - uid_pubkey = checked_pubkey + uid_pubkey = str(checked_pubkey) identity, pubkey, signed = choose_identity(uid_pubkey) - certifications = OrderedDict() + certifications = OrderedDict() # type: OrderedDict params = get_blockchain_parameters() requirements = client(wot.requirements, pubkey) for req in requirements["identities"]: @@ -99,37 +102,38 @@ sent {nbr_sent_certs}/{params["sigStock"]} certifications:\n\ membership_status(certifications, pubkey, req) -def cert_written_in_the_blockchain(written_certs, certifieur): +def cert_written_in_the_blockchain(written_certs: Dict, certifieur: Dict): for cert in written_certs: if cert["from"] == certifieur["pubkey"]: return certifieur["uids"][0] + " ✔" return certifieur["uids"][0] -def membership_status(certifications, pubkey, req): +def membership_status(certifications: OrderedDict, pubkey: str, req: Dict) -> None: params = get_blockchain_parameters() if len(certifications["received"]) >= params["sigQty"]: date = certifications["received_expire"][ len(certifications["received"]) - params["sigQty"] ] print(f"Membership expiration due to certification expirations: {date}") - member = wt.is_member(pubkey) - if member: - member = True - print("member:", member) + member_lookup = wt.is_member(pubkey) + is_member = bool(member_lookup) + print("member:", is_member) if req["revoked"]: revoke_date = from_timestamp(req["revoked_on"], tz="local").format(DATE) print(f"revoked: {req['revoked']}\nrevoked on: {revoke_date}") - if not member and req["wasMember"]: + if not is_member and req["wasMember"]: print("expired:", req["expired"], "\nwasMember:", req["wasMember"]) - elif member: + elif is_member: expiration_date = now().add(seconds=req["membershipExpiresIn"]).format(DATE) print(f"Membership document expiration: {expiration_date}") print("Sentry:", req["isSentry"]) print("outdistanced:", req["outdistanced"]) -def expiration_date_from_block_id(block_id, time_first_block, params): +def expiration_date_from_block_id( + block_id: str, time_first_block: int, params: Dict +) -> str: expir_timestamp = ( date_approximation(block_id, time_first_block, params["avgGenTime"]) + params["sigValidity"] @@ -143,10 +147,10 @@ def date_approximation(block_id, time_first_block, avgentime): @click.command("lookup", help="User identifier and public key lookup") @click.argument("uid_pubkey") -def id_pubkey_correspondence(uid_pubkey): +def id_pubkey_correspondence(uid_pubkey: str) -> None: checked_pubkey = is_pubkey_and_check(uid_pubkey) if checked_pubkey: - uid_pubkey = checked_pubkey + uid_pubkey = str(checked_pubkey) try: lookups = wt.wot_lookup(uid_pubkey) @@ -161,7 +165,7 @@ def id_pubkey_correspondence(uid_pubkey): click.echo(content) -def choose_identity(pubkey_uid): +def choose_identity(pubkey_uid: str) -> Tuple[Dict, str, List]: """ Get lookup from a pubkey or an uid Loop over the double lists: pubkeys, then uids @@ -175,7 +179,12 @@ def choose_identity(pubkey_uid): exit_on_http_error(e, 404, f"No identity found for {pubkey_uid}") # Generate table containing the choices - identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []} + identities_choices = { + "id": [], + "uid": [], + "pubkey": [], + "timestamp": [], + } # type: Dict for pubkey_index, lookup in enumerate(lookups): for uid_index, identity in enumerate(lookup["uids"]): identities_choices["id"].append(str(pubkey_index) + str(uid_index)) @@ -199,8 +208,8 @@ def choose_identity(pubkey_uid): while selected_id not in identities_choices["id"]: selected_id = click.prompt(message) - pubkey_index = int(selected_id[:-1]) - uid_index = int(selected_id[-1:]) + pubkey_index = int(str(selected_id)[:-1]) + uid_index = int(str(selected_id)[-1:]) return ( lookups[pubkey_index]["uids"][uid_index], -- GitLab