def clear_cos_connection_error(self, msg=""): self.clear() self.error_msg = PLANTUML_VIEW_INTERNET_FAIL % msg # print(plant_uml_create_png_and_return_image_url.cache_info()) plant_uml_create_png_and_return_image_url_async.cache_clear() url_to_data.cache_clear() self.Refresh()
async def ViewImage(self, thefile="", url=""): """Loads url or file and sets .bmp and .bmp_transparent_ori, the img is discarded""" self.error_msg = None if thefile: img = wx.Image(thefile, wx.BITMAP_TYPE_ANY) bmp = wx.Bitmap(img) # ANDY added 2019 elif url: # print(url_to_data.cache_info()) try: data, status = await url_to_data(url) log.info(f"(2nd, image grabbing) Response from plant_uml_server status_code {status}") except asyncio.TimeoutError as e: # there is no string repr of this exception self.clear_cos_connection_error(msg="(timeout)") url_to_data.cache_clear() # so if retry you won't get the same error log.error("TimeoutError getting plantuml IMAGE") return if status != 200: self.clear_cos_connection_error(msg=f"(bad response {status})") log.error(f"Error getting plantuml IMAGE, (bad response {status})") return stream = io.BytesIO(data) img = wx.Image(stream) bmp = wx.Bitmap(img) # try: # bmp = img.ConvertToBitmap() # except Exception as e: # print(e) # return self.maxWidth, self.maxHeight = bmp.GetWidth(), bmp.GetHeight() self.maxHeight += BMP_EXTRA_MARGIN # stop bitmaps getting slightly clipped # dbg(bmp) # ANDY bmp.HasAlpha() does not work, since wx.Image has this method but wx.Bitmap # does not. But Bitmaps have some alpha channel concepts in them too...? # Render bmp to a second white bmp to remove transparency effects # if False and bmp.HasAlpha(): if img.HasAlpha(): self.bmp_transparent_ori = bmp bmp2 = wx.Bitmap(bmp.GetWidth(), bmp.GetHeight()) dc = wx.MemoryDC() dc.SelectObject(bmp2) dc.Clear() dc.DrawBitmap(bmp, 0, 0, True) dc.SelectObject(wx.NullBitmap) self.bmp = bmp2 else: self.bmp_transparent_ori = None self.bmp = bmp
def user_aborted(self): self.error_msg = PLANTUML_VIEW_USER_ABORT plant_uml_create_png_and_return_image_url_async.cache_clear() url_to_data.cache_clear() self.Refresh()