70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
import io
|
|
import pathlib
|
|
import os
|
|
import re
|
|
import tomlkit
|
|
|
|
from cliff.command import Command
|
|
|
|
|
|
class Base(Command):
|
|
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
|
|
|
|
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"):
|
|
self._init_attr([d], d, fn=self.doom.joinpath)
|
|
|
|
self._init_attr(["thumbnail", "height"], 720)
|
|
self._init_attr(["thumbnail", "width"], 1280)
|
|
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"
|
|
)
|
|
|
|
def run(self, parsed_args):
|
|
self.init_base(parsed_args)
|
|
self.take_action(parsed_args)
|
|
|
|
def _init_attr(self, what, default, fn=lambda x: x):
|
|
propname = "_".join(what)
|
|
val = self._config
|
|
for w in what:
|
|
val = val[w]
|
|
if val is None:
|
|
val = default
|
|
break
|
|
|
|
setattr(self, f"_{propname}", fn(val))
|
|
setattr(
|
|
type(self), propname,
|
|
property(lambda self: getattr(self, f"_{propname}"))
|
|
)
|
|
|
|
@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)
|