diff options
Diffstat (limited to 'asciifarm/client')
| -rw-r--r-- | asciifarm/client/commandhandler.py | 58 | ||||
| -rw-r--r-- | asciifarm/client/inputhandler.py | 4 |
2 files changed, 43 insertions, 19 deletions
diff --git a/asciifarm/client/commandhandler.py b/asciifarm/client/commandhandler.py index 1f5177b..983e211 100644 --- a/asciifarm/client/commandhandler.py +++ b/asciifarm/client/commandhandler.py @@ -1,7 +1,12 @@ -import shlex import json +try: + import hy +except ImportError as e: + hy is None + hyErr = e + class InvalidCommandException(Exception): pass @@ -31,18 +36,32 @@ class CommandHandler: "exec": self.exec, "scrollchat": self.scrollChat, "json": self.json, - "ijson": self.ijson - } + "j": self.json, + "ijson": self.ijson, + "ij": self.ijson, + "hy": self.hy + } + + self.evalArgs = { + "self": self, + "client": self.client, + "connection": self.client.connection, + "display": self.client.display, + "print": self.log + } def execute(self, action): - if isinstance(action[0], str): - command = action[0] - if command in self.commands: - self.commands[command](*action[1:]) + try: + if isinstance(action[0], str): + command = action[0] + if command in self.commands: + self.commands[command](*action[1:]) + else: + raise InvalidCommandException("Invalid command '{}'".format(command)) else: - raise InvalidCommandException("Invalid command '{}'".format(command)) - else: - raise Exception("Command should be a string") + raise Exception("Command should be a string") + except Exception as e: + self.log(e) # Commands @@ -116,19 +135,24 @@ class CommandHandler: return self.input([action, selected]) - def eval(self, *texts): - text = " ".join(texts) - self.log(eval(text, {"self": self, "client": self.client, "connection": self.client.connection, "display": self.client.display})) + def eval(self, text): + self.log(eval(text, self.evalArgs)) - def exec(self, *texts): - text = " ".join(texts) - exec(text, {"self": self, "client": self.client, "connection": self.client.connection, "display": self.client.display}) + def exec(self, text): + exec(text, self.evalArgs) + + def hy(self, code): + if hy is None: + self.log(hyErr) + return + expr = hy.read_str(code) + self.log(hy.eval(expr, self.evalArgs)) def scrollChat(self, lines): self.client.display.scrollBack(lines) def json(self, text): - self.send(json.loads(text)) + self.execute(json.loads(text)) def ijson(self, text): self.input(json.loads(text)) diff --git a/asciifarm/client/inputhandler.py b/asciifarm/client/inputhandler.py index 6aed51c..8d99c40 100644 --- a/asciifarm/client/inputhandler.py +++ b/asciifarm/client/inputhandler.py @@ -1,7 +1,6 @@ import curses import curses.ascii -import shlex from .commandhandler import CommandHandler, InvalidCommandException from .keynames import nameFromKey @@ -35,7 +34,8 @@ class InputHandler: self.commandHandler.chat(message[1:]) else: try: - self.commandHandler.execute(shlex.split(message[1:])) + command, _sep, arg = message[1:].partition(' ') + self.commandHandler.execute([command, arg]) except InvalidCommandException as e: self.client.log(", ".join(e.args)) else: |
