From 8e5a82f89a9bb9046d48edd2208dc37088b7a942 Mon Sep 17 00:00:00 2001 From: Sergio Date: Sat, 30 Dec 2023 18:56:26 +0100 Subject: [PATCH] Cambiado getopt por argparse --- python/fbneo_roms_by_manufacturer.py | 111 +++++++++++---------------- 1 file changed, 45 insertions(+), 66 deletions(-) diff --git a/python/fbneo_roms_by_manufacturer.py b/python/fbneo_roms_by_manufacturer.py index 4fbb4e6..fa5132a 100644 --- a/python/fbneo_roms_by_manufacturer.py +++ b/python/fbneo_roms_by_manufacturer.py @@ -7,58 +7,37 @@ import os import shutil import sys -import getopt +import argparse from xml.dom import minidom # Inicialización de las opciones -opt_manufacturer = "" -opt_list = "no" -opt_dat = "" -opt_src_roms = "" -opt_dst_roms = "" -opt_copy = "no" -opt_sort = "no" ignore_list = ["DECO Cassette"] -# Comprueba los parametros -try: - opts, args = getopt.getopt( - sys.argv[1:], - "hlm:d:ci:o:s", - ["help", "list", "manufacturer=", "dat=", "copy", "input=", "output=", "sort"], - ) +parser = argparse.ArgumentParser() +parser.add_argument("-l", "--list", help="Muestra la lista de desarrolladores o de juegos", action="store_true") +parser.add_argument("-c", "--copy", help="Copia las roms", action="store_true") +parser.add_argument("-s", "--sort", help="Ordena los resultados", action="store_true") -except getopt.GetoptError: - print("test.py -i -o ") - sys.exit(2) - -for opt, arg in opts: - if opt in ("-h", "--help"): - print("test.py -i -o ") - sys.exit() - elif opt in ("-l", "--list"): - opt_list = "yes" - elif opt in ("-m", "--manufacturer"): - opt_manufacturer = arg - elif opt in ("-d", "--dat"): - opt_dat = arg - elif opt in ("-c", "--copy"): - opt_copy = "yes" - elif opt in ("-i", "--input"): - opt_src_roms = arg - elif opt in ("-o", "--output"): - opt_dst_roms = arg - elif opt in ("-s", "--sort"): - opt_sort = "yes" +parser.add_argument("-m", "--manufacturer", help="Selecciona un desarrollador") +parser.add_argument("-d", "--dat", help="Ruta del fichero .dat con información de las roms") +parser.add_argument("-i", "--input", help="Ruta donde se encuentran las roms", default="") +parser.add_argument("-o", "--output", help="Ruta donde depositar las roms", default="") +args = parser.parse_args() # Importa el xml -if opt_dat == "" or not os.path.isfile(opt_dat): +if args.dat == None: + print("No se ha especificado un fichero .dat") sys.exit(2) -print("Parsing {} file".format(opt_dat)) -file = minidom.parse(opt_dat) + +if not os.path.isfile(args.dat): + print("No se encuentra el fichero .dat") + sys.exit(2) + +print("Parsing {} file".format(args.dat)) +file = minidom.parse(args.dat) # Lista los desarrolladores -if opt_list == "yes" and opt_manufacturer == "": +if args.list and args.manufacturer == None: print("List of all manufacturers:") manufacturers = [] games = file.getElementsByTagName("game") @@ -74,8 +53,8 @@ if opt_list == "yes" and opt_manufacturer == "": print(i) # Lista los juegos de un desarrollador -if opt_list == "yes" and opt_manufacturer != "": - print("List of all {} games".format(opt_manufacturer)) +if args.list and args.manufacturer != None: + print("List of all {} games".format(args.manufacturer)) games = file.getElementsByTagName("game") for game in games: cloneof = game.getAttribute("cloneof") @@ -83,7 +62,7 @@ if opt_list == "yes" and opt_manufacturer != "": manufacturer = game.getElementsByTagName("manufacturer")[0] description = game.getElementsByTagName("description")[0] if ( - manufacturer.firstChild.data == opt_manufacturer + manufacturer.firstChild.data == args.manufacturer and not cloneof and not isbios ): @@ -91,12 +70,12 @@ if opt_list == "yes" and opt_manufacturer != "": # Copia los juegos seleccionados if ( - opt_copy == "yes" - and os.path.isdir(opt_src_roms) - and os.path.isdir(opt_dst_roms) - and opt_manufacturer != "" + args.copy + and os.path.isdir(args.input) + and os.path.isdir(args.output) + and args.manufacturer != None ): - print("Copying all {} games".format(opt_manufacturer)) + print("Copying all {} games".format(args.manufacturer)) notfound = [] ignored_games = [] games = file.getElementsByTagName("game") @@ -108,7 +87,7 @@ if ( manufacturer = game.getElementsByTagName("manufacturer")[0] description = game.getElementsByTagName("description")[0] if ( - manufacturer.firstChild.data == opt_manufacturer + manufacturer.firstChild.data == args.manufacturer and not cloneof and not isbios ): @@ -117,15 +96,15 @@ if ( ignored_games.append(description.firstChild.data) isignored = True if not isignored: - src = os.path.join(opt_src_roms, name) - if opt_sort == "yes": + src = os.path.join(args.input, name) + if args.sort: x = manufacturer.firstChild.data x = x.replace(r"/", r"-") - dst = os.path.join(opt_dst_roms, x, name) - if not os.path.isdir(os.path.join(opt_dst_roms, x)): - os.mkdir(os.path.join(opt_dst_roms, x)) + dst = os.path.join(args.output, x, name) + if not os.path.isdir(os.path.join(args.output, x)): + os.mkdir(os.path.join(args.output, x)) else: - dst = os.path.join(opt_dst_roms, name) + dst = os.path.join(args.output, name) if os.path.isfile(src): shutil.copyfile(src, dst) print("%s" % (description.firstChild.data)) @@ -141,10 +120,10 @@ if ( # Copia todos los juegos if ( - opt_copy == "yes" - and os.path.isdir(opt_src_roms) - and os.path.isdir(opt_dst_roms) - and opt_manufacturer == "" + args.copy + and os.path.isdir(args.input) + and os.path.isdir(args.output) + and args.manufacturer == None ): print("Copying all games") notfound = [] @@ -156,15 +135,15 @@ if ( manufacturer = game.getElementsByTagName("manufacturer")[0] description = game.getElementsByTagName("description")[0] if not cloneof and not isbios: - src = os.path.join(opt_src_roms, name) - if opt_sort == "yes": + src = os.path.join(args.input, name) + if args.sort: x = manufacturer.firstChild.data x = x.replace(r"/", r"-") - dst = os.path.join(opt_dst_roms, x, name) - if not os.path.isdir(os.path.join(opt_dst_roms, x)): - os.mkdir(os.path.join(opt_dst_roms, x)) + dst = os.path.join(args.output, x, name) + if not os.path.isdir(os.path.join(args.output, x)): + os.mkdir(os.path.join(args.output, x)) else: - dst = os.path.join(opt_dst_roms, name) + dst = os.path.join(args.output, name) if os.path.isfile(src): shutil.copyfile(src, dst) print("%s" % (description.firstChild.data))