Mercurial > pyshada
changeset 24:81cbe3a35e18
Implement def command for defining new commands
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Fri, 01 Apr 2011 19:26:24 +0800 |
parents | 2bf87f8e1f3f |
children | c516d8593c0e |
files | shada/shhelper.py |
diffstat | 1 files changed, 28 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/shada/shhelper.py Fri Apr 01 00:54:10 2011 +0800 +++ b/shada/shhelper.py Fri Apr 01 19:26:24 2011 +0800 @@ -1,6 +1,32 @@ from shada import comm def def_cmd(args, env, session): + import types + + if len(args) != 1: + session.send('You should give an argument for command name.\n') + return + + cmd_name = args[0] + + lines = [] + while True: + yield + line = session.recv() + if line is None: + break + lines.append(line) + pass + + full_txt = '\t'.join(lines) + full_txt = ('def %s(args, env, session):\n\t' % (cmd_name)) + full_txt + + mcode = compile(full_txt, '<shhelper>', 'single') # compile the module + if mcode: + mfunc = types.FunctionType(mcode, ns) + mfunc() # run code of the module + session.send('command %s was defined\n' % (cmd_name)) + pass pass def cmds(args, env, session): @@ -38,7 +64,8 @@ if __name__ == '__main__': import os - ns = {'def': def_cmd, 'cmds': cmds, 'values': values, 'set': set, + ns = {'__builtins__': __builtins__, + 'def': def_cmd, 'cmds': cmds, 'values': values, 'set': set, 'close': close} server = comm.server(ns) server.listen()