Commit 9446dfaa authored by matograine's avatar matograine
Browse files

[mod] #344 remove loop dependency

  * create silkaj.wot_tools module
  * remove tui.convert_time()
  * update imports
  * modify related tests
parent 0d57e0a3
Pipeline #12442 passed with stages
in 2 minutes and 22 seconds
......@@ -23,8 +23,7 @@ from pathlib import Path
from duniterpy.key import SigningKey
from duniterpy.key.scrypt_params import ScryptParams
# had to import display_pubkey_and_checksum from wot to avoid loop dependency.
from silkaj.wot import display_pubkey_and_checksum
from silkaj.tui import display_pubkey_and_checksum
from silkaj.tools import message_exit
from silkaj.constants import PUBKEY_PATTERN
......
......@@ -31,6 +31,7 @@ from silkaj.license import license_approval
from silkaj import wot, tui
from silkaj.constants import SUCCESS_EXIT_STATUS, DATE, ALL
from silkaj.crypto_tools import is_pubkey_and_check
from silkaj import wot_tools as wt
@click.command("cert", help="Send certification")
......@@ -92,7 +93,7 @@ async def send_certification(ctx, uid_pubkey_to_certify):
async def pre_checks(client, issuer_pubkey, pubkey_to_certify):
# Check whether current user is member
issuer = await wot.is_member(issuer_pubkey)
issuer = await wt.is_member(issuer_pubkey)
if not issuer:
message_exit("Current identity is not member.")
......
......@@ -29,7 +29,7 @@ from _socket import gaierror
from duniterpy.api import bma
from silkaj.tools import coroutine
from silkaj.wot import identity_of
from silkaj.wot_tools import identity_of
from silkaj.network_tools import (
best_endpoint_address,
EndPoint,
......
......@@ -18,13 +18,11 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from click import command, argument, pass_context, echo
from tabulate import tabulate
from silkaj import wot_tools as wt
from silkaj.network_tools import ClientInstance
from silkaj.blockchain_tools import HeadBlock
from silkaj.tools import CurrencySymbol, message_exit, coroutine
from silkaj.auth import auth_method, has_auth_method
# had to import wot to prevent loop dependency. No use here.
from silkaj import wot
from silkaj.crypto_tools import (
is_pubkey_and_check,
check_pubkey_format,
......@@ -91,7 +89,7 @@ async def show_amount_from_pubkey(label, inputs_balance):
# if `pubkey` is a pubkey, get pubkey:checksum and uid
if label != "Total":
member = await wot.is_member(label)
member = await wt.is_member(label)
label = display_pubkey_and_checksum(label)
# display balance table
display = list()
......
......@@ -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"]])
......@@ -67,19 +67,3 @@ async def send_doc_confirmation(document_name):
client = network_tools.ClientInstance().client
await client.close()
sys.exit(constants.SUCCESS_EXIT_STATUS)
def convert_time(timestamp, kind):
ts = int(timestamp)
date = "%Y-%m-%d"
hour = "%H:%M"
second = ":%S"
if kind == "all":
pattern = date + " " + hour + second
elif kind == "date":
pattern = date
elif kind == "hour":
pattern = hour
if ts >= 3600:
pattern += second
return datetime.fromtimestamp(ts).strftime(pattern)
......@@ -23,6 +23,7 @@ from operator import itemgetter, neg, eq, ne
from duniterpy.api.bma.tx import history
from duniterpy.documents.transaction import Transaction
from silkaj import wot_tools as wt
from silkaj.network_tools import ClientInstance
from silkaj.tools import coroutine
from silkaj.tui import display_pubkey_and_checksum
......@@ -61,7 +62,7 @@ async def transaction_history(pubkey, uids, full_pubkey):
async def generate_header(pubkey, currency_symbol, ud_value):
try:
idty = await wot.identity_of(pubkey)
idty = await wt.identity_of(pubkey)
except:
idty = dict([("uid", "")])
balance = await get_amount_from_pubkey(pubkey)
......@@ -148,7 +149,7 @@ async def parse_received_tx(
for received_tx in received_txs:
for issuer in received_tx.issuers:
issuers.append(issuer)
identities = await wot.identities_from_pubkeys(issuers, uids)
identities = await wt.identities_from_pubkeys(issuers, uids)
for received_tx in received_txs:
tx_list = list()
tx_list.append(from_timestamp(received_tx.time, tz="local").format(ALL_DIGITAL))
......@@ -180,7 +181,7 @@ async def parse_sent_tx(sent_txs_table, sent_txs, pubkey, ud_value, uids, full_p
if output_available(output.condition, ne, pubkey):
pubkeys.append(output.condition.left.pubkey)
identities = await wot.identities_from_pubkeys(pubkeys, uids)
identities = await wt.identities_from_pubkeys(pubkeys, uids)
for sent_tx in sent_txs:
tx_list = list()
tx_list.append(from_timestamp(sent_tx.time, tz="local").format(ALL_DIGITAL))
......
......@@ -19,16 +19,16 @@ import click
from pendulum import from_timestamp, now
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 import wot_tools as wt
from silkaj.network_tools import ClientInstance
from silkaj.crypto_tools import is_pubkey_and_check
from silkaj.tools import message_exit, coroutine
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):
......@@ -123,7 +123,7 @@ async def membership_status(certifications, pubkey, req):
len(certifications["received"]) - params["sigQty"]
]
)
member = await is_member(pubkey)
member = await wt.is_member(pubkey)
if member:
member = True
print("member:", member)
......@@ -160,7 +160,7 @@ async def id_pubkey_correspondence(uid_pubkey):
uid_pubkey = checked_pubkey
client = ClientInstance().client
lookups = await wot_lookup(uid_pubkey)
lookups = await wt.wot_lookup(uid_pubkey)
await client.close()
content = f"Public keys or user id found matching '{uid_pubkey}':\n"
......@@ -178,7 +178,7 @@ async def choose_identity(pubkey_uid):
If there is one uid, returns it
If there is multiple uids, prompt a selector
"""
lookups = await wot_lookup(pubkey_uid)
lookups = await wt.wot_lookup(pubkey_uid)
# Generate table containing the choices
identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []}
......@@ -215,62 +215,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
"""
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
......@@ -18,6 +18,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
import pytest
from silkaj.tui import display_pubkey, display_amount, display_pubkey_and_checksum
from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE
from silkaj import wot_tools
from patched.wot import patched_is_member
from patched.test_constants import mock_ud_value
......@@ -55,7 +56,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(wot_tools, "is_member", patched_is_member)
expected = [[message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]]
if id:
......
......@@ -16,7 +16,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
"""
import pytest
from silkaj import tx_history, wot
from silkaj import tx_history, wot, wot_tools
from silkaj.constants import (
G1_DEFAULT_ENDPOINT,
SHORT_PUBKEY_SIZE,
......@@ -48,7 +48,9 @@ async def test_tx_history_generate_table_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, "identities_from_pubkeys", patched_identities_from_pubkeys)
monkeypatch.setattr(
wot_tools, "identities_from_pubkeys", patched_identities_from_pubkeys
)
client = "whatever"
ud_value = 10.07
......
......@@ -18,7 +18,16 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
import sys
import pytest
from silkaj import tx, wot, money, tools, blockchain_tools, auth, network_tools
from silkaj import (
tx,
wot,
wot_tools,
money,
tools,
blockchain_tools,
auth,
network_tools,
)
from silkaj.tui import display_pubkey, display_amount
from silkaj.constants import (
G1_SYMBOL,
......@@ -128,7 +137,7 @@ async def test_gen_confirmation_table(
monkeypatch,
):
# patched functions
monkeypatch.setattr(wot, "is_member", patched_is_member)
monkeypatch.setattr(wot_tools, "is_member", patched_is_member)
monkeypatch.setattr(money.UDValue, "get_ud_value", patched_ud_value)
monkeypatch.setattr(tools.CurrencySymbol, "get_symbol", patched_currency_symbol)
......
......@@ -19,7 +19,7 @@ import pytest
import click
from silkaj import wot
from silkaj import wot_tools
pubkey_titi_tata = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88"
pubkey_toto_tutu = "totoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88"
......@@ -148,7 +148,7 @@ def patched_prompt_tutu(message):
async def test_choose_identity(
selected_uid, pubkey, patched_prompt, patched_lookup, capsys, monkeypatch
):
monkeypatch.setattr(wot, "wot_lookup", patched_lookup)
monkeypatch.setattr(wot_tools, "wot_lookup", patched_lookup)
monkeypatch.setattr(click, "prompt", patched_prompt)
identity_card, get_pubkey, signed = await wot.choose_identity(pubkey)
expected_pubkey = pubkey.split(":")[0]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment