From 2ed55a58d2c4340da58d635526ff58f474cb7214 Mon Sep 17 00:00:00 2001 From: Sam Abradi Date: Tue, 24 Feb 2015 18:00:21 -0500 Subject: [PATCH] fixed the bullshit with the name conflict when loading commands that share names with commands being loaded from PATH. also made arguments work, basically by making BaseCommand take an aray of args in __init__ and then passing it all of the args in parse_cmd --- commands.py | 3 ++- example_cmd.py | 9 ++++----- main.py | 5 +++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/commands.py b/commands.py index 9071680..b672a00 100644 --- a/commands.py +++ b/commands.py @@ -3,7 +3,8 @@ from io import StringIO class BaseCommand(object): - + def __init__(self, args): + self.cmd_args = args def __call__(self, *args, **kwargs): raise NotImplementedError( "BaseCommands must be callable and return a generator") diff --git a/example_cmd.py b/example_cmd.py index 75385a8..aa704c4 100644 --- a/example_cmd.py +++ b/example_cmd.py @@ -2,7 +2,6 @@ from commands import BaseCommand, register_cmd @register_cmd class example_cmd(BaseCommand): - def __call__(self, *args, **kwargs): def output_generator(): yield b'example' @@ -12,9 +11,9 @@ class example_cmd(BaseCommand): @register_cmd class echo(BaseCommand): - - def __call__(self, *args, **kwargs): + def __call__(self,*args,**kwargs): def output_generator(): - for line in self.input_cmd(): - yield line + for line in self.cmd_args: + yield line.encode('utf-8') return output_generator() + diff --git a/main.py b/main.py index 0e12c94..db8689b 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,8 @@ for path in os.environ['PATH'].split(':'): if os.path.exists(path): binaries = os.listdir(path) for binary in binaries: - globals()[binary] = RawCommand(binary) + if binary not in globals(): + globals()[binary] = RawCommand(binary) def parse_cmd(potential_cmd): @@ -29,7 +30,7 @@ def parse_cmd(potential_cmd): if cmd_name not in registered_cmds: return "RawCommand({})".format(shlex.split(potential_cmd)) else: - return "{}()".format(cmd_name) + return "{0}({1})".format(cmd_name,str(args)) def parse_cmds(raw):