refacto and translate main file

This commit is contained in:
poka 2023-09-10 02:03:42 +02:00
parent 595f7722bf
commit 6dc86592b9
1 changed files with 118 additions and 125 deletions

243
jaklis.py
View File

@ -1,6 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse, sys, os, string, random import argparse
import sys
import os
import string
import random
from os.path import join, dirname from os.path import join, dirname
from shutil import copyfile from shutil import copyfile
from dotenv import load_dotenv from dotenv import load_dotenv
@ -10,96 +14,87 @@ __version__ = "0.0.5"
MY_PATH = os.path.realpath(sys.argv[0]).replace("jaklis.py", "") MY_PATH = os.path.realpath(sys.argv[0]).replace("jaklis.py", "")
# Get variables environment # Get environment variables
if not os.path.isfile(MY_PATH + ".env"): if not os.path.isfile(MY_PATH + ".env"):
copyfile(MY_PATH + ".env.template", MY_PATH + ".env") copyfile(MY_PATH + ".env.template", MY_PATH + ".env")
dotenv_path = join(dirname(__file__), MY_PATH + ".env") dotenv_path = join(dirname(__file__), MY_PATH + ".env")
load_dotenv(dotenv_path) load_dotenv(dotenv_path)
# Set global values (default parameters) , regarding variables environments # Set global values (default parameters) regarding environment variables
node = os.getenv("DUNITER") + "/gva" node = (
if not node: os.getenv("DUNITER") + "/gva"
node = "https://g1v1.p2p.legal/gva" if os.getenv("DUNITER")
else "https://g1v1.p2p.legal/gva"
pod = os.getenv("ESNODE") )
if not pod: pod = os.getenv("ESNODE") if os.getenv("ESNODE") else "https://g1.data.e-is.pro"
pod = "https://g1.data.e-is.pro"
destPubkey = False destPubkey = False
# Parse arguments # Parse arguments
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Client CLI pour Cesium+ et Ḡchange", description="CLI Client for Cesium+ and Ḡchange",
epilog="current node: '" + node + "', current pod: '" + pod + "'.", epilog="current node: '" + node + "', current pod: '" + pod + "'.",
) )
parser.add_argument( parser.add_argument(
"-v", "-v",
"--version", "--version",
action="store_true", action="store_true",
help="Affiche la version actuelle du programme", help="Display the current program version",
) )
parser.add_argument("-k", "--key", help="Chemin vers mon trousseau de clé (PubSec)") parser.add_argument("-k", "--key", help="Path to the keyfile (PubSec)")
parser.add_argument( parser.add_argument(
"-n", "--node", help="Adresse du noeud Cesium+, Gchange ou Duniter à utiliser" "-n", "--node", help="Address of the Cesium+, Gchange, or Duniter node to use"
) )
subparsers = parser.add_subparsers(title="Commandes de jaklis", dest="cmd") # Create subparsers for different commands
read_cmd = subparsers.add_parser("read", help="Lecture des messages") subparsers = parser.add_subparsers(title="jaklis Commands", dest="cmd")
send_cmd = subparsers.add_parser("send", help="Envoi d'un message") read_cmd = subparsers.add_parser("read", help="Read messages")
delete_cmd = subparsers.add_parser("delete", help="Supression d'un message") send_cmd = subparsers.add_parser("send", help="Send a message")
getProfile_cmd = subparsers.add_parser("get", help="Voir un profile Cesium+") delete_cmd = subparsers.add_parser("delete", help="Delete a message")
getPage_cmd = subparsers.add_parser("page", help="Voir une page Cesium+") getProfile_cmd = subparsers.add_parser("get", help="View a Cesium+ profile")
setProfile_cmd = subparsers.add_parser("set", help="Configurer son profile Cesium+") getPage_cmd = subparsers.add_parser("page", help="View a Cesium+ page")
eraseProfile_cmd = subparsers.add_parser("erase", help="Effacer son profile Cesium+") setProfile_cmd = subparsers.add_parser("set", help="Configure your Cesium+ profile")
eraseProfile_cmd = subparsers.add_parser("erase", help="Erase your Cesium+ profile")
stars_cmd = subparsers.add_parser( stars_cmd = subparsers.add_parser(
"stars", help="Voir les étoiles d'un profile / Noter un profile (option -s NOTE)" "stars", help="View a profile's stars / Rate a profile (option -s RATING)"
) )
unstars_cmd = subparsers.add_parser("unstars", help="Supprimer un star") unstars_cmd = subparsers.add_parser("unstars", help="Remove a star")
getoffer_cmd = subparsers.add_parser( getoffer_cmd = subparsers.add_parser(
"getoffer", help="Obtenir les informations d'une annonce gchange" "getoffer", help="Get information about a Ḡchange listing"
) )
setoffer_cmd = subparsers.add_parser("setoffer", help="Créer une annonce gchange") setoffer_cmd = subparsers.add_parser("setoffer", help="Create a Ḡchange listing")
deleteoffer_cmd = subparsers.add_parser( deleteoffer_cmd = subparsers.add_parser("deleteoffer", help="Delete a Ḡchange listing")
"deleteoffer", help="Supprimer une annonce gchange" pay_cmd = subparsers.add_parser("pay", help="Pay in Ḡ1")
)
pay_cmd = subparsers.add_parser("pay", help="Payer en Ḡ1")
history_cmd = subparsers.add_parser( history_cmd = subparsers.add_parser(
"history", help="Voir l'historique des transactions d'un compte Ḡ1" "history", help="View Ḡ1 account transaction history"
) )
balance_cmd = subparsers.add_parser("balance", help="Voir le solde d'un compte Ḡ1") balance_cmd = subparsers.add_parser("balance", help="View Ḡ1 account balance")
id_cmd = subparsers.add_parser("id", help="Voir l'identité d'une clé publique/username") id_cmd = subparsers.add_parser("id", help="View public key/username identity")
id_balance_cmd = subparsers.add_parser( id_balance_cmd = subparsers.add_parser(
"idBalance", help="Voir l'identité d'une clé publique/username et son solde" "idBalance", help="View public key/username identity and balance"
) )
currentUd = subparsers.add_parser( currentUd = subparsers.add_parser(
"currentUd", help="Affiche la montant actuel du dividende Universel" "currentUd", help="Display the current Universal Dividend amount"
)
listWallets = subparsers.add_parser(
"listWallets", help="Liste de toutes les portefeuilles G1"
) )
listWallets = subparsers.add_parser("listWallets", help="List all G1 wallets")
geolocProfiles = subparsers.add_parser( geolocProfiles = subparsers.add_parser(
"geolocProfiles", help="Obtenir le JSON de tous les comptes géolocalisés" "geolocProfiles", help="Get JSON of all geolocated accounts"
) )
# Messages management # Messaging management commands
read_cmd.add_argument( read_cmd.add_argument(
"-n", "--number", type=int, default=3, help="Affiche les NUMBER derniers messages" "-n", "--number", type=int, default=3, help="Display the last NUMBER messages"
)
read_cmd.add_argument("-j", "--json", action="store_true", help="Sort au format JSON")
read_cmd.add_argument(
"-o", "--outbox", action="store_true", help="Lit les messages envoyés"
) )
read_cmd.add_argument("-j", "--json", action="store_true", help="Output in JSON format")
read_cmd.add_argument("-o", "--outbox", action="store_true", help="Read sent messages")
send_cmd.add_argument( send_cmd.add_argument(
"-d", "--destinataire", required=True, help="Destinataire du message" "-d", "--destinataire", required=True, help="Recipient of the message"
) )
send_cmd.add_argument("-t", "--titre", help="Titre du message à envoyer") send_cmd.add_argument("-t", "--titre", help="Title of the message to send")
send_cmd.add_argument("-m", "--message", help="Message à envoyer") send_cmd.add_argument("-m", "--message", help="Message to send")
send_cmd.add_argument("-f", "--fichier", help="Send the message from the 'FILE'")
send_cmd.add_argument( send_cmd.add_argument(
"-f", "--fichier", help="Envoyer le message contenu dans le fichier 'FICHIER'" "-o", "--outbox", action="store_true", help="Send the message to the outbox"
)
send_cmd.add_argument(
"-o", "--outbox", action="store_true", help="Envoi le message sur la boite d'envoi"
) )
delete_cmd.add_argument( delete_cmd.add_argument(
@ -108,119 +103,117 @@ delete_cmd.add_argument(
action="append", action="append",
nargs="+", nargs="+",
required=True, required=True,
help="ID(s) du/des message(s) à supprimer", help="ID(s) of the message(s) to delete",
) )
delete_cmd.add_argument( delete_cmd.add_argument(
"-o", "--outbox", action="store_true", help="Suppression d'un message envoyé" "-o", "--outbox", action="store_true", help="Delete a sent message"
) )
# Profiles management # Profile management commands
setProfile_cmd.add_argument("-n", "--name", help="Nom du profile") setProfile_cmd.add_argument("-n", "--name", help="Profile name")
setProfile_cmd.add_argument("-d", "--description", help="Description du profile") setProfile_cmd.add_argument("-d", "--description", help="Profile description")
setProfile_cmd.add_argument("-v", "--ville", help="Ville du profile") setProfile_cmd.add_argument("-v", "--ville", help="Profile city")
setProfile_cmd.add_argument("-a", "--adresse", help="Adresse du profile") setProfile_cmd.add_argument("-a", "--adresse", help="Profile address")
setProfile_cmd.add_argument( setProfile_cmd.add_argument(
"-pos", "--position", nargs=2, help="Points géographiques (lat + lon)" "-pos", "--position", nargs=2, help="Geographical coordinates (lat + lon)"
) )
setProfile_cmd.add_argument("-s", "--site", help="Site web du profile") setProfile_cmd.add_argument("-s", "--site", help="Profile website")
setProfile_cmd.add_argument("-A", "--avatar", help="Chemin vers mon avatar en PNG") setProfile_cmd.add_argument("-A", "--avatar", help="Path to profile avatar in PNG")
getProfile_cmd.add_argument("-p", "--profile", help="Nom du profile") getProfile_cmd.add_argument("-p", "--profile", help="Profile name")
getProfile_cmd.add_argument( getProfile_cmd.add_argument(
"-a", "-a",
"--avatar", "--avatar",
action="store_true", action="store_true",
help="Récupérer également l'avatar au format raw base64", help="Also retrieve the avatar in raw base64 format",
) )
getPage_cmd.add_argument("-p", "--page", help="Nom de la page") getPage_cmd.add_argument("-p", "--page", help="Page name")
getPage_cmd.add_argument( getPage_cmd.add_argument(
"-a", "-a",
"--avatar", "--avatar",
action="store_true", action="store_true",
help="Récupérer également l'avatar au format raw base64", help="Also retrieve the page's avatar in raw base64 format",
) )
# Likes management # Likes management commands
stars_cmd.add_argument("-p", "--profile", help="Profile cible") stars_cmd.add_argument("-p", "--profile", help="Target profile")
stars_cmd.add_argument("-n", "--number", type=int, help="Nombre d'étoile") stars_cmd.add_argument("-n", "--number", type=int, help="Number of stars")
unstars_cmd.add_argument("-p", "--profile", help="Profile à dénoter") unstars_cmd.add_argument("-p", "--profile", help="Profile to unstar")
# Offers management # Offers management commands
getoffer_cmd.add_argument("-i", "--id", help="Annonce cible à récupérer") getoffer_cmd.add_argument("-i", "--id", help="Target listing to retrieve")
setoffer_cmd.add_argument("-t", "--title", help="Titre de l'annonce à créer") setoffer_cmd.add_argument("-t", "--title", help="Title of the listing to create")
setoffer_cmd.add_argument( setoffer_cmd.add_argument(
"-d", "--description", help="Description de l'annonce à créer" "-d", "--description", help="Description of the listing to create"
) )
setoffer_cmd.add_argument("-c", "--category", help="Categorie de l'annonce à créer") setoffer_cmd.add_argument("-c", "--category", help="Category of the listing to create")
setoffer_cmd.add_argument( setoffer_cmd.add_argument(
"-l", "-l",
"--localisation", "--localisation",
nargs=2, nargs=2,
help="Localisation de l'annonce à créer (lat + lon)", help="Location of the listing to create (lat + lon)",
) )
setoffer_cmd.add_argument("-p", "--picture", help="Image de l'annonce à créer") setoffer_cmd.add_argument("-p", "--picture", help="Image of the listing to create")
setoffer_cmd.add_argument("-ci", "--city", help="Ville de l'annonce à créer") setoffer_cmd.add_argument("-ci", "--city", help="City of the listing to create")
setoffer_cmd.add_argument("-pr", "--price", help="Prix de l'annonce à créer") setoffer_cmd.add_argument("-pr", "--price", help="Price of the listing to create")
deleteoffer_cmd.add_argument("-i", "--id", help="Annonce cible à supprimer") deleteoffer_cmd.add_argument("-i", "--id", help="Target listing to delete")
# GVA usage # GVA usage commands
pay_cmd.add_argument("-p", "--pubkey", help="Destinataire du paiement") pay_cmd.add_argument("-p", "--pubkey", help="Payment recipient")
pay_cmd.add_argument("-a", "--amount", type=float, help="Montant de la transaction") pay_cmd.add_argument("-a", "--amount", type=float, help="Transaction amount")
pay_cmd.add_argument( pay_cmd.add_argument(
"-c", "--comment", default="", help="Commentaire de la transaction", nargs="*" "-c", "--comment", default="", help="Transaction comment", nargs="*"
)
pay_cmd.add_argument(
"-m", "--mempool", action="store_true", help="Utilise les sources en Mempool"
) )
pay_cmd.add_argument("-m", "--mempool", action="store_true", help="Use mempool sources")
pay_cmd.add_argument( pay_cmd.add_argument(
"-v", "-v",
"--verbose", "--verbose",
action="store_true", action="store_true",
help="Affiche le résultat JSON de la transaction", help="Display the JSON result of the transaction",
) )
history_cmd.add_argument("-p", "--pubkey", help="Clé publique du compte visé") history_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
history_cmd.add_argument( history_cmd.add_argument(
"-n", "-n",
"--number", "--number",
type=int, type=int,
default=10, default=10,
help="Affiche les NUMBER dernières transactions", help="Display the last NUMBER transactions",
) )
history_cmd.add_argument( history_cmd.add_argument(
"-j", "--json", action="store_true", help="Affiche le résultat en format JSON" "-j", "--json", action="store_true", help="Display the result in JSON format"
) )
history_cmd.add_argument( history_cmd.add_argument(
"--nocolors", action="store_true", help="Affiche le résultat en noir et blanc" "--nocolors", action="store_true", help="Display the result in black and white"
) )
balance_cmd.add_argument("-p", "--pubkey", help="Clé publique du compte visé") balance_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
balance_cmd.add_argument( balance_cmd.add_argument(
"-m", "--mempool", action="store_true", help="Utilise les sources en Mempool" "-m", "--mempool", action="store_true", help="Use mempool sources"
) )
id_cmd.add_argument("-p", "--pubkey", help="Clé publique du compte visé") id_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
id_cmd.add_argument("-u", "--username", help="Username du compte visé") id_cmd.add_argument("-u", "--username", help="Username of the target account")
id_balance_cmd.add_argument("-p", "--pubkey", help="Pubkey du compte visé") id_balance_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
currentUd.add_argument("-p", "--pubkey", help="Pubkey du compte visé") currentUd.add_argument("-p", "--pubkey", help="Public key of the target account")
listWallets.add_argument( listWallets.add_argument(
"-b", "--balance", action="store_true", help="Affiche les soldes" "-b", "--balance", action="store_true", help="Display balances"
) )
listWallets.add_argument( listWallets.add_argument(
"--mbr", action="store_true", help="Affiche la liste de pubkey membres brut" "--mbr", action="store_true", help="Display raw list of member pubkeys"
) )
listWallets.add_argument( listWallets.add_argument(
"--non_mbr", "--non_mbr",
action="store_true", action="store_true",
help="Affiche la liste de pubkey des identités non membres brut", help="Display raw list of non-member identity pubkeys",
) )
listWallets.add_argument( listWallets.add_argument(
"--larf", action="store_true", help="Affiche la liste des pubkey non membres brut" "--larf", action="store_true", help="Display raw list of non-member pubkeys"
) )
listWallets.add_argument( listWallets.add_argument(
"--brut", action="store_true", help="Affiche la liste de toutes les pubkey brut" "--brut", action="store_true", help="Display raw list of all pubkeys"
) )
listWallets.add_argument("-p", "--pubkey", help="useless but needed") listWallets.add_argument("-p", "--pubkey", help="Useless but needed")
args = parser.parse_args() args = parser.parse_args()
cmd = args.cmd cmd = args.cmd
@ -235,14 +228,13 @@ if not cmd:
def createTmpDunikey(): def createTmpDunikey():
# Generate pseudo-random nonce # Generate a pseudo-random nonce
nonce = [] nonce = "".join(
for _ in range(32): random.choice(string.ascii_letters + string.digits) for _ in range(32)
nonce.append(random.choice(string.ascii_letters + string.digits)) )
nonce = "".join(nonce)
keyPath = "/tmp/secret.dunikey-" + nonce keyPath = "/tmp/secret.dunikey-" + nonce
# key = SigningKey.from_credentials(getpass.getpass("Identifiant: "), getpass.getpass("Mot de passe: "), None) # Create a dummy key (replace with actual key creation logic)
key = SigningKey.from_credentials( key = SigningKey.from_credentials(
"sgse547yhd54xv6541srdh", "sfdgwdrhpkxdawsbszqpof1sdg65xc", None "sgse547yhd54xv6541srdh", "sfdgwdrhpkxdawsbszqpof1sdg65xc", None
) )
@ -251,7 +243,7 @@ def createTmpDunikey():
return keyPath return keyPath
# Check if we need dunikey # Check if a dunikey is needed
try: try:
pubkey = args.pubkey pubkey = args.pubkey
except: except:
@ -261,10 +253,15 @@ try:
except: except:
profile = False profile = False
# print(pubkey, profile) if cmd in (
if cmd in ("history", "balance", "get", "page", "id", "idBalance", "listWallets") and ( "history",
pubkey or profile "balance",
): "get",
"page",
"id",
"idBalance",
"listWallets",
) and (pubkey or profile):
noNeedDunikey = True noNeedDunikey = True
keyPath = False keyPath = False
try: try:
@ -287,13 +284,10 @@ else:
HOME = os.getenv("HOME") HOME = os.getenv("HOME")
dunikey = HOME + dunikey dunikey = HOME + dunikey
if not os.path.isfile(dunikey): if not os.path.isfile(dunikey):
sys.stderr.write( sys.stderr.write("The keyfile {0} is not found.\n".format(dunikey))
"Le fichier de trousseau {0} est introuvable.\n".format(dunikey)
)
sys.exit(1) sys.exit(1)
# Construct the CesiumPlus object
# Construct CesiumPlus object
if cmd in ( if cmd in (
"read", "read",
"send", "send",
@ -332,8 +326,8 @@ if cmd in (
titre = args.titre titre = args.titre
msg = args.message msg = args.message
else: else:
titre = input("Indiquez le titre du message: ") titre = input("Enter the message title: ")
msg = input("Indiquez le contenu du message: ") msg = input("Enter the message content: ")
cesium.send(titre, msg, args.destinataire, args.outbox) cesium.send(titre, msg, args.destinataire, args.outbox)
@ -385,7 +379,7 @@ if cmd in (
elif cmd == "deleteoffer": elif cmd == "deleteoffer":
cesium.deleteOffer(args.id) cesium.deleteOffer(args.id)
# Construct GVA object # Construct the GvaApi object
elif cmd in ( elif cmd in (
"pay", "pay",
"history", "history",
@ -420,6 +414,5 @@ elif cmd in (
elif cmd == "listWallets": elif cmd == "listWallets":
gva.listWallets(args.brut, args.mbr, args.non_mbr, args.larf) gva.listWallets(args.brut, args.mbr, args.non_mbr, args.larf)
if keyPath: if keyPath:
os.remove(keyPath) os.remove(keyPath)