Coverage for gws-app/gws/plugin/legend/remote/__init__.py: 0%

25 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""Remote legend.""" 

2 

3import gws 

4import gws.base.legend 

5import gws.base.ows.client 

6import gws.lib.image 

7 

8gws.ext.new.legend('remote') 

9 

10 

11class Config(gws.base.legend.Config): 

12 """External legend.""" 

13 

14 urls: list[gws.Url] 

15 """urls of externals legend images""" 

16 

17 

18class Object(gws.base.legend.Object): 

19 urls: list[str] 

20 

21 def configure(self): 

22 self.urls = self.cfg('urls') 

23 

24 def render(self, args=None): 

25 lros = [] 

26 

27 for url in self.urls: 

28 try: 

29 res = gws.base.ows.client.request.get_url(url, max_age=self.cacheMaxAge) 

30 if not res.content_type.startswith('image/'): 

31 raise gws.base.ows.client.Error(f'wrong content type {res.content_type!r}') 

32 img = gws.lib.image.from_bytes(res.content) 

33 lro = gws.LegendRenderOutput(image=img, size=img.size()) 

34 lros.append(lro) 

35 except gws.base.ows.client.Error: 

36 gws.log.exception(f'render_legend: download failed url={url!r}') 

37 

38 # NB even if there's only one image, it's not a bad idea to run it through the image converter 

39 return gws.base.legend.combine_outputs(gws.u.compact(lros), self.options)