Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CommandError (Exception):
- """Represents an error on command handling"""
- pass
- class Command (object):
- """Command
- Represents a command executed from CLI or
- the daemon.
- """
- _data = {}
- def __init__(self, command, params=None):
- """Constructor"""
- self._data['command'] = command.lower()
- if params is not None:
- self._data['params'] = dict(params)
- else:
- self._data['params'] = {}
- def get_command(self):
- """Returns the command name"""
- return self._data['command']
- def __getattr__(self, attr):
- return self._data[attr]
- def __setattr__(self, attr, value):
- self._data[attr] = value
- return self
- def __unicode__(self):
- """Returns the name of the command"""
- return self._data['command']
- class CommandHandler (object):
- """Command handler
- Handles commands and parameters supplied
- by users and makes the appropriate calls
- to the API.
- """
- _api = None
- def __init__ (self):
- """Constructor"""
- self._api = API()
- def handle (self, command):
- """Handles a command and outputs the response"""
- if not isinstance(command, Command):
- raise CommandError("Invalid command. Expecting Command object")
- cmd = command.get_command()
- if callable(getattr(self._api, cmd)):
- pass
- else:
- raise CommandError("Invalid command %s" % cmd)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement