reduce hardcoded extensions in python files, only instance at wmltool3.py

This commit is contained in:
Subhraman Sarkar 2024-07-01 15:08:01 +05:30
parent 078c618f3a
commit 4c1b1ebe20
2 changed files with 37 additions and 25 deletions

View File

@ -11,15 +11,27 @@ import sys, os, re, sre_constants, hashlib, glob, gzip
import string
import enum
# Extensions
# Ordering is important, see default extensions below
map_extensions = ("map", "mask")
wml_extensions = ("cfg",)
image_extensions = ("png", "jpg", "jpeg", "webp")
sound_extensions = ("ogg", "wav")
vc_directories = (".git", ".svn")
misc_files_extensions = ("-bak", ".DS_Store", "Thumbs.db") # These files and extensions should be included in the `default_blacklist` in filesystem.hpp.
l10n_directories = ("l10n",)
# Default extensions
default_map_extension = "." + map_extensions[0] # ".map" at the moment
default_mask_extension = "." + map_extensions[1] # ".mask" at the moment
default_wml_extension = "." + wml_extensions[0] # ".cfg" at the moment
resource_extensions = map_extensions + image_extensions + sound_extensions
image_reference = r"[A-Za-z0-9{}.][A-Za-z0-9_/+{}.\-\[\]~\*,]*\.(png|jpe?g|webp)(?=(~.*)?)"
# Directories
l10n_directories = ("l10n",)
vc_directories = (".git", ".svn")
# Misc files and extensions
misc_files_extensions = ("-bak", ".DS_Store", "Thumbs.db") # These files and extensions should be included in the `default_blacklist` in filesystem.hpp.
EQUALS = '='
QUOTE = '"'
OPEN_BRACE = '{'
@ -184,7 +196,7 @@ class Forest:
subtree = []
rooted = False
if os.path.isdir(directory): # So we skip .cfgs in a UMC mirror
oldmain = os.path.join(os.path.dirname(directory), os.path.basename(directory) + '.cfg')
oldmain = os.path.join(os.path.dirname(directory), os.path.basename(directory) + default_wml_extension)
if os.path.isfile(oldmain):
subtree.append(oldmain)
base = os.path.basename(os.path.dirname(os.path.abspath(directory)))
@ -199,8 +211,8 @@ class Forest:
rooted = True
elif os.path.basename(root) in roots:
for subdir in dirlist:
if subdir + '.cfg' in files:
files.remove(subdir + '.cfg')
if subdir + default_wml_extension in files:
files.remove(subdir + default_wml_extension)
dirs.remove(subdir)
dirpath.append(os.path.join(root, subdir))
rooted = True
@ -219,8 +231,8 @@ class Forest:
if count >= (stop // 2):
roots.append(os.path.basename(root))
for subdir in dirlist:
if subdir + '.cfg' in files:
files.remove(subdir + '.cfg')
if subdir + default_wml_extension in files:
files.remove(subdir + default_wml_extension)
dirs.remove(subdir)
dirpath.append(os.path.join(root, subdir))
subtree.extend([os.path.normpath(os.path.join(root, x)) for x in files])
@ -265,9 +277,13 @@ class Forest:
for filename in tree:
yield (directory, filename)
def ismap(filename):
"Is this file a map?"
return filename.split('.')[-1] in map_extensions
def iswml(filename):
"Is the specified filename WML?"
return filename.endswith(".cfg")
return filename.split('.')[-1] in wml_extensions
def issave(filename):
"Is the specified filename a WML save? (Detects compressed saves too.)"
@ -1004,7 +1020,7 @@ class CrossRef:
for pattern in split_filenames(match):
for name in expand_square_braces(pattern):
# Catches maps that look like macro names.
if (name.endswith(".map") or name.endswith(".mask")):
if (ismap(name)):
if name.startswith("{~"):
name = name[2:]
elif name.startswith("{"):
@ -1132,8 +1148,8 @@ def resolve_unit_cfg(namespace, utype, resource=None):
else:
resource = utype
loc = namespace_directory(namespace) + "units/" + resource
if not loc.endswith(".cfg"):
loc += ".cfg"
if not loc.endswith(default_wml_extension):
loc += default_wml_extension
return loc
def resolve_unit_image(namespace, subdir, resource):

View File

@ -3055,10 +3055,6 @@ must be converted manually".format(filename, baseline + y + 1, old, name))
# Generic machinery starts here
def is_map(filename):
"Is this file a map?"
return filename.endswith(".map") or filename.endswith(".mask")
if 0: # Not used, as there are currently no defined map transforms
class maptransform_error(BaseException):
"Error object to be thrown by maptransform."
@ -3108,7 +3104,7 @@ def translator(filename, mapxforms, textxform):
return
# Pull file into an array of lines, CR-stripping as needed
mfile = []
map_only = filename.endswith(".map")
map_only = filename.endswith(default_map_extension)
terminator = "\n"
UTF8_BOM = "\ufeff"
for i, line in enumerate(unmodified):
@ -3145,9 +3141,9 @@ def translator(filename, mapxforms, textxform):
# Ignore all-caps macro arguments.
if refname == refname.upper():
pass
elif 'mask=' in line and not (refname.endswith("}") or refname.endswith(".mask")):
print('"%s", line %d: mask file without .mask extension or not a mask file (%s)' \
% (filename, lineno+1, refname))
elif 'mask=' in line and not (refname.endswith("}") or refname.endswith(default_mask_extension)):
print('"%s", line %d: mask file without %s extension or not a mask file (%s)' \
% (filename, lineno+1, default_mask_extension, refname))
# Exclude map_data= lines that are just 1 line without
# continuation, or which contain {}. The former are
# pathological and the parse won't handle them, the latter
@ -3163,7 +3159,7 @@ def translator(filename, mapxforms, textxform):
maskwarn = False
maptype = None
if map_only:
if filename.endswith(".mask"):
if filename.endswith(default_mask_extension):
maptype = "mask"
else:
maptype = "map"
@ -3239,8 +3235,8 @@ def translator(filename, mapxforms, textxform):
refre = re.compile(r"\{@?([^A-Z].*)\}").search(line)
if refre:
mapfile = refre.group(1)
if not mapfile.endswith(".map") and is_map(mapfile):
newline = newline.replace(mapfile, mapfile + ".map")
if not mapfile.endswith(default_map_extension) and ismap(mapfile):
newline = newline.replace(mapfile, mapfile + default_map_extension)
newdata.append(newline + terminator)
if newline != line:
if verbose > 0:
@ -3505,8 +3501,8 @@ vctypes = (".svn", ".git", ".hg")
def interesting(fn):
"Is a file interesting for conversion purposes?"
return (fn.endswith(".cfg") and not fn.endswith("_info.cfg")) \
or is_map(fn) or issave(fn)
return (fn.endswith(default_wml_extension) and not fn.endswith("_info.cfg")) \
or ismap(fn) or issave(fn)
def allcfgfiles(directory):
"Get the names of all interesting files under directory."