wmlunits: Normalize file paths consistently
This commit is contained in:
parent
89f819661c
commit
71ff0cfabc
3 changed files with 39 additions and 24 deletions
|
@ -3,7 +3,7 @@
|
|||
"""
|
||||
Various helpers for use by the wmlunits tool.
|
||||
"""
|
||||
import sys, os, re, glob, shutil, copy, subprocess
|
||||
import sys, os, re, glob, shutil, copy, subprocess, traceback
|
||||
|
||||
import wesnoth.wmlparser3 as wmlparser3
|
||||
from unit_tree.team_colorizer import colorize
|
||||
|
@ -47,7 +47,10 @@ class ImageCollector:
|
|||
tilde = name.find("~")
|
||||
if tilde >= 0:
|
||||
name = name[:tilde]
|
||||
bases = [os.path.join(self.datadir, "data/core/images"), os.path.join(self.datadir, "images")]
|
||||
bases = [
|
||||
os.path.join(self.datadir, "data", "core", "images"),
|
||||
os.path.join(self.datadir, "images")
|
||||
]
|
||||
binpaths = self.binary_paths_per_addon.get(addon, [])[:]
|
||||
binpaths.reverse()
|
||||
for path in binpaths:
|
||||
|
@ -66,9 +69,10 @@ class ImageCollector:
|
|||
bases = new_bases
|
||||
|
||||
for ipath in bases:
|
||||
ipath = os.path.normpath(ipath)
|
||||
if os.path.exists(ipath):
|
||||
return ipath, bases
|
||||
return None, bases
|
||||
return ipath, list(map(os.path.normpath, bases))
|
||||
return None, list(map(os.path.normpath, bases))
|
||||
|
||||
def add_image_check(self, addon, name, no_tc=False, check_transparent=False):
|
||||
if (addon, name) in self.images_by_addon_name:
|
||||
|
@ -105,7 +109,7 @@ class ImageCollector:
|
|||
if ipath:
|
||||
id_name = make_name(ipath)
|
||||
else:
|
||||
id_name = make_name(addon + "/" + name)
|
||||
id_name = make_name(os.path.join(addon, name))
|
||||
|
||||
image = Image(id_name, ipath, bases, no_tc)
|
||||
image.addons.add(addon)
|
||||
|
@ -125,15 +129,18 @@ class ImageCollector:
|
|||
opath = os.path.join(target_path, "pics", image.id_name)
|
||||
try:
|
||||
os.makedirs(os.path.dirname(opath))
|
||||
except OSError:
|
||||
except FileExistsError:
|
||||
pass
|
||||
except OSError:
|
||||
traceback.print_exc()
|
||||
|
||||
no_tc = image.no_tc
|
||||
|
||||
ipath = os.path.normpath(image.ipath)
|
||||
cdir = os.path.normpath(options.config_dir + "/data/add-ons")
|
||||
if ipath.startswith(cdir):
|
||||
ipath = os.path.join(options.addons, ipath[len(cdir):].lstrip("/"))
|
||||
default_addons_dir = os.path.join(options.config_dir, "data", "add-ons")
|
||||
if ipath.startswith(default_addons_dir):
|
||||
# Override with custom --addons
|
||||
ipath = os.path.join(options.addons, os.path.relpath(ipath, default_addons_dir))
|
||||
if ipath and os.path.exists(ipath) and not os.path.isdir(ipath):
|
||||
if no_tc:
|
||||
shutil.copy2(ipath, opath)
|
||||
|
@ -141,8 +148,8 @@ class ImageCollector:
|
|||
colorize(None, ipath, opath, magick=self.magick)
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"Warning: Required image %s does not exist (referenced by %s).\n" % (
|
||||
image.id_name, ", ".join(image.addons)))
|
||||
"Warning: Required image %s does not exist at %s (referenced by %s).\n" % (
|
||||
image.id_name, ipath, ", ".join(image.addons)))
|
||||
if options.verbose:
|
||||
if image.bases:
|
||||
sys.stderr.write("Warning: Looked at the following locations:\n")
|
||||
|
|
|
@ -12,7 +12,7 @@ import urllib.parse
|
|||
import unit_tree.helpers as helpers
|
||||
import wesnoth.wmlparser3 as wmlparser3
|
||||
|
||||
PICS_LOCATION = "../../pics"
|
||||
PICS_LOCATION = os.path.join("..", "..", "pics")
|
||||
|
||||
# Icons for mainline terrains used on the unit details page
|
||||
TERRAIN_ICONS = {
|
||||
|
@ -737,7 +737,7 @@ class HTMLOutput:
|
|||
error_message("Warning: Missing image for unit %s(%s).\n" %
|
||||
(u.get_text_val("id"), x.name.decode("utf8")))
|
||||
return None, None
|
||||
icpic = image_collector.add_image_check(self.addon, image)
|
||||
icpic = image_collector.add_image_check(self.addon, os.path.normpath(image))
|
||||
if not icpic.ipath:
|
||||
error_message("Warning: No picture %s for unit %s.\n" %
|
||||
(image, u.get_text_val("id")))
|
||||
|
@ -813,9 +813,11 @@ class HTMLOutput:
|
|||
write('<col class="col%d" />' % i)
|
||||
write('</colgroup>')
|
||||
|
||||
pic = image_collector.add_image("general",
|
||||
"../../../images/misc/leader-crown.png",
|
||||
no_tc=True)
|
||||
pic = image_collector.add_image(
|
||||
"general",
|
||||
os.path.join("..", "..", "..", "images", "misc", "leader-crown.png"),
|
||||
no_tc=True
|
||||
)
|
||||
crownimage = cleanurl(path2url(os.path.join(PICS_LOCATION, pic)))
|
||||
ms = None
|
||||
for row in range(len(rows)):
|
||||
|
@ -931,10 +933,10 @@ class HTMLOutput:
|
|||
|
||||
r = T(attack, "range")
|
||||
t = T(attack, "type")
|
||||
range_icon = image_collector.add_image_check(self.addon, 'icons/profiles/%s_attack.png' % r, no_tc=True)
|
||||
range_icon = image_collector.add_image_check(self.addon, os.path.normpath('icons/profiles/%s_attack.png' % r), no_tc=True)
|
||||
range_icon = cleanurl(path2url(os.path.join(PICS_LOCATION, range_icon.id_name)))
|
||||
range_alt_text = 'attack range %s' % cleantext(_(r), quote=False)
|
||||
type_icon = image_collector.add_image_check(self.addon, 'icons/profiles/%s.png' % t, no_tc=True)
|
||||
type_icon = image_collector.add_image_check(self.addon, os.path.normpath('icons/profiles/%s.png' % t), no_tc=True)
|
||||
type_icon = cleanurl(path2url(os.path.join(PICS_LOCATION, type_icon.id_name)))
|
||||
type_alt_text = 'attack type %s' % cleantext(_(t), quote=False)
|
||||
x = '<img src="%s" alt="(%s)"/> <img src="%s" alt="(%s)"/> ' % (range_icon, range_alt_text, type_icon, type_alt_text)
|
||||
|
@ -1192,7 +1194,7 @@ class HTMLOutput:
|
|||
if not icon:
|
||||
icon = "attacks/%s.png" % aid
|
||||
|
||||
image_add = image_collector.add_image_check(self.addon, icon, no_tc=True)
|
||||
image_add = image_collector.add_image_check(self.addon, os.path.normpath(icon), no_tc=True)
|
||||
if not image_add.ipath:
|
||||
error_message("Error: No attack icon '%s' found for '%s'.\n" % (
|
||||
icon, uid))
|
||||
|
@ -1204,7 +1206,7 @@ class HTMLOutput:
|
|||
write('<td><b>%s</b></td>' % cleantext(aname, quote=False))
|
||||
|
||||
t = T(attack, "type")
|
||||
type_icon = image_collector.add_image_check(self.addon, 'icons/profiles/%s.png' % t, no_tc=True)
|
||||
type_icon = image_collector.add_image_check(self.addon, os.path.normpath('icons/profiles/%s.png' % t), no_tc=True)
|
||||
type_icon = cleanurl(os.path.join(PICS_LOCATION, type_icon.id_name))
|
||||
type_alt_text = cleantext('%s attack' % t, quote=False)
|
||||
x = '<td><img src="%s" alt="(%s)"/> %s</td>' % (type_icon, type_alt_text, cleantext(_(t), quote=False))
|
||||
|
@ -1216,7 +1218,7 @@ class HTMLOutput:
|
|||
write('<td><i>%s</i></td>' % x)
|
||||
|
||||
r = T(attack, "range")
|
||||
range_icon = image_collector.add_image_check(self.addon, 'icons/profiles/%s_attack.png' % r, no_tc=True)
|
||||
range_icon = image_collector.add_image_check(self.addon, os.path.normpath('icons/profiles/%s_attack.png' % r), no_tc=True)
|
||||
range_icon = cleanurl(os.path.join(PICS_LOCATION, range_icon.id_name))
|
||||
range_alt_text = cleantext('%s attack' % r, quote=False)
|
||||
x = '<td><img src="%s" alt="(%s)"/> %s</td>' % (range_icon, range_alt_text, cleantext(_(r), quote=False))
|
||||
|
@ -1261,7 +1263,7 @@ class HTMLOutput:
|
|||
write('<tr>\n')
|
||||
else:
|
||||
write('<td></td>')
|
||||
picname = image_collector.add_image(self.addon, ricon, no_tc=True)
|
||||
picname = image_collector.add_image(self.addon, os.path.normpath(ricon), no_tc=True)
|
||||
icon = os.path.join(PICS_LOCATION, picname)
|
||||
write('<td><img src="%s" alt="(icon)" /></td>\n' % (icon, ))
|
||||
write('<th>%s</th><td class="%s">%s</td>\n' % (cleantext(_(rid), quote=False), ' '.join(resist_classes), resist_str))
|
||||
|
@ -1345,7 +1347,7 @@ class HTMLOutput:
|
|||
|
||||
write('<tr>\n')
|
||||
picname = image_collector.add_image(self.addon,
|
||||
"terrain/%s.png" % ticon,
|
||||
os.path.normpath("terrain/%s.png" % ticon),
|
||||
no_tc=True)
|
||||
icon = os.path.join(PICS_LOCATION, picname)
|
||||
write('<td><img src="%s" alt="(icon)" /></td>\n' % cleanurl(icon))
|
||||
|
|
|
@ -40,7 +40,7 @@ def shell_out(wesnoth_exe, com):
|
|||
com = base_com + cli_opts
|
||||
p = subprocess.run(com, capture_output=True, text=True)
|
||||
if p.returncode == 0:
|
||||
return p.stdout.strip()
|
||||
return os.path.normpath(p.stdout.strip())
|
||||
return ""
|
||||
|
||||
def move(f, t, name):
|
||||
|
@ -636,6 +636,12 @@ if __name__ == '__main__':
|
|||
if not options.transdir:
|
||||
options.transdir = os.getcwd()
|
||||
|
||||
options.data_dir = os.path.normpath(options.data_dir)
|
||||
options.config_dir = os.path.normpath(options.config_dir)
|
||||
options.transdir = os.path.normpath(options.transdir)
|
||||
if options.addons:
|
||||
options.addons = os.path.normpath(options.addons)
|
||||
|
||||
if options.wiki:
|
||||
wiki_output.main()
|
||||
sys.exit(0)
|
||||
|
|
Loading…
Add table
Reference in a new issue