diff --git a/silkaj/tui.py b/silkaj/tui.py index 8d99094a0501d4efb0bf8ea002fa231abc4f7c74..53abbc5b29e8b295512791a161b18f77092a936e 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 b26d1fbea030ae15adbcfb8cc98cf9a938bbb7dd..5b837a1f2e6d4e2cf7c3183d83fae3e4f6270ebc 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 0000000000000000000000000000000000000000..829b105fb3cfc7ca548a00eb6ceeeb3c39ee00c9 --- /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 ca39bd96a7d9f2e15f7a67cf4acb2dd045c5bb6c..ab30cab81b5c0daa1a99e4dcfb0599db772f53c6 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: