shell.py 861 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from pathlib import Path
  2. from typing import Union, Iterable
  3. from asyncio.subprocess import Process
  4. from .process import run_cmd
  5. class Shell:
  6. pwd: Union[Path, None] = None
  7. def _kwargs_to_params(self, **kwargs: dict[str, str]) -> list[str]:
  8. result = []
  9. for key in kwargs:
  10. param = key.replace('_', '-')
  11. value = kwargs[key]
  12. if isinstance(value, bool):
  13. result.append(param)
  14. else:
  15. result.extend((param, kwargs[key]))
  16. return result
  17. def cd(self, path: Union[str, Path]):
  18. path = Path(path)
  19. if path.is_file():
  20. raise ValueError(f'{path} is a file')
  21. self.pwd = path
  22. return self
  23. async def run(self, args: Iterable[str], **kwargs) -> Process:
  24. return await run_cmd(args, self.pwd, **kwargs)