This repository has been archived on 2015-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
psh/commands.py
Ian Adam Naval 9324774208 Improve shell argument parsing
For example, this patch fixes the command "df | awk '{print $1}'"
2015-02-20 22:53:22 -05:00

52 lines
1.3 KiB
Python

from formatters import Printer
from io import StringIO
class BaseCommand(object):
def __call__(self, *args, **kwargs):
raise NotImplementedError(
"BaseCommands must be callable and return a generator")
class NoneCommand(BaseCommand):
def __call__(self, *args, **kwargs):
return []
class RawCommand(BaseCommand):
"""Fallback raw command that just invokes an existing Unix utility program
with the builtin subprocess module. Each output object is just a tree node
whose data is a simple string."""
def __init__(self, cmd, input_generator=[]):
self.input_generator = input_generator
self.cmd = cmd
def __call__(self, input_generator=[], *args, **kwargs):
import subprocess
try:
p = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def output_generator():
input_str = b""
for line in input_generator:
input_str += line + b'\n'
outs, errs = p.communicate(input_str)
if outs:
yield outs
return output_generator()
except:
import traceback
traceback.print_exc()
return []
registered_cmds = []
def register_cmd(cls):
registered_cmds.append(cls.__name__)
return cls