From 5ee7e534ffc916e22b57ec697169e701ba05bd1c Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 29 Dec 2020 10:39:20 +0800 Subject: [PATCH] bugfix --- kivyblocks/i18n.py | 22 ++++++++++++++++++++++ kivyblocks/osc_server.py | 37 +++++++++++++++++++++++++++++++++++++ kivyblocks/register.py | 2 ++ requirements.txt | 1 + 4 files changed, 62 insertions(+) create mode 100644 kivyblocks/osc_server.py diff --git a/kivyblocks/i18n.py b/kivyblocks/i18n.py index 6339c55..1293c5e 100644 --- a/kivyblocks/i18n.py +++ b/kivyblocks/i18n.py @@ -1,3 +1,6 @@ +import os +import codecs + import locale from kivy.app import App from kivy.properties import StringProperty @@ -17,9 +20,28 @@ class I18n: def addI18nWidget(self,w): self.i18nWidgets.append(w) + def loadI18nFromI18nFolder(self, lang): + config = gtConfig() + fpath = os.path.join(config.i18n_folder,lang,'msg.txt') + with codecs.open(fpath,'r','utf-8') as f: + line = f.readline() + d = {} + while line: + if line.startswith('#'): + line = readline() + continue + k,v = line.split(':',1) + d.update({k:v}) + line = readline() + return d + def loadI18n(self,lang): app = App.get_running_app() config = getConfig() + if config.i18n_folder: + self.kvlang[lang] = self.loadI18nFromFolder(lang) + return + url = '%s%s/%s' % (config.uihome, config.i18n_url, lang) hc = HttpClient() d = hc.get(url) diff --git a/kivyblocks/osc_server.py b/kivyblocks/osc_server.py new file mode 100644 index 0000000..cbddd39 --- /dev/null +++ b/kivyblocks/osc_server.py @@ -0,0 +1,37 @@ +from functools import partial +from kivy.event import EventDispatcher +from kivy.clock import Clock +from oscpy.server import OSCThreadServer +from oscpy.client import OSCClient + +class OSCServer(EventDispatcher): + def __init__(self,addr='0.0.0.0', port=60405,apis=[]): + self.port = port + self.addr = addr + self.osc_server = OSCThreadServer() + self.osc_server.listen(self.addr,port=self.port,default=True) + EventDispatcher.__init__(self) + for api in apis: + self.register_event_type(api) + f = partial(self.handle, api) + setattr(self, api, f) + api_f = partial(self.apihandle,api) + bstr = '/%s' % api + bstr.encode('utf-8') + self.osc_server.bind(bstr, api_f) + + def handle(self,api,o,*args): + print(api, *args) + + def apihandle(self, api, *args): + self.dispatch(api,*args) + + 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) + diff --git a/kivyblocks/register.py b/kivyblocks/register.py index 1b360b3..aef28d9 100644 --- a/kivyblocks/register.py +++ b/kivyblocks/register.py @@ -15,8 +15,10 @@ from .tab import TabsPanel from .qrdata import QRCodeWidget # from .kivycamera import KivyCamera from .filebrowser import FileLoaderBrowser +from .osc_server import OSCServer r = Factory.register +r('OSCServer',OSCServer) r('DataGrid',DataGrid) r('FileLoaderBrowser',FileLoaderBrowser) # r('KivyCamera',KivyCamera) diff --git a/requirements.txt b/requirements.txt index 7b8dc18..2de3061 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ pillow requests plyer python-osc +oscpy