def __init__(self, application): self.app = application self.address_val = AddressValidator()
class SolipsisUiFactory(protocol.ServerFactory): protocol = SolipsisUiProtocol def __init__(self, application): self.app = application self.address_val = AddressValidator() # UI events def do_check(self, arg): return str(self.app._CheckNodeProxy()) def do_mem(self, arg): return self.app._MemDebug() # UI events in menu def do_about(self, arg): return self.app._About(arg) def do_create(self, arg): return self.app._CreateNode(arg) def do_connect(self, arg): stream = StringIO() args = self.address_val._Validate(arg) if not args: args = self.address_val._Validate(COMMANDS['connect'][1]) print >> stream, "no valid address. Default used..." host, port = args.groups() self.app.config_data.host = host self.app.config_data.port = int(port) deferred = defer.Deferred() deferred.addCallback( lambda res : self.transport.write("Connected\r\n")) print >> stream, self.app._OpenConnect(arg, deferred) return stream.getvalue() def do_disconnect(self, arg): return self.app._Disconnect(arg) def do_kill(self, arg): return self.app._Kill(arg) def do_reset(self, arg): return self.app._Reset(arg) def do_jump(self, arg): if not self.address_val._Validate(arg): arg = COMMANDS['jump'][1] return self.app._JumpNear(arg) def do_pref(self, arg): return self.app._Preferences(arg) def do_quit(self, arg): return self.app._Quit(arg) def do_display(self, arg): return self.app._DisplayNodeAddress(arg) # UI events in viewport def do_go(self, arg): stream = StringIO() # parsing position = arg.split(',') if len(position) != 2: position = COMMANDS['go'][1].split(',') print >> stream, "%d parameters instead of 2, using default"% len(position) # convert try: position = [float(coord)*2**128 for coord in position] except ValueError: position = [float(coord)*2**128 for coord in COMMANDS['go'][1].split(',')] print >> stream, "parameters expected as x,y. using default" print >> stream, self.app._LeftClickViewport(position) return stream.getvalue() def do_menu(self, arg): return self.app._RightClickViewport(arg) def do_hover(self, arg): # arg must be like 'x,y' try: position = [float(coord)*2**128 for coord in arg.split(',')] if len(position) == 2: return self.app._HoverViewport(position) else: return "enter position as 'x,y'" except ValueError: return "enter position as 'x,y'" def do_help(self, arg): str_stream = StringIO() if arg in ["", "all"]: pprint(COMMANDS, str_stream) elif arg in COMMANDS: print >> str_stream, COMMANDS[arg][0] else: print >> str_stream, "available commands are:" pprint(COMMANDS, str_stream) return str_stream.getvalue()