From 056bd85e9fb5e24415bf045aa3243bbce26f4c21 Mon Sep 17 00:00:00 2001 From: matograine <tom.ngr@zaclys.net> Date: Mon, 26 Apr 2021 17:45:21 +0200 Subject: [PATCH] [mod] #344 remove loop dependency * create silkaj.wot_tools module * modify related test --- silkaj/tui.py | 4 +-- silkaj/wot.py | 65 ++--------------------------------- silkaj/wot_tools.py | 83 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_tui.py | 2 +- 4 files changed, 89 insertions(+), 65 deletions(-) create mode 100644 silkaj/wot_tools.py diff --git a/silkaj/tui.py b/silkaj/tui.py index 8d99094a..53abbc5b 100644 --- a/silkaj/tui.py +++ b/silkaj/tui.py @@ -19,7 +19,7 @@ import sys import click from datetime import datetime -from silkaj import wot, network_tools, constants +from silkaj import network_tools, constants, wot_tools from silkaj import crypto_tools as ct @@ -45,7 +45,7 @@ async def display_pubkey(tx, message, pubkey): Displays a pubkey and the eventually associated id. """ tx.append([message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]) - id = await wot.is_member(pubkey) + id = await wot_tools.is_member(pubkey) if id: tx.append([message + " (id)", id["uid"]]) diff --git a/silkaj/wot.py b/silkaj/wot.py index b26d1fbe..5b837a1f 100644 --- a/silkaj/wot.py +++ b/silkaj/wot.py @@ -19,16 +19,16 @@ import click, pendulum from time import time from tabulate import tabulate from collections import OrderedDict -from asyncio import sleep from duniterpy.api.bma import wot, blockchain from duniterpy.api.errors import DuniterError from silkaj.network_tools import ClientInstance from silkaj.crypto_tools import is_pubkey_and_check -from silkaj.tools import message_exit, coroutine +from silkaj.wot_tools import is_member, identity_of, wot_lookup, identities_from_pubkeys +from silkaj.tools import coroutine from silkaj.tui import display_pubkey_and_checksum from silkaj.blockchain_tools import BlockchainParams -from silkaj.constants import ASYNC_SLEEP, DATE +from silkaj.constants import DATE def get_sent_certifications(signed, time_first_block, params): @@ -220,62 +220,3 @@ async def choose_identity(pubkey_uid): lookups[pubkey_index]["pubkey"], lookups[pubkey_index]["signed"], ) - - -async def identity_of(pubkey_uid): - """ - Only works for members - Not able to get corresponding uid from a non-member identity - Able to know if an identity is member or not - """ - client = ClientInstance().client - try: - return await client(wot.identity_of, pubkey_uid) - except ValueError as e: - pass - - -async def is_member(pubkey_uid): - """ - Check identity is member - If member, return corresponding identity, else: False - """ - try: - return await identity_of(pubkey_uid) - except: - return False - - -async def wot_lookup(identifier): - """ - :identifier: identity or pubkey in part or whole - Return received and sent certifications lists of matching identities - if one identity found - """ - client = ClientInstance().client - try: - results = await client(wot.lookup, identifier) - return results["results"] - except DuniterError as e: - message_exit(e.message) - except ValueError as e: - pass - - -async def identities_from_pubkeys(pubkeys, uids): - """ - Make list of pubkeys unique, and remove empty strings - Request identities - """ - if not uids: - return list() - - uniq_pubkeys = list(filter(None, set(pubkeys))) - identities = list() - for pubkey in uniq_pubkeys: - try: - identities.append(await identity_of(pubkey)) - except Exception as e: - pass - await sleep(ASYNC_SLEEP) - return identities diff --git a/silkaj/wot_tools.py b/silkaj/wot_tools.py new file mode 100644 index 00000000..829b105f --- /dev/null +++ b/silkaj/wot_tools.py @@ -0,0 +1,83 @@ +""" +Copyright 2016-2021 Maël Azimi <m.a@moul.re> + +Silkaj is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Silkaj is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Silkaj. If not, see <https://www.gnu.org/licenses/>. +""" + +from asyncio import sleep +from duniterpy.api.bma import wot +from duniterpy.api.errors import DuniterError + +from silkaj.network_tools import ClientInstance +from silkaj.tools import message_exit +from silkaj.constants import ASYNC_SLEEP + + +async def identity_of(pubkey_uid): + """ + Only works for members + Not able to get corresponding uid from a non-member identity + Able to know if an identity is member or not + """ + client = ClientInstance().client + try: + return await client(wot.identity_of, pubkey_uid) + except ValueError as e: + pass + + +async def is_member(pubkey_uid): + """ + Check identity is member + If member, return corresponding identity, else: False + """ + try: + return await identity_of(pubkey_uid) + except: + return False + + +async def wot_lookup(identifier): + """ + :identifier: identity or pubkey in part or whole + Return received and sent certifications lists of matching identities + if one identity found + """ + client = ClientInstance().client + try: + results = await client(wot.lookup, identifier) + return results["results"] + except DuniterError as e: + message_exit(e.message) + except ValueError as e: + pass + + +async def identities_from_pubkeys(pubkeys, uids): + """ + Make list of pubkeys unique, and remove empty strings + Request identities + """ + if not uids: + return list() + + uniq_pubkeys = list(filter(None, set(pubkeys))) + identities = list() + for pubkey in uniq_pubkeys: + try: + identities.append(await identity_of(pubkey)) + except Exception as e: + pass + await sleep(ASYNC_SLEEP) + return identities diff --git a/tests/test_tui.py b/tests/test_tui.py index ca39bd96..ab30cab8 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -55,7 +55,7 @@ def test_display_amount(message, amount, currency_symbol): ) @pytest.mark.asyncio async def test_display_pubkey(message, pubkey, id, monkeypatch): - monkeypatch.setattr("silkaj.wot.is_member", patched_is_member) + monkeypatch.setattr("silkaj.wot_tools.is_member", patched_is_member) expected = [[message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]] if id: -- GitLab