From 35b6d7a5658cbff424a4462e7578934a9a17faf0 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sun, 26 Apr 2020 15:16:31 +0800 Subject: [PATCH] bugfix --- kivyblocks/baseWidget.py | 6 +++- kivyblocks/hostimage.py | 35 ++++++++++++++++++++ kivyblocks/paging.py | 15 ++------- kivyblocks/register.py | 2 ++ kivyblocks/threadcall.py | 69 ++++++++++++++++++++++++++-------------- 5 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 kivyblocks/hostimage.py diff --git a/kivyblocks/baseWidget.py b/kivyblocks/baseWidget.py index 9028e36..07bc132 100755 --- a/kivyblocks/baseWidget.py +++ b/kivyblocks/baseWidget.py @@ -86,7 +86,8 @@ class PressableLabel(ButtonBehavior, Text): class HTTPDataHandler(EventDispatcher): def __init__(self, url,method='GET',params={}, headers={}, - files={} + files={}, + stream=False ): EventDispatcher.__init__(self) self.url = url @@ -94,6 +95,7 @@ class HTTPDataHandler(EventDispatcher): self.params = params self.headers = headers self.files=files + self.stream=stream self.hc = App.get_running_app().hc self.register_event_type('on_success') self.register_event_type('on_error') @@ -129,6 +131,8 @@ class HTTPDataHandler(EventDispatcher): self.hc(self.url,method=self.method, params=p, headers=h, + files=self.files, + stream=self.stream, callback=self.onSuccess, errback=self.onError) diff --git a/kivyblocks/hostimage.py b/kivyblocks/hostimage.py new file mode 100644 index 0000000..156b006 --- /dev/null +++ b/kivyblocks/hostimage.py @@ -0,0 +1,35 @@ +import tempfile +from kivyblocks.baseWidget import HTTPDataHandler +from kivyblocks.utils import blocksImage, absurl + +from kivy.uix.image import Image + +class HostImage(Image): + def __init__(self, target,**kwargs): + self.options = kwargs + self.target = target + kwargs['source'] = blocksImage('running.gif') + url = kwargs.get('url') + del kwargs['url'] + super().__init__(**kwargs) + self.downloadImage(url) + + def downloadImage(url): + realurl = absurl(url,self.target.parenturl) + loader = HTTPDataHandler(url,stream=True) + loader.bind(on_success=self.createTmpfile) + loader.bind(on_failed=self.showBadImage) + loader.handle() + + def showBadImage(self,o,e): + self.source = blocksImage('break.png') + + def createTmpfile(self,o,resp): + fn = tempfile.NamedTemporaryFile(delete=True) + with open(fn, 'wb') as f: + for chunk in r.iter_content(chunk_size=8192): + if chunk: # filter out keep-alive new chunks + f.write(chunk) + # f.flush() + self.source = fn + diff --git a/kivyblocks/paging.py b/kivyblocks/paging.py index 1d8627f..e8e5ef5 100644 --- a/kivyblocks/paging.py +++ b/kivyblocks/paging.py @@ -152,7 +152,7 @@ class PageLoader(EventDispatcher): filter } events: -'on_bufferraise': erase +'on_deletepage': erase """ class RelatedLoader(PageLoader): @@ -244,8 +244,6 @@ class RelatedLoader(PageLoader): class Paging(PageLoader): def __init__(self,**options): PageLoader.__init__(self,**options) - self.adder = options.get('adder') - self.clearer = options.get('clearer') self.target = options.get('target') self.init() @@ -254,6 +252,7 @@ class Paging(PageLoader): kwargs['size_hint_y'] = None kwargs['height'] = CSize(2) kwargs['orientation'] = 'horizontal' + kwargs['spacing'] = CSize(1) self.widget = BoxLayout(**kwargs) self.b_f = PagingButton(text="|<") self.b_p = PagingButton(text="<") @@ -270,16 +269,6 @@ class Paging(PageLoader): if self.filter: self.widget.add_widget(self.filter) - def show_page(self,o,d): - super().show_page(o,data) - d = DictObject(**d) - self.total_cnt = d.total - self.calculateTotalPage() - self.clearer() - for r in d.rows: - self.adder(r) - self.loading = False - def loadFirstPage(self,o=None): if self.curpage == 1: print('return not loading') diff --git a/kivyblocks/register.py b/kivyblocks/register.py index e63e77d..b47f90d 100644 --- a/kivyblocks/register.py +++ b/kivyblocks/register.py @@ -7,6 +7,7 @@ from .vplayer import VPlayer from .form import Form, StrSearchForm from .boxViewer import BoxViewer from .pagescontainer import PageContainer +from .hostimage import HostImage r = Factory.register r('PageContainer', PageContainer) @@ -36,6 +37,7 @@ r('PressableLabel', PressableLabel) r('Tree',Tree) r('TextTree',TextTree) r('PopupMenu',PopupMenu) +r('HostImage',HostImage) if platform == 'android': r('PhoneButton',PhoneButton) r('AWebView',AWebView) diff --git a/kivyblocks/threadcall.py b/kivyblocks/threadcall.py index ad67661..ea8c73f 100644 --- a/kivyblocks/threadcall.py +++ b/kivyblocks/threadcall.py @@ -89,15 +89,14 @@ class HttpClient: pre = '/'.join(parts) return pre - def webcall(self,url,method="GET",params={},files={},headers={}): + def _webcall(self,url,method="GET", + params={}, + files={}, + headers={} + stream=False): app = App.get_running_app() domain = self.url2domain(url) sessionid = hostsessions.get(domain,None) - print('hostsessions=', hostsessions, - 'sessionid=',sessionid, - 'domain=',domain, - 'url=',url - ) if sessionid: headers.update({'session':sessionid}) elif app.getAuthHeader(): @@ -114,21 +113,8 @@ class HttpClient: if resp.status_code == 200: h = resp.headers.get('Set-Cookie',None) if h: - print('*************set-Cookie=',h,'domain=',domain) sessionid = h.split(';')[0] hostsessions[domain] = sessionid - try: - data = resp.json() - if type(data) != type({}): - return data - status = data.get('status',None) - if status is None: - return data - if status == 'OK': - return data['data'] - return data - except: - return resp.text if resp.status_code == 401: print('NeedLogin:',url) raise NeedLogin @@ -136,12 +122,46 @@ class HttpClient: if resp.status_code == 403: raise InsufficientPrivilege - print('Error', url, method, - params, resp.status_code, - type(resp.status_code)) - raise HTTPError(resp.status_code) + if resp.status_code != 200: + print('Error', url, method, + params, resp.status_code, + type(resp.status_code)) + raise HTTPError(resp.status_code) + return resp - def __call__(self,url,method="GET",params={},headers={},files={},callback=None,errback=None): + + def webcall(self,url,method="GET", + params={}, + files={}, + headers={}, + stream=False): + resp = self._webcall(url,method=method, + params=params, + files=files, + headers=headers, + stream=stream) + if stream: + return resp + try: + data = resp.json() + if type(data) != type({}): + return data + status = data.get('status',None) + if status is None: + return data + if status == 'OK': + return data['data'] + return data + except: + return resp.text + + def __call__(self,url,method="GET", + params={}, + headers={}, + files={}, + stream=False, + callback=None, + errback=None): def cb(t,resp): return resp @@ -161,6 +181,7 @@ class HttpClient: "method":method, "params":params, "files":files, + "stream":stream, "headers":headers } self.workers.add(self.webcall,callback,errback,kwargs=kwargs)