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
import argparse, sys, os, string, random
import argparse
import sys
import os
import string
import random
from os.path import join, dirname
from shutil import copyfile
from dotenv import load_dotenv
@ -10,96 +14,87 @@ __version__ = "0.0.5"
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"):
copyfile(MY_PATH + ".env.template", MY_PATH + ".env")
dotenv_path = join(dirname(__file__), MY_PATH + ".env")
load_dotenv(dotenv_path)
# Set global values (default parameters) , regarding variables environments
node = os.getenv("DUNITER") + "/gva"
if not node:
node = "https://g1v1.p2p.legal/gva"
pod = os.getenv("ESNODE")
if not pod:
pod = "https://g1.data.e-is.pro"
# Set global values (default parameters) regarding environment variables
node = (
os.getenv("DUNITER") + "/gva"
if os.getenv("DUNITER")
else "https://g1v1.p2p.legal/gva"
)
pod = os.getenv("ESNODE") if os.getenv("ESNODE") else "https://g1.data.e-is.pro"
destPubkey = False
# Parse arguments
parser = argparse.ArgumentParser(
description="Client CLI pour Cesium+ et Ḡchange",
description="CLI Client for Cesium+ and Ḡchange",
epilog="current node: '" + node + "', current pod: '" + pod + "'.",
)
parser.add_argument(
"-v",
"--version",
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(
"-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")
read_cmd = subparsers.add_parser("read", help="Lecture des messages")
send_cmd = subparsers.add_parser("send", help="Envoi d'un message")
delete_cmd = subparsers.add_parser("delete", help="Supression d'un message")
getProfile_cmd = subparsers.add_parser("get", help="Voir un profile Cesium+")
getPage_cmd = subparsers.add_parser("page", help="Voir une page Cesium+")
setProfile_cmd = subparsers.add_parser("set", help="Configurer son profile Cesium+")
eraseProfile_cmd = subparsers.add_parser("erase", help="Effacer son profile Cesium+")
# Create subparsers for different commands
subparsers = parser.add_subparsers(title="jaklis Commands", dest="cmd")
read_cmd = subparsers.add_parser("read", help="Read messages")
send_cmd = subparsers.add_parser("send", help="Send a message")
delete_cmd = subparsers.add_parser("delete", help="Delete a message")
getProfile_cmd = subparsers.add_parser("get", help="View a Cesium+ profile")
getPage_cmd = subparsers.add_parser("page", help="View a Cesium+ page")
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", 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", 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")
deleteoffer_cmd = subparsers.add_parser(
"deleteoffer", help="Supprimer une annonce gchange"
)
pay_cmd = subparsers.add_parser("pay", help="Payer en Ḡ1")
setoffer_cmd = subparsers.add_parser("setoffer", help="Create a Ḡchange listing")
deleteoffer_cmd = subparsers.add_parser("deleteoffer", help="Delete a Ḡchange listing")
pay_cmd = subparsers.add_parser("pay", help="Pay in Ḡ1")
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")
id_cmd = subparsers.add_parser("id", help="Voir l'identité d'une clé publique/username")
balance_cmd = subparsers.add_parser("balance", help="View Ḡ1 account balance")
id_cmd = subparsers.add_parser("id", help="View public key/username identity")
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", help="Affiche la montant actuel du dividende Universel"
)
listWallets = subparsers.add_parser(
"listWallets", help="Liste de toutes les portefeuilles G1"
"currentUd", help="Display the current Universal Dividend amount"
)
listWallets = subparsers.add_parser("listWallets", help="List all G1 wallets")
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(
"-n", "--number", type=int, default=3, help="Affiche les NUMBER derniers 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"
"-n", "--number", type=int, default=3, help="Display the last NUMBER messages"
)
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(
"-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("-m", "--message", help="Message à envoyer")
send_cmd.add_argument("-t", "--titre", help="Title of the message to send")
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(
"-f", "--fichier", help="Envoyer le message contenu dans le fichier 'FICHIER'"
)
send_cmd.add_argument(
"-o", "--outbox", action="store_true", help="Envoi le message sur la boite d'envoi"
"-o", "--outbox", action="store_true", help="Send the message to the outbox"
)
delete_cmd.add_argument(
@ -108,119 +103,117 @@ delete_cmd.add_argument(
action="append",
nargs="+",
required=True,
help="ID(s) du/des message(s) à supprimer",
help="ID(s) of the message(s) to delete",
)
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
setProfile_cmd.add_argument("-n", "--name", help="Nom du profile")
setProfile_cmd.add_argument("-d", "--description", help="Description du profile")
setProfile_cmd.add_argument("-v", "--ville", help="Ville du profile")
setProfile_cmd.add_argument("-a", "--adresse", help="Adresse du profile")
# Profile management commands
setProfile_cmd.add_argument("-n", "--name", help="Profile name")
setProfile_cmd.add_argument("-d", "--description", help="Profile description")
setProfile_cmd.add_argument("-v", "--ville", help="Profile city")
setProfile_cmd.add_argument("-a", "--adresse", help="Profile address")
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("-A", "--avatar", help="Chemin vers mon avatar en PNG")
setProfile_cmd.add_argument("-s", "--site", help="Profile website")
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(
"-a",
"--avatar",
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(
"-a",
"--avatar",
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
stars_cmd.add_argument("-p", "--profile", help="Profile cible")
stars_cmd.add_argument("-n", "--number", type=int, help="Nombre d'étoile")
unstars_cmd.add_argument("-p", "--profile", help="Profile à dénoter")
# Likes management commands
stars_cmd.add_argument("-p", "--profile", help="Target profile")
stars_cmd.add_argument("-n", "--number", type=int, help="Number of stars")
unstars_cmd.add_argument("-p", "--profile", help="Profile to unstar")
# Offers management
getoffer_cmd.add_argument("-i", "--id", help="Annonce cible à récupérer")
setoffer_cmd.add_argument("-t", "--title", help="Titre de l'annonce à créer")
# Offers management commands
getoffer_cmd.add_argument("-i", "--id", help="Target listing to retrieve")
setoffer_cmd.add_argument("-t", "--title", help="Title of the listing to create")
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(
"-l",
"--localisation",
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("-ci", "--city", help="Ville de l'annonce à créer")
setoffer_cmd.add_argument("-pr", "--price", help="Prix de l'annonce à créer")
deleteoffer_cmd.add_argument("-i", "--id", help="Annonce cible à supprimer")
setoffer_cmd.add_argument("-p", "--picture", help="Image of the listing to create")
setoffer_cmd.add_argument("-ci", "--city", help="City of the listing to create")
setoffer_cmd.add_argument("-pr", "--price", help="Price of the listing to create")
deleteoffer_cmd.add_argument("-i", "--id", help="Target listing to delete")
# GVA usage
pay_cmd.add_argument("-p", "--pubkey", help="Destinataire du paiement")
pay_cmd.add_argument("-a", "--amount", type=float, help="Montant de la transaction")
# GVA usage commands
pay_cmd.add_argument("-p", "--pubkey", help="Payment recipient")
pay_cmd.add_argument("-a", "--amount", type=float, help="Transaction amount")
pay_cmd.add_argument(
"-c", "--comment", default="", help="Commentaire de la transaction", nargs="*"
)
pay_cmd.add_argument(
"-m", "--mempool", action="store_true", help="Utilise les sources en Mempool"
"-c", "--comment", default="", help="Transaction comment", nargs="*"
)
pay_cmd.add_argument("-m", "--mempool", action="store_true", help="Use mempool sources")
pay_cmd.add_argument(
"-v",
"--verbose",
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(
"-n",
"--number",
type=int,
default=10,
help="Affiche les NUMBER dernières transactions",
help="Display the last NUMBER transactions",
)
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(
"--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(
"-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("-u", "--username", help="Username du compte visé")
id_balance_cmd.add_argument("-p", "--pubkey", help="Pubkey du compte visé")
currentUd.add_argument("-p", "--pubkey", help="Pubkey du compte visé")
id_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
id_cmd.add_argument("-u", "--username", help="Username of the target account")
id_balance_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
currentUd.add_argument("-p", "--pubkey", help="Public key of the target account")
listWallets.add_argument(
"-b", "--balance", action="store_true", help="Affiche les soldes"
"-b", "--balance", action="store_true", help="Display balances"
)
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(
"--non_mbr",
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(
"--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(
"--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()
cmd = args.cmd
@ -235,14 +228,13 @@ if not cmd:
def createTmpDunikey():
# Generate pseudo-random nonce
nonce = []
for _ in range(32):
nonce.append(random.choice(string.ascii_letters + string.digits))
nonce = "".join(nonce)
# Generate a pseudo-random nonce
nonce = "".join(
random.choice(string.ascii_letters + string.digits) for _ in range(32)
)
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(
"sgse547yhd54xv6541srdh", "sfdgwdrhpkxdawsbszqpof1sdg65xc", None
)
@ -251,7 +243,7 @@ def createTmpDunikey():
return keyPath
# Check if we need dunikey
# Check if a dunikey is needed
try:
pubkey = args.pubkey
except:
@ -261,10 +253,15 @@ try:
except:
profile = False
# print(pubkey, profile)
if cmd in ("history", "balance", "get", "page", "id", "idBalance", "listWallets") and (
pubkey or profile
):
if cmd in (
"history",
"balance",
"get",
"page",
"id",
"idBalance",
"listWallets",
) and (pubkey or profile):
noNeedDunikey = True
keyPath = False
try:
@ -287,13 +284,10 @@ else:
HOME = os.getenv("HOME")
dunikey = HOME + dunikey
if not os.path.isfile(dunikey):
sys.stderr.write(
"Le fichier de trousseau {0} est introuvable.\n".format(dunikey)
)
sys.stderr.write("The keyfile {0} is not found.\n".format(dunikey))
sys.exit(1)
# Construct CesiumPlus object
# Construct the CesiumPlus object
if cmd in (
"read",
"send",
@ -332,8 +326,8 @@ if cmd in (
titre = args.titre
msg = args.message
else:
titre = input("Indiquez le titre du message: ")
msg = input("Indiquez le contenu du message: ")
titre = input("Enter the message title: ")
msg = input("Enter the message content: ")
cesium.send(titre, msg, args.destinataire, args.outbox)
@ -385,7 +379,7 @@ if cmd in (
elif cmd == "deleteoffer":
cesium.deleteOffer(args.id)
# Construct GVA object
# Construct the GvaApi object
elif cmd in (
"pay",
"history",
@ -420,6 +414,5 @@ elif cmd in (
elif cmd == "listWallets":
gva.listWallets(args.brut, args.mbr, args.non_mbr, args.larf)
if keyPath:
os.remove(keyPath)