From 87fe111d72845be8b49ec9ee796806ecd583c0f6 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Fri, 14 Oct 2022 18:37:11 +0200 Subject: [PATCH] Move wot to wot.tools (#330) --- silkaj/blockchain/blocks.py | 2 +- silkaj/idty_tools.py | 6 ++-- silkaj/money/balance.py | 2 +- silkaj/money/history.py | 2 +- silkaj/money/tools.py | 4 +-- silkaj/wot/certification.py | 3 +- silkaj/wot/lookup.py | 2 +- silkaj/wot/tools.py | 58 +++++++++++++++++++++++++++--- silkaj/wot/wot.py | 5 ++- silkaj/wot_tools.py | 70 ------------------------------------ tests/money/test_history.py | 6 ++-- tests/money/test_tools.py | 4 +-- tests/money/test_transfer.py | 5 +-- tests/wot/test_tools.py | 3 +- 14 files changed, 74 insertions(+), 98 deletions(-) delete mode 100644 silkaj/wot_tools.py diff --git a/silkaj/blockchain/blocks.py b/silkaj/blockchain/blocks.py index 563fb33d..96ce7e0f 100644 --- a/silkaj/blockchain/blocks.py +++ b/silkaj/blockchain/blocks.py @@ -26,7 +26,7 @@ from silkaj import tui from silkaj.blockchain.tools import get_head_block from silkaj.constants import ALL from silkaj.network import client_instance -from silkaj.wot_tools import identity_of +from silkaj.wot.tools import identity_of @command("blocks", help="Display blocks: default: 0 for current window size") diff --git a/silkaj/idty_tools.py b/silkaj/idty_tools.py index 1d1f265b..63fb8238 100644 --- a/silkaj/idty_tools.py +++ b/silkaj/idty_tools.py @@ -24,10 +24,10 @@ from duniterpy.api import bma from duniterpy.documents import BlockID, Identity, Revocation from texttable import Texttable -from silkaj import wot_tools as wt from silkaj.constants import ALL from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum +from silkaj.wot.tools import wot_lookup def display_identity(idty: Identity) -> Texttable: @@ -60,8 +60,8 @@ def check_many_identities(document: Union[Identity, Revocation]) -> bool: idty = document if doc_type == "Identity" else document.identity try: - results_pubkey = wt.wot_lookup(idty.pubkey) - results_uid = wt.wot_lookup(idty.uid) + results_pubkey = wot_lookup(idty.pubkey) + results_uid = wot_lookup(idty.uid) except urllib.error.HTTPError: sys.exit( f"{error_no_identical_id}\nuid: {idty.uid}\npubkey: \ diff --git a/silkaj/money/balance.py b/silkaj/money/balance.py index 8fff6ecf..9483cb75 100644 --- a/silkaj/money/balance.py +++ b/silkaj/money/balance.py @@ -19,12 +19,12 @@ from typing import List from click import Context, argument, command, echo, pass_context from silkaj import tui -from silkaj import wot_tools as wt from silkaj.auth import auth_method, has_auth_method from silkaj.blockchain.tools import get_head_block from silkaj.money import tools as m_tools from silkaj.public_key import gen_pubkey_checksum, is_pubkey_and_check from silkaj.tools import get_currency_symbol +from silkaj.wot import tools as wt @command("balance", help="Get wallet balance") diff --git a/silkaj/money/history.py b/silkaj/money/history.py index 98a8f892..005b3873 100644 --- a/silkaj/money/history.py +++ b/silkaj/money/history.py @@ -24,7 +24,6 @@ from duniterpy.documents.transaction import OutputSource, Transaction from duniterpy.grammars.output import Condition from pendulum import from_timestamp, now -from silkaj import wot_tools as wt from silkaj.constants import ALL, ALL_DIGITAL from silkaj.money.tools import ( amount_in_current_base, @@ -39,6 +38,7 @@ from silkaj.public_key import ( ) from silkaj.tools import get_currency_symbol from silkaj.tui import Table +from silkaj.wot import tools as wt @command("history", help="Display transaction history") diff --git a/silkaj/money/tools.py b/silkaj/money/tools.py index 2d5032e7..ba83be3f 100644 --- a/silkaj/money/tools.py +++ b/silkaj/money/tools.py @@ -19,9 +19,9 @@ from typing import List, Tuple, Union from duniterpy.api import bma from duniterpy.documents.transaction import InputSource, OutputSource -from silkaj import wot_tools from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum +from silkaj.wot import tools as wt def display_amount( @@ -45,7 +45,7 @@ def display_pubkey(tx: List, message: str, pubkey: str) -> None: Displays a pubkey and the eventually associated identity """ tx.append([f"{message} (pubkey:checksum)", gen_pubkey_checksum(pubkey)]) - idty = wot_tools.is_member(pubkey) + idty = wt.is_member(pubkey) if idty: tx.append([f"{message} (id)", idty["uid"]]) diff --git a/silkaj/wot/certification.py b/silkaj/wot/certification.py index 8b12b051..4cfa31bf 100644 --- a/silkaj/wot/certification.py +++ b/silkaj/wot/certification.py @@ -24,7 +24,6 @@ from duniterpy.key import SigningKey from pendulum import from_timestamp, now from silkaj import tui -from silkaj import wot_tools as wt from silkaj.auth import auth_method from silkaj.blockchain import tools as bc_tools from silkaj.constants import ALL, DATE @@ -85,7 +84,7 @@ def send_certification(ctx: click.Context, uid_pubkey_to_certify: str) -> None: def pre_checks(client: Client, issuer_pubkey: str, pubkey_to_certify: str) -> Dict: # Check whether current user is member - issuer = wt.is_member(issuer_pubkey) + issuer = wot_tools.is_member(issuer_pubkey) if not issuer: sys.exit("Current identity is not member.") diff --git a/silkaj/wot/lookup.py b/silkaj/wot/lookup.py index 88c3b0b8..8ed02536 100644 --- a/silkaj/wot/lookup.py +++ b/silkaj/wot/lookup.py @@ -17,9 +17,9 @@ import urllib import click -from silkaj import wot_tools as wt from silkaj.network import exit_on_http_error from silkaj.public_key import gen_pubkey_checksum, is_pubkey_and_check +from silkaj.wot import tools as wt @click.command("lookup", help="User identifier and public key lookup") diff --git a/silkaj/wot/tools.py b/silkaj/wot/tools.py index 2830208b..566fe264 100644 --- a/silkaj/wot/tools.py +++ b/silkaj/wot/tools.py @@ -14,16 +14,66 @@ # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import urllib -from typing import Dict, List, Tuple +from typing import Dict, List, Optional, Tuple +from urllib.error import HTTPError import click +from duniterpy.api.bma import wot -from silkaj import wot_tools as wt -from silkaj.network import exit_on_http_error +from silkaj.network import client_instance, exit_on_http_error from silkaj.public_key import gen_pubkey_checksum from silkaj.tui import Table +def identity_of(pubkey_uid: str) -> Dict: + """ + 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 = client_instance() + return client(wot.identity_of, pubkey_uid) + + +def is_member(pubkey_uid: str) -> Optional[Dict]: + """ + Check identity is member + If member, return corresponding identity, else: False + """ + try: + return identity_of(pubkey_uid) + except HTTPError: + return None + + +def wot_lookup(identifier: str) -> List: + """ + :identifier: identity or pubkey in part or whole + Return received and sent certifications lists of matching identities + if one identity found + """ + client = client_instance() + return (client(wot.lookup, identifier))["results"] + + +def identities_from_pubkeys(pubkeys: List[str], uids: bool) -> List: + """ + Make list of pubkeys unique, and remove empty strings + Request identities + """ + if not uids: + return [] + + uniq_pubkeys = list(filter(None, set(pubkeys))) + identities = [] + for pubkey in uniq_pubkeys: + try: + identities.append(identity_of(pubkey)) + except HTTPError: + pass + return identities + + def choose_identity(pubkey_uid: str) -> Tuple[Dict, str, List]: """ Get lookup from a pubkey or an uid @@ -33,7 +83,7 @@ def choose_identity(pubkey_uid: str) -> Tuple[Dict, str, List]: """ try: - lookups = wt.wot_lookup(pubkey_uid) + lookups = wot_lookup(pubkey_uid) except urllib.error.HTTPError as e: exit_on_http_error(e, 404, f"No identity found for {pubkey_uid}") diff --git a/silkaj/wot/wot.py b/silkaj/wot/wot.py index 635bdc43..887dca2c 100644 --- a/silkaj/wot/wot.py +++ b/silkaj/wot/wot.py @@ -20,13 +20,12 @@ import click from duniterpy.api.bma import blockchain, wot from pendulum import from_timestamp, now -from silkaj import wot_tools as wt from silkaj.blockchain.tools import get_blockchain_parameters from silkaj.constants import DATE from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum, is_pubkey_and_check from silkaj.tui import Table -from silkaj.wot import tools as w_tools +from silkaj.wot import tools as wt def get_sent_certifications( @@ -65,7 +64,7 @@ def received_sent_certifications(uid_pubkey: str) -> None: if checked_pubkey: uid_pubkey = str(checked_pubkey) - identity, pubkey, signed = w_tools.choose_identity(uid_pubkey) + identity, pubkey, signed = wt.choose_identity(uid_pubkey) certifications = OrderedDict() # type: OrderedDict params = get_blockchain_parameters() diff --git a/silkaj/wot_tools.py b/silkaj/wot_tools.py deleted file mode 100644 index b233f9ff..00000000 --- a/silkaj/wot_tools.py +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright 2016-2022 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 typing import Dict, List, Optional -from urllib.error import HTTPError - -from duniterpy.api.bma import wot - -from silkaj.network import client_instance - - -def identity_of(pubkey_uid: str) -> Dict: - """ - 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 = client_instance() - return client(wot.identity_of, pubkey_uid) - - -def is_member(pubkey_uid: str) -> Optional[Dict]: - """ - Check identity is member - If member, return corresponding identity, else: False - """ - try: - return identity_of(pubkey_uid) - except HTTPError: - return None - - -def wot_lookup(identifier: str) -> List: - """ - :identifier: identity or pubkey in part or whole - Return received and sent certifications lists of matching identities - if one identity found - """ - client = client_instance() - return (client(wot.lookup, identifier))["results"] - - -def identities_from_pubkeys(pubkeys: List[str], uids: bool) -> List: - """ - Make list of pubkeys unique, and remove empty strings - Request identities - """ - if not uids: - return [] - - uniq_pubkeys = list(filter(None, set(pubkeys))) - identities = [] - for pubkey in uniq_pubkeys: - try: - identities.append(identity_of(pubkey)) - except HTTPError: - pass - return identities diff --git a/tests/money/test_history.py b/tests/money/test_history.py index cd18e22d..7b49aa31 100644 --- a/tests/money/test_history.py +++ b/tests/money/test_history.py @@ -15,10 +15,10 @@ import pytest -from silkaj import wot_tools from silkaj.constants import PUBKEY_MAX_LENGTH, PUBKEY_MIN_LENGTH, SHORT_PUBKEY_SIZE from silkaj.money import history from silkaj.public_key import CHECKSUM_SIZE +from silkaj.wot import tools as wt from tests.patched.blockchain_tools import currency from tests.patched.tx_history import patched_get_transactions_history from tests.patched.wot import patched_identities_from_pubkeys @@ -41,9 +41,7 @@ def test_history_generate_txs_list_and_pubkey_uid_display(monkeypatch): # uid is at least one char : "<uid> - <pubkey>" adds min 4 chars to <pubkey> return pubkey + 4 - monkeypatch.setattr( - wot_tools, "identities_from_pubkeys", patched_identities_from_pubkeys - ) + monkeypatch.setattr(wt, "identities_from_pubkeys", patched_identities_from_pubkeys) client = "whatever" ud_value = 10.07 diff --git a/tests/money/test_tools.py b/tests/money/test_tools.py index 756a953e..d282d6ef 100644 --- a/tests/money/test_tools.py +++ b/tests/money/test_tools.py @@ -17,11 +17,11 @@ import duniterpy.api.bma.tx as bma_tx import pytest from click.testing import CliRunner -from silkaj import wot_tools from silkaj.cli import cli from silkaj.constants import FAILURE_EXIT_STATUS, G1_SYMBOL from silkaj.money import tools as m_tools from silkaj.public_key import gen_pubkey_checksum +from silkaj.wot import tools from tests.patched.test_constants import mock_ud_value from tests.patched.wot import patched_is_member @@ -52,7 +52,7 @@ def test_display_amount(message, amount, currency_symbol): ], ) def test_display_pubkey(message, pubkey, uid, monkeypatch): - monkeypatch.setattr(wot_tools, "is_member", patched_is_member) + monkeypatch.setattr(tools, "is_member", patched_is_member) expected = [[f"{message} (pubkey:checksum)", gen_pubkey_checksum(pubkey)]] if uid: diff --git a/tests/money/test_transfer.py b/tests/money/test_transfer.py index 57d93162..d5243b4f 100644 --- a/tests/money/test_transfer.py +++ b/tests/money/test_transfer.py @@ -26,12 +26,13 @@ from duniterpy.documents.transaction import ( Unlock, ) -from silkaj import auth, network, tools, wot_tools +from silkaj import auth, network, tools from silkaj.blockchain import tools as bc_tools from silkaj.cli import cli from silkaj.constants import CENT_MULT_TO_UNIT, G1_SYMBOL from silkaj.money import tools as m_tools from silkaj.money import transfer +from silkaj.wot import tools as wt from tests.patched.auth import patched_auth_method from tests.patched.blockchain_tools import fake_block_id, patched_get_head_block from tests.patched.money import Counter, patched_get_sources, patched_get_ud_value @@ -111,7 +112,7 @@ def test_gen_confirmation_table( monkeypatch, ): # patched functions - monkeypatch.setattr(wot_tools, "is_member", patched_is_member) + monkeypatch.setattr(wt, "is_member", patched_is_member) monkeypatch.setattr(m_tools, "get_ud_value", patched_get_ud_value) monkeypatch.setattr(tools, "get_currency_symbol", patched_get_currency_symbol) diff --git a/tests/wot/test_tools.py b/tests/wot/test_tools.py index 2e0aa4ea..f10b37f9 100644 --- a/tests/wot/test_tools.py +++ b/tests/wot/test_tools.py @@ -16,7 +16,6 @@ import click import pytest -from silkaj import wot_tools from silkaj.wot import tools as w_tools pubkey_titi_tata = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88" @@ -145,7 +144,7 @@ def patched_prompt_tutu(message): def test_choose_identity( selected_uid, pubkey, patched_prompt, patched_lookup, capsys, monkeypatch ): - monkeypatch.setattr(wot_tools, "wot_lookup", patched_lookup) + monkeypatch.setattr(w_tools, "wot_lookup", patched_lookup) monkeypatch.setattr(click, "prompt", patched_prompt) # pylint: disable=unused-variable idty_card, get_pubkey, signed = w_tools.choose_identity(pubkey) -- GitLab