# # Utility functions and objects # import string import imp import types import warlib def launch_bot(spec, bot, wrapClass, extraData, *args): "Launch the bot code." # get the bot code, its namespace, and run it code, fname = _get_bot_code(spec) dict = _get_bot_namespace(bot, wrapClass, extraData) exec code in dict # if the bot is started through a function (rather than simply running # the code object), then call that function. if fname: f = dict[fname] apply(f, args) def _get_bot_code(spec): "Build a code object for the bot." idx = string.rfind(spec, '.') if idx == -1: mname = spec fname = None else: mname = spec[:idx] fname = spec[idx+1:] idx = string.rfind(mname, '.') if idx == -1: path = None else: path = [ mname[:idx] ] mname = mname[idx+1:] stuff = imp.find_module(mname, path) return compile(stuff[0].read(), '__bot__', 'exec'), fname def _get_bot_namespace(bot, wrapClass, extraData): "Build a namespace that the bot will run within." dict = { } for name, value in vars(warlib).items(): if name[0] != '_': # For functions, we create a proxy that inserts the SimBot instance # as the first argument for all function calls. To do this, we use # a callable class instance that will hold the necessary state. if type(value) == types.FunctionType: value = wrapClass(bot, value, extraData) dict[name] = value # inject a status object into the namespace dict['status'] = warlib._Status(bot) ### we need to insert a custom __builtins__ into this dictionary to ### create a restricted execution environment ### Joe refers to MakeSafeGlobals() from poo.py as an example here... return dict