From 13fdea6429c8b3f25fa1279d11a24f4086b0df59 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sat, 23 Jan 2021 15:55:38 +0800 Subject: [PATCH] bugfix --- kivyblocks/blocks.py | 17 ++++++-------- kivyblocks/osc_server.py | 49 ++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/kivyblocks/blocks.py b/kivyblocks/blocks.py index 159dbc0..6a712e1 100755 --- a/kivyblocks/blocks.py +++ b/kivyblocks/blocks.py @@ -298,7 +298,7 @@ class Blocks(EventDispatcher): raise Exception(desc) widgetClass = desc['widgettype'] - opts = desc.get('options',{}).copy() + opts = self.valueExpr(desc.get('options',{}).copy()) widget = None try: klass = wrap_ready(widgetClass) @@ -329,26 +329,23 @@ class Blocks(EventDispatcher): continue setattr(widget,k,w) continue - setattr(widget,k,v) + setattr(widget,k,self.valueExpr(v,\ + localnamespace={'self':widget})) def build_rest(self, widget,desc,t=None): self.subwidget_total = len(desc.get('subwidgets',[])) self.subwidgets = [ None for i in range(self.subwidget_total)] pos = 0 for pos,sw in enumerate(desc.get('subwidgets',[])): - params={ - 'desc':desc, - 'widget':widget, - 'pos':pos - } b = Blocks() kw = sw.copy() w = b.widgetBuild(kw) widget.add_widget(w) for b in desc.get('binds',[]): - kw = b.copy() - self.buildBind(widget,b) + kw = self.valueExpr(b.copy(), \ + localnamespace={'self':widget}) + self.buildBind(widget,kw) def buildBind(self,widget,desc): wid = desc.get('wid','self') @@ -545,7 +542,7 @@ class Blocks(EventDispatcher): desc,type(desc)) return None - desc = self.valueExpr(desc) + # desc = self.valueExpr(desc) widget = self.w_build(desc) self.dispatch('on_built',widget) if hasattr(widget,'ready'): diff --git a/kivyblocks/osc_server.py b/kivyblocks/osc_server.py index cbddd39..1603339 100644 --- a/kivyblocks/osc_server.py +++ b/kivyblocks/osc_server.py @@ -5,33 +5,58 @@ from oscpy.server import OSCThreadServer from oscpy.client import OSCClient class OSCServer(EventDispatcher): - def __init__(self,addr='0.0.0.0', port=60405,apis=[]): + def __init__(self,addr='0.0.0.0', + port=60405, + apis=[]): self.port = port self.addr = addr + self.clients = [] self.osc_server = OSCThreadServer() self.osc_server.listen(self.addr,port=self.port,default=True) EventDispatcher.__init__(self) + apis.append('broadcast') for api in apis: - self.register_event_type(api) + event_name = 'on_%s' % api f = partial(self.handle, api) - setattr(self, api, f) + setattr(self, event_name, f) + self.register_event_type(event_name) api_f = partial(self.apihandle,api) bstr = '/%s' % api bstr.encode('utf-8') self.osc_server.bind(bstr, api_f) + def info(self): + return { + "address":self.osc_server.getaddress() + } + + def __del__(self): + self.osc_server.stop_all() + self.osc_server.terminate_server() + self.osc_server.join_server() + def handle(self,api,o,*args): print(api, *args) def apihandle(self, api, *args): - self.dispatch(api,*args) + data = json.loads(args[0]) + sock, ip_address, response_port = self.osc_server.get_sender() + self.dispatch(api, + {'address':address, + 'data':data + }) + if api == 'broadcast': + return + if not address in self.clients: + self.clients.append([ip_address, response_port]) - def sendMessage(self,api='', addr=None, port=None, *args): - try: - bstr = '/%s' % api - bstr = bstr.encode('utf-8') - client = OSCClient(addr, port) - client.sendMessage(bstr, args) - except: - print('sendMessage error:',api, addr,port,args) + def send_message(self,api, data, addr, port): + data = json.dumps(data) + bstr = '/%s' % api + bstr = bstr.encode('utf-8') + self.osc_server.send_message(b'/broadcast', [data], addr, port) + def broadcast(self, data): + for address in self.clients: + self.osc_server.send_message('broadcast', data, *address) +