jw-pkg/src/python/jw/pkg/lib/base.py

53 lines
1.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from __future__ import annotations
from enum import Enum, auto
from typing import NamedTuple, TypeAlias, TYPE_CHECKING
if TYPE_CHECKING:
from typing import Type
class InputMode(Enum):
Interactive = auto()
NonInteractive = auto()
OptInteractive = auto()
Auto = auto()
Input: TypeAlias = InputMode | bytes | str
class Result(NamedTuple):
stdout: str|None
stderr: str|None
status: int|None
def decode(self, encoding='UTF-8', errors='replace') -> Result:
return Result(
self.stdout.decode(encoding, errors=errors) if self.stdout is not None else None,
self.stderr.decode(encoding, errors=errors) if self.stderr is not None else None,
self.status
)
class StatResult(NamedTuple):
mode: int
owner: str
group: str
size: int
atime: int
mtime: int
ctime: int
@classmethod
def from_os(cls, rhs: os.stat_result) -> StatResult:
import pwd, grp
return StatResult(
rhs.st_mode,
pwd.getpwuid(rhs.st_uid).pw_name,
grp.getgrgid(rhs.st_gid).gr_name,
rhs.st_size,
rhs.st_atime,
rhs.st_mtime,
rhs.st_ctime,
)