Replace getopt code with argparse as its more modern.

This commit is contained in:
James David Clarke 2024-01-09 08:16:05 +00:00 committed by the-real-herowl
parent 6324c805eb
commit 254fe4d98e

View file

@ -8,33 +8,19 @@ __author__ = "Wuzzy"
__license__ = "MIT License" __license__ = "MIT License"
__status__ = "Development" __status__ = "Development"
import shutil, csv, os, tempfile, sys, getopt, glob import shutil, csv, os, tempfile, sys, argparse, glob
from PIL import Image from PIL import Image
from collections import Counter from collections import Counter
# Constants
SUPPORTED_MINECRAFT_VERSION="1.20"
# Helper vars # Helper vars
home = os.environ["HOME"] home = os.environ["HOME"]
mineclone2_path = home + "/.minetest/games/mineclone2" mineclone2_path = home + "/.minetest/games/mineclone2"
working_dir = os.getcwd() working_dir = os.getcwd()
appname = "Texture_Converter.py" appname = "Texture_Converter.py"
### SETTINGS ###
output_dir = working_dir
base_dir = None
# If True, will only make console output but not convert anything.
dry_run = False
# If True, textures will be put into a texture pack directory structure.
# If False, textures will be put into MineClone 2 directories.
make_texture_pack = True
# If True, prints all copying actions
verbose = False
PXSIZE = None
def detect_pixel_size(directory): def detect_pixel_size(directory):
sizes = [] sizes = []
for filename in glob.glob(directory + '/**/*.png', recursive=True): for filename in glob.glob(directory + '/**/*.png', recursive=True):
@ -46,55 +32,32 @@ def detect_pixel_size(directory):
print(f"Autodetected pixel size: {most_common_size[0]}x{most_common_size[1]}") print(f"Autodetected pixel size: {most_common_size[0]}x{most_common_size[1]}")
return most_common_size[0] return most_common_size[0]
syntax_help = appname+""" -i <input dir> [-o <output dir>] [-d] [-v|-q] [-h] # Argument parsing
Mandatory argument: description_text = f"""This is the official MineClone 2 Texture Converter.
-i <input directory> This will convert textures from Minecraft resource packs to
Directory of Minecraft resource pack to convert a Minetest texture pack.
Optional arguments: Supported Minecraft version: {SUPPORTED_MINECRAFT_VERSION} (Java Edition)
-p <texture size> """
Specify the size (in pixels) of the original textures (default: 16) parser = argparse.ArgumentParser(description=description_text)
-o <output directory> parser.add_argument("-i", "--input", required=True, help="Directory of Minecraft resource pack to convert")
Directory in which to put the resulting Minetest texture pack parser.add_argument("-o", "--output", default=working_dir, help="Directory in which to put the resulting Minetest texture pack")
(default: working directory) parser.add_argument("-p", "--pixelsize", type=int, help="Size (in pixels) of the original textures")
-d parser.add_argument("-d", "--dry_run", action="store_true", help="Pretend to convert textures without changing any files")
Just pretend to convert textures and just print output, but do not actually parser.add_argument("-v", "--verbose", action="store_true", help="Print out all copying actions")
change any files. args = parser.parse_args()
-v
Print out all copying actions
-h
Show this help and exit"""
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:o:p:dv")
except getopt.GetoptError:
print(
"""ERROR! The options you gave me make no sense!
Here's the syntax reference:""") ### SETTINGS ###
print(syntax_help) base_dir = args.input
sys.exit(2) output_dir = args.output
for opt, arg in opts: PXSIZE = args.pixelsize
if opt == "-h": # If True, will only make console output but not convert anything.
print( dry_run = args.dry_run
"""This is the official MineClone 2 Texture Converter. # If True, prints all copying actions
This will convert textures from Minecraft resource packs to verbose = args.verbose
a Minetest texture pack. # If True, textures will be put into a texture pack directory structure.
# If False, textures will be put into MineClone 2 directories.
Supported Minecraft version: 1.20 (Java Edition) make_texture_pack = True # Adjust as needed
Syntax:""")
print(syntax_help)
sys.exit()
elif opt == "-d":
dry_run = True
elif opt == "-v":
verbose = True
elif opt == "-i":
base_dir = arg
elif opt == "-o":
output_dir = arg
elif opt == "-p":
PXSIZE = int(arg)
if PXSIZE is None: if PXSIZE is None:
PXSIZE = detect_pixel_size(base_dir) PXSIZE = detect_pixel_size(base_dir)