2025-04-07 02:19:25 -04:00
|
|
|
import io
|
|
|
|
import pathlib
|
|
|
|
import os
|
|
|
|
import re
|
2025-05-17 11:50:59 -04:00
|
|
|
import tomlkit
|
2025-04-07 02:19:25 -04:00
|
|
|
|
2025-05-17 11:50:59 -04:00
|
|
|
from cliff.command import Command
|
2025-04-07 02:19:25 -04:00
|
|
|
|
2025-04-28 18:31:36 -04:00
|
|
|
|
2025-05-17 11:50:59 -04:00
|
|
|
class Base(Command):
|
2025-06-14 22:40:05 -04:00
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super().get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'--doom', default=pathlib.Path.home().joinpath("doom"))
|
|
|
|
parser.add_argument('--config-name', default='config.toml')
|
|
|
|
return parser
|
2025-04-28 18:31:36 -04:00
|
|
|
|
2025-06-14 22:40:05 -04:00
|
|
|
def init_base(self, parsed_args):
|
|
|
|
self._doom = pathlib.Path(parsed_args.doom)
|
|
|
|
self._config_name = parsed_args.config_name
|
|
|
|
self._config = tomlkit.toml_file.TOMLFile(
|
|
|
|
self.doom.joinpath(self.config_name)).read()
|
|
|
|
self._dsda = self._config.get("dsda")
|
|
|
|
if self.dsda is None:
|
|
|
|
raise Exception(
|
|
|
|
"required key 'dsda' not set in config "
|
|
|
|
+ f"{self.doom.joinpath(self.config_name)}.")
|
|
|
|
for d in ("iwads", "pwads", "demos", "fabricate"):
|
2025-07-03 18:04:19 -04:00
|
|
|
self._init_attr(d, d, fn=self.doom.joinpath)
|
|
|
|
|
|
|
|
self._init_attr("thumbnail.height", 1280)
|
|
|
|
self._init_attr("thumbnail.width", 720)
|
|
|
|
self._init_attr("thumbnail.font", None)
|
|
|
|
self._init_attr("thumbnail.text_fill", "white")
|
|
|
|
self._init_attr("thumbnail.text_stroke", "red")
|
|
|
|
self._init_attr("fetch.mirror", "https://youfailit.net/pub/idgames")
|
2025-06-14 22:40:05 -04:00
|
|
|
|
|
|
|
def run(self, parsed_args):
|
|
|
|
self.init_base(parsed_args)
|
|
|
|
self.take_action(parsed_args)
|
|
|
|
|
2025-07-03 18:04:19 -04:00
|
|
|
def _init_attr(self, what, default, fn=lambda x: x):
|
|
|
|
what = what.replace(".", "_")
|
2025-06-14 22:40:05 -04:00
|
|
|
setattr(
|
2025-07-03 18:04:19 -04:00
|
|
|
self, f"_{what}", fn(self._config.get(what, default)))
|
2025-06-14 22:40:05 -04:00
|
|
|
setattr(
|
|
|
|
type(self), what, property(lambda self: getattr(self, f"_{what}")))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def doom(self):
|
|
|
|
return self._doom
|
|
|
|
|
|
|
|
@property
|
|
|
|
def config_name(self):
|
|
|
|
return self._config_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dsda(self):
|
|
|
|
return self._doom.joinpath(self._dsda)
|
|
|
|
|
|
|
|
def iwad_path(self, wad):
|
|
|
|
iwad = self.iwads.joinpath(self._config.get("default_iwad"))
|
|
|
|
iwadpath = self.pwads.joinpath(wad).joinpath("iwad")
|
|
|
|
if iwadpath.exists():
|
|
|
|
with io.open(iwadpath) as f:
|
|
|
|
iwad = self.iwads.joinpath(f.read().strip() + ".WAD")
|
|
|
|
return iwad
|