From 2e49872061cb7ea776ee29f1f9aa630eb25b88e7 Mon Sep 17 00:00:00 2001 From: Ian Adam Naval Date: Thu, 26 Feb 2015 16:22:21 -0500 Subject: [PATCH 1/2] Add basic TreeNode --- commands.py | 2 ++ example_cmd.py | 13 ++++++++----- formatters.py | 3 ++- raw_commands.py | 7 +++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/commands.py b/commands.py index ab41676..31cdd43 100644 --- a/commands.py +++ b/commands.py @@ -1,5 +1,7 @@ from io import StringIO +from tree import TreeNode + class BaseCommand(object): """Commands can be used to chain the execution of multiple programs diff --git a/example_cmd.py b/example_cmd.py index 1b23ad3..9caaae3 100644 --- a/example_cmd.py +++ b/example_cmd.py @@ -1,5 +1,7 @@ from commands import BaseCommand, register_cmd +from tree import TreeNode + @register_cmd class example_cmd(BaseCommand): @@ -8,8 +10,8 @@ class example_cmd(BaseCommand): def call(self, *args, **kwargs): def output_generator(): - yield b'example' - yield b'command' + yield TreeNode(b'example') + yield TreeNode(b'command') return output_generator @@ -26,8 +28,9 @@ class echo(BaseCommand): input_generator = self.get_input_generator() def output_generator(): for args in self.args: - yield args.encode("utf-8") - for line in input_generator: - yield line + yield TreeNode(args.encode("utf-8")) + for node in input_generator: + line = node.data + yield TreeNode(line) return output_generator diff --git a/formatters.py b/formatters.py index 8a516fb..78153d6 100644 --- a/formatters.py +++ b/formatters.py @@ -7,6 +7,7 @@ class Printer(BaseCommand): def call(self): input_generator = self.get_input_generator() - for line in input_generator: + for node in input_generator: + line = node.data print(str(line.decode('utf-8'))) return None diff --git a/raw_commands.py b/raw_commands.py index 82f4c54..119d1c9 100644 --- a/raw_commands.py +++ b/raw_commands.py @@ -1,6 +1,8 @@ from formatters import Printer from commands import BaseCommand +from tree import TreeNode + class RawCommand(BaseCommand): """Fallback raw command that just invokes an existing Unix utility program @@ -18,11 +20,12 @@ class RawCommand(BaseCommand): p = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) def make_output_generator(): input_str = b"" - for line in input_generator: + for node in input_generator: + line = node.data input_str += line + b'\n' outs, errs = p.communicate(input_str) if outs: - yield outs + yield TreeNode(outs) return make_output_generator except: From e9d7f5f2b64573f864f45e09a0a7f93f235c356c Mon Sep 17 00:00:00 2001 From: Ian Adam Naval Date: Tue, 3 Mar 2015 12:14:20 -0500 Subject: [PATCH 2/2] Fix new stuff from tests to work with TreeNodes --- psh/commands.py | 2 +- psh/example_cmd.py | 2 +- psh/raw_commands.py | 3 +-- test/utils.py | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/psh/commands.py b/psh/commands.py index 89cb0bf..6bf481d 100644 --- a/psh/commands.py +++ b/psh/commands.py @@ -1,6 +1,6 @@ from io import StringIO -from tree import TreeNode +from psh.tree import TreeNode class BaseCommand(object): diff --git a/psh/example_cmd.py b/psh/example_cmd.py index 90c496d..e91de76 100644 --- a/psh/example_cmd.py +++ b/psh/example_cmd.py @@ -1,6 +1,6 @@ from psh.commands import BaseCommand, register_cmd -from tree import TreeNode +from psh.tree import TreeNode @register_cmd("example") diff --git a/psh/raw_commands.py b/psh/raw_commands.py index d509511..e6787c1 100644 --- a/psh/raw_commands.py +++ b/psh/raw_commands.py @@ -2,8 +2,7 @@ import shlex from psh.formatters import Printer from psh.commands import BaseCommand - -from tree import TreeNode +from psh.tree import TreeNode class RawCommand(BaseCommand): diff --git a/test/utils.py b/test/utils.py index be7696c..5228c55 100644 --- a/test/utils.py +++ b/test/utils.py @@ -14,7 +14,7 @@ class TestFormatter(BaseCommand): def call(self): input_generator = self.get_input_generator() for line in input_generator: - self.buffer.write(line.decode('utf-8')) + self.buffer.write(line.data.decode('utf-8')) return None def get_data(self):