Have the ls command to inherit from lister.
This requires multiple inheritance and other shenanigans to actually work. I think I'm supposed to do this with hooks. Work for the future.
This commit is contained in:
parent
16eb67c797
commit
5e188573a0
2 changed files with 55 additions and 33 deletions
|
@ -1,20 +1,13 @@
|
|||
import cliff.command
|
||||
import cliff.lister
|
||||
import io
|
||||
import pathlib
|
||||
import os
|
||||
import re
|
||||
import tomlkit.toml_file
|
||||
|
||||
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
|
||||
|
||||
class ConfigBase(object):
|
||||
def init_base(self, parsed_args):
|
||||
self._doom = pathlib.Path(parsed_args.doom)
|
||||
self._config_name = parsed_args.config_name
|
||||
|
@ -40,11 +33,6 @@ class Base(Command):
|
|||
"https://youfailit.net/pub/idgames"
|
||||
)
|
||||
|
||||
def run(self, parsed_args):
|
||||
self.init_base(parsed_args)
|
||||
self.finalize()
|
||||
self.take_action(parsed_args)
|
||||
|
||||
def _init_attr(self, what, default, fn=lambda x: x):
|
||||
propname = "_".join(what)
|
||||
val = self._config
|
||||
|
@ -71,3 +59,32 @@ class Base(Command):
|
|||
@property
|
||||
def dsda(self):
|
||||
return self._doom.joinpath(self._dsda)
|
||||
|
||||
|
||||
def AddCommonArgs(parser):
|
||||
parser.add_argument(
|
||||
"--doom", default=pathlib.Path.home().joinpath("doom"))
|
||||
parser.add_argument("--config-name", default="config.toml")
|
||||
return parser
|
||||
|
||||
|
||||
class Base(cliff.command.Command, ConfigBase):
|
||||
def get_parser(self, prog_name):
|
||||
return AddCommonArgs(super().get_parser(prog_name))
|
||||
|
||||
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 get_parser(self, prog_name):
|
||||
return AddCommonArgs(super().get_parser(prog_name))
|
||||
|
||||
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)
|
||||
|
|
41
dcc/ls.py
41
dcc/ls.py
|
@ -2,7 +2,7 @@ import dcc.config
|
|||
import os
|
||||
|
||||
|
||||
class List(dcc.config.Base):
|
||||
class List(dcc.config.ListerBase):
|
||||
def get_parser(self, prog_name):
|
||||
parser = super().get_parser(prog_name)
|
||||
parser.add_argument("target")
|
||||
|
@ -12,28 +12,33 @@ class List(dcc.config.Base):
|
|||
def take_action(self, parsed_args):
|
||||
match parsed_args.target:
|
||||
case "pwads":
|
||||
self.list(x.name for x in os.scandir(self.pwads) if x.is_dir())
|
||||
return (
|
||||
("pwads",), sorted(
|
||||
(x.name,) for x in os.scandir(self.pwads) if x.is_dir()
|
||||
)
|
||||
)
|
||||
case "iwads":
|
||||
self.list(
|
||||
x.name for x in os.scandir(self.iwads)
|
||||
if x.is_file()
|
||||
return (
|
||||
("iwads",), sorted(
|
||||
(x.name,) for x in os.scandir(self.iwads) if x.is_file()
|
||||
)
|
||||
)
|
||||
case "demos":
|
||||
self.list(
|
||||
x.name for x in
|
||||
os.scandir(self.demos.joinpath(parsed_args.wad))
|
||||
if x.name.endswith(".lmp")
|
||||
return (
|
||||
("demos",), sorted(
|
||||
(x.name,) for x in
|
||||
os.scandir(self.demos.joinpath(parsed_args.wad))
|
||||
if x.name.endswith(".lmp")
|
||||
)
|
||||
)
|
||||
|
||||
case "videos":
|
||||
self.list(
|
||||
x.name for x in
|
||||
os.scandir(self.fabricate.joinpath(parsed_args.wad))
|
||||
if x.name.endswith(".mp4")
|
||||
return (
|
||||
("videos",), sorted(
|
||||
(x.name,) for x in
|
||||
os.scandir(self.fabricate.joinpath(parsed_args.wad))
|
||||
if x.name.endswith(".mp4")
|
||||
)
|
||||
)
|
||||
case _:
|
||||
raise Exception(f"unknown target {parsed_args.target}")
|
||||
|
||||
def list(self, gen):
|
||||
# TODO: fancy text?
|
||||
for i in sorted(gen):
|
||||
print(i)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue