Made registering commands better

Registering commands requires adding the name as a string so that we can
have nice PEP 8-compatible class names for commands.
This commit is contained in:
Ian Adam Naval 2015-02-26 17:47:06 -05:00
parent 26cb8a5ed7
commit 7e708ba4ab
3 changed files with 24 additions and 11 deletions

View File

@ -0,0 +1,11 @@
from psh.commands import registered_cmds
# Import the exported commands
from psh.example_cmd import *
# Instantiate the registered commands
for name, cls in registered_cmds.items():
globals()[name] = cls()
# Only export the names of registered commands
__all__ = registered_cmds.keys()

View File

@ -38,9 +38,11 @@ class BaseCommand(object):
return cmd return cmd
registered_cmds = [] registered_cmds = {}
def register_cmd(cls): def register_cmd(name):
"""Decorator for putting all of the commands in one nice place.""" def decorator(cls):
registered_cmds.append(cls.__name__) """Decorator for putting all of the commands in one nice place."""
return cls registered_cmds[name] = cls
return cls
return decorator

View File

@ -1,8 +1,8 @@
from psh.commands import BaseCommand, register_cmd from psh.commands import BaseCommand, register_cmd
@register_cmd @register_cmd("example")
class example_cmd(BaseCommand): class Example(BaseCommand):
"""Simple command that just returns 'example' and 'command'. Does """Simple command that just returns 'example' and 'command'. Does
nothing at all with the input.""" nothing at all with the input."""
@ -13,13 +13,13 @@ class example_cmd(BaseCommand):
return output_generator return output_generator
@register_cmd @register_cmd("echo")
class echo(BaseCommand): class Echo(BaseCommand):
"""Echoes anything from the command line arguments as well as input """Echoes anything from the command line arguments as well as input
from the previous command.""" from the previous command."""
def __init__(self, args): def __init__(self, args=[]):
super(echo, self).__init__() super(Echo, self).__init__()
self.args = args self.args = args
def call(self,*args,**kwargs): def call(self,*args,**kwargs):