refacto args passing

This commit is contained in:
poka 2023-09-10 09:44:52 +02:00
parent 5fe53c1d4c
commit 56f5972ad0
3 changed files with 62 additions and 65 deletions

113
jaklis.py
View File

@ -62,6 +62,7 @@ commands = {
("j", "json"): {"action": "store_true", "help": "Output in JSON format"}, ("j", "json"): {"action": "store_true", "help": "Output in JSON format"},
("o", "outbox"): {"action": "store_true", "help": "Read sent messages"}, ("o", "outbox"): {"action": "store_true", "help": "Read sent messages"},
}, },
"type": "cesium",
}, },
"send": { "send": {
"help": "Send a message", "help": "Send a message",
@ -78,6 +79,7 @@ commands = {
"help": "Send the message to the outbox", "help": "Send the message to the outbox",
}, },
}, },
"type": "cesium",
}, },
"delete": { "delete": {
"help": "Delete a message", "help": "Delete a message",
@ -93,6 +95,7 @@ commands = {
"help": "Delete a sent message", "help": "Delete a sent message",
}, },
}, },
"type": "cesium",
}, },
"get": { "get": {
"help": "View a Cesium+ profile", "help": "View a Cesium+ profile",
@ -103,6 +106,7 @@ commands = {
"help": "Also retrieve the avatar in raw base64 format", "help": "Also retrieve the avatar in raw base64 format",
}, },
}, },
"type": "cesium",
}, },
"page": { "page": {
"help": "View a Cesium+ page", "help": "View a Cesium+ page",
@ -113,6 +117,7 @@ commands = {
"help": "Also retrieve the page's avatar in raw base64 format", "help": "Also retrieve the page's avatar in raw base64 format",
}, },
}, },
"type": "cesium",
}, },
"set": { "set": {
"help": "Configure your Cesium+ profile", "help": "Configure your Cesium+ profile",
@ -128,26 +133,34 @@ commands = {
("s", "site"): {"help": "Profile website"}, ("s", "site"): {"help": "Profile website"},
("A", "avatar"): {"help": "Path to profile avatar in PNG"}, ("A", "avatar"): {"help": "Path to profile avatar in PNG"},
}, },
"type": "cesium",
},
"erase": {
"help": "Erase your Cesium+ profile",
"arguments": {},
"type": "cesium",
}, },
"erase": {"help": "Erase your Cesium+ profile", "arguments": {}},
"stars": { "stars": {
"help": "View a profile's stars / Rate a profile (option -s RATING)", "help": "View a profile's stars / Rate a profile (option -s RATING)",
"arguments": { "arguments": {
("p", "profile"): {"help": "Target profile"}, ("p", "profile"): {"help": "Target profile"},
("n", "number"): {"type": int, "help": "Number of stars"}, ("n", "number"): {"type": int, "help": "Number of stars"},
}, },
"type": "cesium",
}, },
"unstars": { "unstars": {
"help": "Remove a star", "help": "Remove a star",
"arguments": { "arguments": {
("p", "profile"): {"help": "Profile to unstar"}, ("p", "profile"): {"help": "Profile to unstar"},
}, },
"type": "cesium",
}, },
"getoffer": { "getoffer": {
"help": "Get information about a Ḡchange listing", "help": "Get information about a Ḡchange listing",
"arguments": { "arguments": {
("i", "id"): {"help": "Target listing to retrieve"}, ("i", "id"): {"help": "Target listing to retrieve"},
}, },
"type": "cesium",
}, },
"setoffer": { "setoffer": {
"help": "Create a Ḡchange listing", "help": "Create a Ḡchange listing",
@ -163,12 +176,19 @@ commands = {
("ci", "city"): {"help": "City of the listing to create"}, ("ci", "city"): {"help": "City of the listing to create"},
("pr", "price"): {"help": "Price of the listing to create"}, ("pr", "price"): {"help": "Price of the listing to create"},
}, },
"type": "cesium",
}, },
"deleteoffer": { "deleteoffer": {
"help": "Delete a Ḡchange listing", "help": "Delete a Ḡchange listing",
"arguments": { "arguments": {
("i", "id"): {"help": "Target listing to delete"}, ("i", "id"): {"help": "Target listing to delete"},
}, },
"type": "cesium",
},
"geolocProfiles": {
"help": "Get JSON of all geolocated accounts",
"arguments": {},
"type": "cesium",
}, },
"pay": { "pay": {
"help": "Pay in Ḡ1", "help": "Pay in Ḡ1",
@ -189,6 +209,7 @@ commands = {
"help": "Display the JSON result of the transaction", "help": "Display the JSON result of the transaction",
}, },
}, },
"type": "gva",
}, },
"history": { "history": {
"help": "View Ḡ1 account transaction history", "help": "View Ḡ1 account transaction history",
@ -208,6 +229,7 @@ commands = {
"help": "Display the result in black and white", "help": "Display the result in black and white",
}, },
}, },
"type": "gva",
}, },
"balance": { "balance": {
"help": "View Ḡ1 account balance", "help": "View Ḡ1 account balance",
@ -218,6 +240,7 @@ commands = {
"help": "Use mempool sources", "help": "Use mempool sources",
}, },
}, },
"type": "gva",
}, },
"id": { "id": {
"help": "View public key/username identity", "help": "View public key/username identity",
@ -225,43 +248,44 @@ commands = {
("p", "pubkey"): {"help": "Public key of the target account"}, ("p", "pubkey"): {"help": "Public key of the target account"},
("u", "username"): {"help": "Username of the target account"}, ("u", "username"): {"help": "Username of the target account"},
}, },
"type": "gva",
}, },
"idBalance": { "idBalance": {
"help": "View public key/username identity and balance", "help": "View public key/username identity and balance",
"arguments": { "arguments": {
("p", "pubkey"): {"help": "Public key of the target account"}, ("p", "pubkey"): {"help": "Public key of the target account"},
}, },
"type": "gva",
}, },
"currentUd": { "currentUd": {
"help": "Display the current Universal Dividend amount", "help": "Display the current Universal Dividend amount",
"arguments": { "arguments": {
("p", "pubkey"): {"help": "Public key of the target account"}, ("p", "pubkey"): {"help": "Public key of the target account"},
}, },
"type": "gva",
}, },
"listWallets": { "listWallets": {
"help": "List all G1 wallets", "help": "List all G1 wallets",
"arguments": { "arguments": {
("b", "balance"): {"action": "store_true", "help": "Display balances"}, ("m", "mbr"): {
("mbr"): {
"action": "store_true", "action": "store_true",
"help": "Display raw list of member pubkeys", "help": "Display raw list of member pubkeys",
}, },
("non_mbr"): { ("nm", "non_mbr"): {
"action": "store_true", "action": "store_true",
"help": "Display raw list of nonmember identity pubkeys", "help": "Display raw list of nonmember identity pubkeys",
}, },
("larf"): { ("l", "larf"): {
"action": "store_true", "action": "store_true",
"help": "Display raw list of nonmember pubkeys", "help": "Display raw list of nonmember pubkeys",
}, },
("brut"): { ("b", "brut"): {
"action": "store_true", "action": "store_true",
"help": "Display raw list of all pubkeys", "help": "Display raw list of all pubkeys",
}, },
("p", "pubkey"): {"help": "Useless but needed"},
}, },
"type": "gva",
}, },
"geolocProfiles": {"help": "Get JSON of all geolocated accounts", "arguments": {}},
} }
# Process commands and arguments # Process commands and arguments
@ -276,8 +300,8 @@ for cmd, cmd_info in commands.items():
cmd_parser.add_argument("-" + short_arg, "--" + long_arg, **kwargs) cmd_parser.add_argument("-" + short_arg, "--" + long_arg, **kwargs)
args = parser.parse_args() args = parser.parse_args()
args_dict = vars(args)
cmd = args.cmd cmd = args.cmd
if args.version: if args.version:
print(__version__) print(__version__)
sys.exit(0) sys.exit(0)
@ -336,6 +360,7 @@ noNeedDunikey = cmd in (
"id", "id",
"idBalance", "idBalance",
"listWallets", "listWallets",
"geolocProfiles",
) and (pubkey or profile) ) and (pubkey or profile)
if noNeedDunikey: if noNeedDunikey:
@ -347,9 +372,14 @@ keyPath = False if dunikey else createTmpDunikey()
def handle_cesium_commands(args, cmd, cesium): def handle_cesium_commands(args, cmd, cesium):
# Get args of the command
cmd_args = list(zip(*list(commands[cmd]["arguments"].keys())))[1]
cmd_args_dict = {arg: args_dict[arg] for arg in cmd_args if arg in args_dict}
cmd_args_values = list(cmd_args_dict.values())
# Messaging # Messaging
if cmd == "read": if cmd == "read":
cesium.read(args.number, args.outbox, args.json) cesium.read(*cmd_args_values)
elif cmd == "send": elif cmd == "send":
if args.fichier: if args.fichier:
with open(args.fichier, "r") as f: with open(args.fichier, "r") as f:
@ -373,19 +403,11 @@ def handle_cesium_commands(args, cmd, cesium):
# Profiles # Profiles
elif cmd == "set": elif cmd == "set":
cesium.set( cesium.set(**cmd_args_dict)
args.name,
args.description,
args.ville,
args.adresse,
args.position,
args.site,
args.avatar,
)
elif cmd == "get": elif cmd == "get":
cesium.get(args.profile, args.avatar) cesium.get(**cmd_args_dict)
elif cmd == "page": elif cmd == "page":
cesium.getPage(args.page, args.avatar) cesium.getPage(**cmd_args_dict)
elif cmd == "erase": elif cmd == "erase":
cesium.erase() cesium.erase()
elif cmd == "geolocProfiles": elif cmd == "geolocProfiles":
@ -404,22 +426,19 @@ def handle_cesium_commands(args, cmd, cesium):
elif cmd == "getoffer": elif cmd == "getoffer":
cesium.getOffer(args.id) cesium.getOffer(args.id)
elif cmd == "setoffer": elif cmd == "setoffer":
cesium.setOffer( cesium.setOffer(**cmd_args_dict)
args.title,
args.description,
args.city,
args.localisation,
args.category,
args.price,
args.picture,
)
elif cmd == "deleteoffer": elif cmd == "deleteoffer":
cesium.deleteOffer(args.id) cesium.deleteOffer(**cmd_args_dict)
else: else:
raise ValueError(f"Unknown command: {cmd}") raise ValueError(f"Unknown command: {cmd}")
def handle_gva_commands(args, cmd, gva): def handle_gva_commands(args, cmd, gva):
# Get args of the command
cmd_args = list(zip(*list(commands[cmd]["arguments"].keys())))[1]
cmd_args_dict = {arg: args_dict[arg] for arg in cmd_args if arg in args_dict}
# cmd_args_values = list(cmd_args_dict.values())
if cmd == "pay": if cmd == "pay":
gva.pay(args.amount, args.comment, args.mempool, args.verbose) gva.pay(args.amount, args.comment, args.mempool, args.verbose)
elif cmd == "history": elif cmd == "history":
@ -427,9 +446,9 @@ def handle_gva_commands(args, cmd, gva):
elif cmd == "balance": elif cmd == "balance":
gva.balance(args.mempool) gva.balance(args.mempool)
elif cmd == "id": elif cmd == "id":
gva.id(args.pubkey, args.username) gva.id(**cmd_args_dict)
elif cmd == "idBalance": elif cmd == "idBalance":
gva.idBalance(args.pubkey) gva.idBalance(**cmd_args_dict)
elif cmd == "currentUd": elif cmd == "currentUd":
gva.currentUd() gva.currentUd()
elif cmd == "listWallets": elif cmd == "listWallets":
@ -439,21 +458,7 @@ def handle_gva_commands(args, cmd, gva):
# Construct the CesiumPlus object # Construct the CesiumPlus object
if cmd in ( if commands[cmd]["type"] == "cesium":
"read",
"send",
"delete",
"set",
"get",
"page",
"erase",
"stars",
"unstars",
"getoffer",
"setoffer",
"deleteoffer",
"geolocProfiles",
):
if args.node: if args.node:
pod = args.node pod = args.node
@ -461,19 +466,11 @@ if cmd in (
handle_cesium_commands(args, cmd, cesium) handle_cesium_commands(args, cmd, cesium)
# Construct the GvaApi object # Construct the GvaApi object
elif cmd in ( elif commands[cmd]["type"] == "gva":
"pay",
"history",
"balance",
"id",
"idBalance",
"currentUd",
"listWallets",
):
if args.node: if args.node:
node = args.node node = args.node
if args.pubkey: if hasattr(args, "pubkey"):
destPubkey = args.pubkey destPubkey = args.pubkey
gva = GvaApi(dunikey, node, destPubkey, noNeedDunikey) gva = GvaApi(dunikey, node, destPubkey, noNeedDunikey)

View File

@ -12,7 +12,7 @@ from lib.offers import Offers
class CesiumPlus(CesiumCommon): class CesiumPlus(CesiumCommon):
#################### Messaging #################### #################### Messaging ####################
def read(self, nbrMsg, outbox, isJSON): def read(self, nbrMsg, isJSON, outbox):
readCesium = ReadFromCesium(self.dunikey, self.pod) readCesium = ReadFromCesium(self.dunikey, self.pod)
jsonMsg = readCesium.sendDocument(nbrMsg, outbox) jsonMsg = readCesium.sendDocument(nbrMsg, outbox)
if isJSON: if isJSON:
@ -139,14 +139,14 @@ class CesiumPlus(CesiumCommon):
title=None, title=None,
description=None, description=None,
city=None, city=None,
localisation=None, location=None,
category=None, category=None,
price=None, price=None,
picture=None, picture=None,
): ):
setOffer = Offers(self.dunikey, self.pod) setOffer = Offers(self.dunikey, self.pod)
document = setOffer.configDocSet( document = setOffer.configDocSet(
title, description, city, localisation, category, price, picture title, description, city, location, category, price, picture
) )
result = setOffer.sendDocumentSet(document, "set") result = setOffer.sendDocumentSet(document, "set")

View File

@ -4,7 +4,7 @@ from lib.cesiumCommon import CesiumCommon, PUBKEY_REGEX
class Offers(CesiumCommon): class Offers(CesiumCommon):
# Configure JSON document SET to send # Configure JSON document SET to send
def configDocSet(self, title, description, city, localisation, category, price: float, picture): def configDocSet(self, title, description, city, location, category, price: float, picture):
timeSent = int(time()) timeSent = int(time())
# {"parent":"cat90","localizedNames":{"en":"Fruits & Vegetables","es-ES":"Frutas y Vegetales","fr-FR":"Fruits & Légumes"},"name":"Fruits & Légumes","id":"cat92"} # {"parent":"cat90","localizedNames":{"en":"Fruits & Vegetables","es-ES":"Frutas y Vegetales","fr-FR":"Fruits & Légumes"},"name":"Fruits & Légumes","id":"cat92"}
@ -13,10 +13,10 @@ class Offers(CesiumCommon):
if title: data['title'] = title if title: data['title'] = title
if description: data['description'] = description if description: data['description'] = description
if city: data['city'] = city if city: data['city'] = city
if localisation: if location:
geoPoint = {} geoPoint = {}
geoPoint['lat'] = localisation[0] geoPoint['lat'] = location[0]
geoPoint['lon'] = localisation[1] geoPoint['lon'] = location[1]
data['geoPoint'] = geoPoint data['geoPoint'] = geoPoint
if picture: if picture:
picture = open(picture, 'rb').read() picture = open(picture, 'rb').read()