Cambiado getopt por argparse
This commit is contained in:
@@ -7,58 +7,37 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import getopt
|
import argparse
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
|
|
||||||
# Inicialización de las opciones
|
# 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"]
|
ignore_list = ["DECO Cassette"]
|
||||||
|
|
||||||
# Comprueba los parametros
|
parser = argparse.ArgumentParser()
|
||||||
try:
|
parser.add_argument("-l", "--list", help="Muestra la lista de desarrolladores o de juegos", action="store_true")
|
||||||
opts, args = getopt.getopt(
|
parser.add_argument("-c", "--copy", help="Copia las roms", action="store_true")
|
||||||
sys.argv[1:],
|
parser.add_argument("-s", "--sort", help="Ordena los resultados", action="store_true")
|
||||||
"hlm:d:ci:o:s",
|
|
||||||
["help", "list", "manufacturer=", "dat=", "copy", "input=", "output=", "sort"],
|
|
||||||
)
|
|
||||||
|
|
||||||
except getopt.GetoptError:
|
parser.add_argument("-m", "--manufacturer", help="Selecciona un desarrollador")
|
||||||
print("test.py -i <inputfile> -o <outputfile>")
|
parser.add_argument("-d", "--dat", help="Ruta del fichero .dat con información de las roms")
|
||||||
sys.exit(2)
|
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="")
|
||||||
for opt, arg in opts:
|
args = parser.parse_args()
|
||||||
if opt in ("-h", "--help"):
|
|
||||||
print("test.py -i <inputfile> -o <outputfile>")
|
|
||||||
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"
|
|
||||||
|
|
||||||
# Importa el xml
|
# 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)
|
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
|
# Lista los desarrolladores
|
||||||
if opt_list == "yes" and opt_manufacturer == "":
|
if args.list and args.manufacturer == None:
|
||||||
print("List of all manufacturers:")
|
print("List of all manufacturers:")
|
||||||
manufacturers = []
|
manufacturers = []
|
||||||
games = file.getElementsByTagName("game")
|
games = file.getElementsByTagName("game")
|
||||||
@@ -74,8 +53,8 @@ if opt_list == "yes" and opt_manufacturer == "":
|
|||||||
print(i)
|
print(i)
|
||||||
|
|
||||||
# Lista los juegos de un desarrollador
|
# Lista los juegos de un desarrollador
|
||||||
if opt_list == "yes" and opt_manufacturer != "":
|
if args.list and args.manufacturer != None:
|
||||||
print("List of all {} games".format(opt_manufacturer))
|
print("List of all {} games".format(args.manufacturer))
|
||||||
games = file.getElementsByTagName("game")
|
games = file.getElementsByTagName("game")
|
||||||
for game in games:
|
for game in games:
|
||||||
cloneof = game.getAttribute("cloneof")
|
cloneof = game.getAttribute("cloneof")
|
||||||
@@ -83,7 +62,7 @@ if opt_list == "yes" and opt_manufacturer != "":
|
|||||||
manufacturer = game.getElementsByTagName("manufacturer")[0]
|
manufacturer = game.getElementsByTagName("manufacturer")[0]
|
||||||
description = game.getElementsByTagName("description")[0]
|
description = game.getElementsByTagName("description")[0]
|
||||||
if (
|
if (
|
||||||
manufacturer.firstChild.data == opt_manufacturer
|
manufacturer.firstChild.data == args.manufacturer
|
||||||
and not cloneof
|
and not cloneof
|
||||||
and not isbios
|
and not isbios
|
||||||
):
|
):
|
||||||
@@ -91,12 +70,12 @@ if opt_list == "yes" and opt_manufacturer != "":
|
|||||||
|
|
||||||
# Copia los juegos seleccionados
|
# Copia los juegos seleccionados
|
||||||
if (
|
if (
|
||||||
opt_copy == "yes"
|
args.copy
|
||||||
and os.path.isdir(opt_src_roms)
|
and os.path.isdir(args.input)
|
||||||
and os.path.isdir(opt_dst_roms)
|
and os.path.isdir(args.output)
|
||||||
and opt_manufacturer != ""
|
and args.manufacturer != None
|
||||||
):
|
):
|
||||||
print("Copying all {} games".format(opt_manufacturer))
|
print("Copying all {} games".format(args.manufacturer))
|
||||||
notfound = []
|
notfound = []
|
||||||
ignored_games = []
|
ignored_games = []
|
||||||
games = file.getElementsByTagName("game")
|
games = file.getElementsByTagName("game")
|
||||||
@@ -108,7 +87,7 @@ if (
|
|||||||
manufacturer = game.getElementsByTagName("manufacturer")[0]
|
manufacturer = game.getElementsByTagName("manufacturer")[0]
|
||||||
description = game.getElementsByTagName("description")[0]
|
description = game.getElementsByTagName("description")[0]
|
||||||
if (
|
if (
|
||||||
manufacturer.firstChild.data == opt_manufacturer
|
manufacturer.firstChild.data == args.manufacturer
|
||||||
and not cloneof
|
and not cloneof
|
||||||
and not isbios
|
and not isbios
|
||||||
):
|
):
|
||||||
@@ -117,15 +96,15 @@ if (
|
|||||||
ignored_games.append(description.firstChild.data)
|
ignored_games.append(description.firstChild.data)
|
||||||
isignored = True
|
isignored = True
|
||||||
if not isignored:
|
if not isignored:
|
||||||
src = os.path.join(opt_src_roms, name)
|
src = os.path.join(args.input, name)
|
||||||
if opt_sort == "yes":
|
if args.sort:
|
||||||
x = manufacturer.firstChild.data
|
x = manufacturer.firstChild.data
|
||||||
x = x.replace(r"/", r"-")
|
x = x.replace(r"/", r"-")
|
||||||
dst = os.path.join(opt_dst_roms, x, name)
|
dst = os.path.join(args.output, x, name)
|
||||||
if not os.path.isdir(os.path.join(opt_dst_roms, x)):
|
if not os.path.isdir(os.path.join(args.output, x)):
|
||||||
os.mkdir(os.path.join(opt_dst_roms, x))
|
os.mkdir(os.path.join(args.output, x))
|
||||||
else:
|
else:
|
||||||
dst = os.path.join(opt_dst_roms, name)
|
dst = os.path.join(args.output, name)
|
||||||
if os.path.isfile(src):
|
if os.path.isfile(src):
|
||||||
shutil.copyfile(src, dst)
|
shutil.copyfile(src, dst)
|
||||||
print("%s" % (description.firstChild.data))
|
print("%s" % (description.firstChild.data))
|
||||||
@@ -141,10 +120,10 @@ if (
|
|||||||
|
|
||||||
# Copia todos los juegos
|
# Copia todos los juegos
|
||||||
if (
|
if (
|
||||||
opt_copy == "yes"
|
args.copy
|
||||||
and os.path.isdir(opt_src_roms)
|
and os.path.isdir(args.input)
|
||||||
and os.path.isdir(opt_dst_roms)
|
and os.path.isdir(args.output)
|
||||||
and opt_manufacturer == ""
|
and args.manufacturer == None
|
||||||
):
|
):
|
||||||
print("Copying all games")
|
print("Copying all games")
|
||||||
notfound = []
|
notfound = []
|
||||||
@@ -156,15 +135,15 @@ if (
|
|||||||
manufacturer = game.getElementsByTagName("manufacturer")[0]
|
manufacturer = game.getElementsByTagName("manufacturer")[0]
|
||||||
description = game.getElementsByTagName("description")[0]
|
description = game.getElementsByTagName("description")[0]
|
||||||
if not cloneof and not isbios:
|
if not cloneof and not isbios:
|
||||||
src = os.path.join(opt_src_roms, name)
|
src = os.path.join(args.input, name)
|
||||||
if opt_sort == "yes":
|
if args.sort:
|
||||||
x = manufacturer.firstChild.data
|
x = manufacturer.firstChild.data
|
||||||
x = x.replace(r"/", r"-")
|
x = x.replace(r"/", r"-")
|
||||||
dst = os.path.join(opt_dst_roms, x, name)
|
dst = os.path.join(args.output, x, name)
|
||||||
if not os.path.isdir(os.path.join(opt_dst_roms, x)):
|
if not os.path.isdir(os.path.join(args.output, x)):
|
||||||
os.mkdir(os.path.join(opt_dst_roms, x))
|
os.mkdir(os.path.join(args.output, x))
|
||||||
else:
|
else:
|
||||||
dst = os.path.join(opt_dst_roms, name)
|
dst = os.path.join(args.output, name)
|
||||||
if os.path.isfile(src):
|
if os.path.isfile(src):
|
||||||
shutil.copyfile(src, dst)
|
shutil.copyfile(src, dst)
|
||||||
print("%s" % (description.firstChild.data))
|
print("%s" % (description.firstChild.data))
|
||||||
|
|||||||
Reference in New Issue
Block a user