Skip to content
Snippets Groups Projects

#258: Add CSV, JSON, YAML output formats on history command

Open #258: Add CSV, JSON, YAML output formats on history command
Open Mr-Djez requested to merge Mr-Djez/silkaj:outpout_format into main
Files
3
+ 75
14
@@ -15,11 +15,14 @@ You should have received a copy of the GNU Affero General Public License
@@ -15,11 +15,14 @@ You should have received a copy of the GNU Affero General Public License
along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
"""
"""
from click import command, argument, option, echo_via_pager, get_terminal_size
from click import command, argument, option, echo_via_pager, get_terminal_size, Choice
from texttable import Texttable
from texttable import Texttable
from operator import itemgetter, neg, eq, ne
from operator import itemgetter, neg, eq, ne
from time import time
from time import time
 
import json
 
import yaml
 
from duniterpy.api.bma.tx import history
from duniterpy.api.bma.tx import history
from duniterpy.documents.transaction import Transaction
from duniterpy.documents.transaction import Transaction
@@ -36,8 +39,16 @@ from silkaj.tools import CurrencySymbol
@@ -36,8 +39,16 @@ from silkaj.tools import CurrencySymbol
@argument("pubkey")
@argument("pubkey")
@option("--uids", "-u", is_flag=True, help="Display uids")
@option("--uids", "-u", is_flag=True, help="Display uids")
@option("--full-pubkey", "-f", is_flag=True, help="Display full-length pubkeys")
@option("--full-pubkey", "-f", is_flag=True, help="Display full-length pubkeys")
 
@option(
 
"--outformat",
 
"-o",
 
default="TABLE",
 
help="Out format. (json, yaml, table[default])",
 
multiple=False,
 
type=Choice(["JSON", "YAML", "TABLE"], case_sensitive=False),
 
)
@coroutine
@coroutine
async def transaction_history(pubkey, uids, full_pubkey):
async def transaction_history(pubkey, uids, full_pubkey, outformat):
if check_pubkey_format(pubkey):
if check_pubkey_format(pubkey):
pubkey = validate_checksum(pubkey)
pubkey = validate_checksum(pubkey)
@@ -45,34 +56,85 @@ async def transaction_history(pubkey, uids, full_pubkey):
@@ -45,34 +56,85 @@ async def transaction_history(pubkey, uids, full_pubkey):
ud_value = await UDValue().ud_value
ud_value = await UDValue().ud_value
currency_symbol = await CurrencySymbol().symbol
currency_symbol = await CurrencySymbol().symbol
header = await generate_header(pubkey, currency_symbol, ud_value)
received_txs, sent_txs = list(), list()
received_txs, sent_txs = list(), list()
await get_transactions_history(client, pubkey, received_txs, sent_txs)
await get_transactions_history(client, pubkey, received_txs, sent_txs)
remove_duplicate_txs(received_txs, sent_txs)
remove_duplicate_txs(received_txs, sent_txs)
txs_list = await generate_table(
txs_list = await generate_table(
received_txs, sent_txs, pubkey, ud_value, currency_symbol, uids, full_pubkey
received_txs, sent_txs, pubkey, ud_value, currency_symbol, uids, full_pubkey
)
)
table = Texttable(max_width=get_terminal_size()[0])
if outformat.upper() in ["JSON", "YAML"]:
table.add_rows(txs_list)
header = await get_header(pubkey, currency_symbol, ud_value)
 
data = make_data_structured(header, txs_list, "Transactions")
 
if outformat.upper() == "JSON":
 
print(json.dumps(data, indent=4, ensure_ascii=False))
 
else: # YAML
 
print(yaml.dump(data, allow_unicode=True))
 
else:
 
table = Texttable(max_width=get_terminal_size()[0])
 
table.add_rows(txs_list)
 
header = await generate_header_table(pubkey, currency_symbol, ud_value)
 
echo_via_pager(header + table.draw())
Please register or sign in to reply
 
await client.close()
await client.close()
echo_via_pager(header + table.draw())
async def generate_header(pubkey, currency_symbol, ud_value):
def list_list_to_list_dict(txs_list):
Please register or sign in to reply
 
"""
 
This function allows you to switch from the transaction list to a dictionary list.
 
 
NOTE: part of the headers made for an array output to make them dictionary keys
 
is probably not the best idea in the world!?
 
"""
 
entt = txs_list.pop(0)
 
txs_lst_of_dists = list()
 
for tx in txs_list:
 
tmp = dict()
 
for i in range(0, len(tx)):
 
tmp[entt[i]] = tx[i]
 
txs_lst_of_dists.append(tmp)
 
return txs_lst_of_dists
 
 
 
def make_data_structured(header, txs_list, name_key):
 
"""
 
Assembling the header and transaction data structure
 
"""
 
t2d = list_list_to_list_dict(txs_list)
 
header[name_key] = t2d
 
return header
 
 
 
async def get_header(pubkey, currency_symbol, ud_value):
 
"""
 
Collects the data of the headers in a dictionary.
 
"""
try:
try:
idty = await wot.identity_of(pubkey)
idty = await wot.identity_of(pubkey)
except:
except:
idty = dict([("uid", "")])
idty = dict([("uid", "")])
balance = await get_amount_from_pubkey(pubkey)
balance = await get_amount_from_pubkey(pubkey)
 
data = {
 
"uid": idty["uid"],
 
"pubkey": display_pubkey_and_checksum(pubkey),
 
"currency": currency_symbol,
 
"balance": balance[1] / 100,
 
"balance_ud": round(balance[1] / ud_value, 2),
 
"date": convert_time(time(), "all"),
 
}
 
return data
 
 
 
async def generate_header_table(pubkey, currency_symbol, ud_value):
 
data = await get_header(pubkey, currency_symbol, ud_value)
return "Transactions history from: {uid} {pubkey}\n\
return "Transactions history from: {uid} {pubkey}\n\
Current balance: {balance} {currency}, {balance_ud} UD {currency} on the {date}\n\
Current balance: {balance} {currency}, {balance_ud} UD {currency} on the {date}\n\
".format(
".format(
uid=idty["uid"],
uid=data["uid"],
pubkey=display_pubkey_and_checksum(pubkey),
pubkey=data["pubkey"],
currency=currency_symbol,
currency=data["currency"],
balance=balance[1] / 100,
balance=data["balance"],
balance_ud=round(balance[1] / ud_value, 2),
balance_ud=data["balance_ud"],
date=convert_time(time(), "all"),
date=data["date"],
)
)
@@ -118,7 +180,6 @@ async def generate_table(
@@ -118,7 +180,6 @@ async def generate_table(
)
)
await parse_sent_tx(sent_txs_table, sent_txs, pubkey, ud_value, uids, full_pubkey)
await parse_sent_tx(sent_txs_table, sent_txs, pubkey, ud_value, uids, full_pubkey)
txs_table = received_txs_table + sent_txs_table
txs_table = received_txs_table + sent_txs_table
table_titles = [
table_titles = [
"Date",
"Date",
"Issuers/Recipients",
"Issuers/Recipients",
Loading