feat: add presets
This commit is contained in:
+48
-24
@@ -1,19 +1,20 @@
|
||||
__all__ = (
|
||||
"EntryCommandNotImplementedError",
|
||||
"Entry",
|
||||
)
|
||||
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
from typing import Dict, Iterable, List, Optional
|
||||
|
||||
from .command import Command
|
||||
from .command import CommandExecutor
|
||||
|
||||
__all__ = (
|
||||
"EntryTaskNotImplementedError",
|
||||
"Entry",
|
||||
"EntryDetector",
|
||||
"EntryResolver",
|
||||
)
|
||||
|
||||
logger = logging.getLogger("entry")
|
||||
|
||||
|
||||
class EntryCommandNotImplementedError(NotImplementedError):
|
||||
class EntryTaskNotImplementedError(NotImplementedError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -22,27 +23,50 @@ class Entry:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entry_type: str,
|
||||
commands: Dict[str, List[List[str]]],
|
||||
tasks: Dict[str, List[str]],
|
||||
basepath: Path,
|
||||
):
|
||||
self._type = entry_type
|
||||
self._commands = commands
|
||||
self._tasks = tasks
|
||||
self._basepath = basepath
|
||||
|
||||
def run_command(self, command_name: str):
|
||||
if command_name == "show":
|
||||
self._logger.info(f"{self}")
|
||||
def show(self):
|
||||
self._logger.info(f"{self}")
|
||||
for task_name, commands in self._tasks.items():
|
||||
self._logger.debug(f" {task_name}")
|
||||
for command in commands:
|
||||
self._logger.trace(f" {command}")
|
||||
|
||||
def run_task(self, task_name: str):
|
||||
if task_name == "show":
|
||||
self.show()
|
||||
return
|
||||
self._logger.info(f"{command_name} {self}")
|
||||
if command_name not in self._commands:
|
||||
raise EntryCommandNotImplementedError(command_name)
|
||||
for command in self._commands[command_name]:
|
||||
Command(*command).run(self._basepath)
|
||||
if task_name not in self._tasks:
|
||||
raise EntryTaskNotImplementedError(task_name)
|
||||
self._logger.info(f"{task_name} {self}")
|
||||
for command in self._tasks[task_name]:
|
||||
CommandExecutor(command).run(self._basepath)
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self._type} {str(self._basepath)}"
|
||||
return f"{str(self._basepath)}"
|
||||
|
||||
@classmethod
|
||||
def detect(cls, basepath: Path) -> Optional["Entry"]:
|
||||
|
||||
class EntryDetector:
|
||||
def detect_entry(self, basepath: Path) -> Optional["Entry"]:
|
||||
raise NotImplemented
|
||||
|
||||
|
||||
class EntryResolver:
|
||||
def __init__(self, detector: EntryDetector, ignore: List[str]):
|
||||
self._detector = detector
|
||||
self._ignore = ignore
|
||||
|
||||
def resolve(self, basepath: Path, depth: int = 128) -> Iterable[Entry]:
|
||||
if basepath.is_dir():
|
||||
if basepath.name in self._ignore:
|
||||
return
|
||||
entry = self._detector.detect_entry(basepath)
|
||||
if entry:
|
||||
yield entry
|
||||
if depth > 0 and basepath.is_dir():
|
||||
for path in basepath.iterdir():
|
||||
yield from self.resolve(path, depth - 1)
|
||||
|
||||
Reference in New Issue
Block a user