Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tusharsadhwani/zxpy
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharsadhwani committed May 10, 2021
2 parents 7f50fd8 + 3d3b8a0 commit c7bbfb2
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions zx.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import subprocess
import sys
import traceback
from typing import Union
from typing import Optional, Union


def cli() -> None:
Expand All @@ -54,10 +54,25 @@ def cli() -> None:
run_zxpy(filename, module)


def run_shell(command: str) -> str:
def run_shell(command: str, print_it: bool = False) -> Optional[str]:
"""This is indirectly run when doing ~'...'"""
output = subprocess.getoutput(command)
return output
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True
)
assert process.stdout is not None

if not print_it:
return process.stdout.read().decode()

while char := process.stdout.read(1):
if print_it:
sys.stdout.buffer.write(char)
sys.stdout.flush()

return None


def run_zxpy(filename: str, module: ast.Module) -> None:
Expand Down Expand Up @@ -109,19 +124,19 @@ def visit_Assign(self, assign: ast.Assign) -> ast.Assign:


def print_shell_outputs(expr_statement: ast.Expr) -> None:
"""Wrap every top level run_shell call with print() to get output"""
"""Set print_it to True on every top level run_shell"""
expr = expr_statement.value
if (
isinstance(expr, ast.Call)
and isinstance(expr.func, ast.Name)
and expr.func.id == 'run_shell'
):
new_expr = ast.Call(
func=ast.Name(id='print', ctx=ast.Load()),
args=[expr],
keywords=[],
)
expr_statement.value = new_expr
expr.keywords = [
ast.keyword(
arg='print_it',
value=ast.Constant(value=True),
)
]


def setup_zxpy_repl() -> None:
Expand Down

0 comments on commit c7bbfb2

Please sign in to comment.