Probably can't do better than this without messing around with the method resolution order and other things I'd rather not touch.
91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
import cliff.command
|
|
import cliff.lister
|
|
import io
|
|
import pathlib
|
|
import os
|
|
import re
|
|
import tomlkit.toml_file
|
|
|
|
|
|
class ConfigBase(object):
|
|
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)
|
|
|
|
def finalize(self):
|
|
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 _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)
|
|
|
|
|
|
def get_parser_func(toc):
|
|
def add_common_args(self, prog_name):
|
|
parser = super(toc, self).get_parser(prog_name)
|
|
parser.add_argument(
|
|
"--doom", default=pathlib.Path.home().joinpath("doom"))
|
|
parser.add_argument("--config-name", default="config.toml")
|
|
return parser
|
|
return add_common_args
|
|
|
|
|
|
class Base(cliff.command.Command, ConfigBase):
|
|
def run(self, parsed_args):
|
|
super().init_base(parsed_args)
|
|
super().finalize()
|
|
return self.take_action(parsed_args)
|
|
|
|
|
|
class ListerBase(cliff.lister.Lister, ConfigBase):
|
|
def run(self, parsed_args):
|
|
super().init_base(parsed_args)
|
|
super().finalize()
|
|
self.formatter = self._formatter_plugins[parsed_args.formatter].obj
|
|
columns, data = self.take_action(parsed_args)
|
|
return self.produce_output(parsed_args, columns, data)
|
|
|
|
|
|
for toc in (Base, ListerBase):
|
|
toc.get_parser = get_parser_func(toc)
|