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

View File

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

View File

@ -4,7 +4,7 @@ from lib.cesiumCommon import CesiumCommon, PUBKEY_REGEX
class Offers(CesiumCommon):
# 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())
# {"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 description: data['description'] = description
if city: data['city'] = city
if localisation:
if location:
geoPoint = {}
geoPoint['lat'] = localisation[0]
geoPoint['lon'] = localisation[1]
geoPoint['lat'] = location[0]
geoPoint['lon'] = location[1]
data['geoPoint'] = geoPoint
if picture:
picture = open(picture, 'rb').read()