Created
December 21, 2021 16:23
-
-
Save ciansen/2c8f3e369d0e545d56c69c33e174f4f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Install the viur-cli with: | |
## pip install viur-cli | |
import logging | |
import os | |
from subprocess import PIPE, Popen | |
from io import BytesIO | |
from typing import List | |
class ViurCommandContext(object): | |
def __init__(self, sub_cmd: str, *args) -> None: | |
super().__init__() | |
self._process: Popen = None | |
self._sub_cmd: str = sub_cmd | |
self._args: List[str] = args | |
self._output: BytesIO = BytesIO() | |
self._send_list: List[str] = list() | |
self._err = None | |
@property | |
def command(self) -> List[str]: | |
cmd = ["viur", self._sub_cmd] | |
cmd.extend(self._args) | |
return cmd | |
def __enter__(self): | |
logging.debug(f"Open new sub process with the command {self.command}") | |
self._process = Popen(self.command, stdin = PIPE, stdout = PIPE, stderr = PIPE, encoding='utf-8', universal_newlines=True) | |
# self._process = Popen(self.command, stdin = PIPE, encoding='utf-8', bufsize=650001) | |
return self | |
def send_line(self, text: str = '') -> None: | |
self._send_list.append(text + os.linesep) | |
logging.debug(f"[{self._process.pid}] Append {text} to the queue.") | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
rep = "".join(self._send_list) | |
print(rep.encode("utf-8")) | |
self._output, self._err = self._process.communicate(rep) | |
print(self._output) | |
if __name__ == "__main__": | |
with ViurCommandContext("init") as context: | |
context.send_line("y") | |
context.send_line("1") | |
context.send_line("1") | |
context.send_line("project-name") | |
context.send_line("abc") | |
context.send_line("n") | |
context.send_line("n") | |
context.send_line("n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment