forked from Mirror/pmbootstrap
pmb: Make RunOutputTypeDefault and RunOutputTypePopen enums
This allows us to get rid of some of the validation in sanity_checks() as mypy handles this validation at "build time", and any typos in the enum instantiation would be a runtime error rather than a silent failure. Additionally, it allows us to encode some of the behaviour of the different output types into the type definition itself by using methods. Part-of: https://gitlab.postmarketos.org/postmarketOS/pmbootstrap/-/merge_requests/2642
This commit is contained in:
parent
77b2717d66
commit
7d2f055bcb
20 changed files with 167 additions and 84 deletions
51
pmb/types.py
51
pmb/types.py
|
@ -58,8 +58,55 @@ class CrossCompile(enum.Enum):
|
|||
return Chroot.native()
|
||||
|
||||
|
||||
RunOutputTypeDefault = Literal["log", "stdout", "interactive", "tui", "null"]
|
||||
RunOutputTypePopen = Literal["background", "pipe"]
|
||||
class RunOutputTypeDefault(enum.Enum):
|
||||
LOG = enum.auto()
|
||||
STDOUT = enum.auto()
|
||||
INTERACTIVE = enum.auto()
|
||||
TUI = enum.auto()
|
||||
NULL = enum.auto()
|
||||
|
||||
def is_to_stdout(self) -> bool:
|
||||
match self:
|
||||
case self.STDOUT | self.INTERACTIVE:
|
||||
return True
|
||||
case self.LOG | self.TUI | self.NULL:
|
||||
return False
|
||||
case _:
|
||||
raise AssertionError
|
||||
|
||||
def has_timeout(self) -> bool:
|
||||
match self:
|
||||
case self.LOG | self.STDOUT:
|
||||
return True
|
||||
case self.INTERACTIVE | self.TUI | self.NULL:
|
||||
return False
|
||||
case _:
|
||||
raise AssertionError
|
||||
|
||||
def has_pass_stdin(self) -> bool:
|
||||
match self:
|
||||
case self.INTERACTIVE | self.TUI:
|
||||
return True
|
||||
case self.LOG | self.STDOUT | self.NULL:
|
||||
return False
|
||||
case _:
|
||||
raise AssertionError
|
||||
|
||||
|
||||
class RunOutputTypePopen(enum.Enum):
|
||||
BACKGROUND = enum.auto()
|
||||
PIPE = enum.auto()
|
||||
|
||||
def is_to_stdout(self) -> bool:
|
||||
return False
|
||||
|
||||
def has_timeout(self) -> bool:
|
||||
return False
|
||||
|
||||
def has_pass_stdin(self) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
RunOutputType = RunOutputTypeDefault | RunOutputTypePopen
|
||||
RunReturnType = str | int | subprocess.Popen
|
||||
PathString = Path | str
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue