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

28 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Remote legend. 

2 

3Downloads legend images from URLs and combines them into one image. 

4Downloads are cached for ``cacheMaxAge``. Responses that are not images are 

5logged and skipped. 

6 

7Example:: 

8 

9 legend { 

10 type "remote" 

11 urls ["https://example.com/legend_1.png", "https://example.com/legend_2.png"] 

12 } 

13""" 

14 

15import gws 

16import gws.base.legend 

17import gws.base.ows.client 

18import gws.lib.image 

19 

20 

21@gws.ext.config.legend('remote') 

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

23 """Legend images loaded from external URLs.""" 

24 

25 urls: list[gws.Url] 

26 """URLs of external legend images.""" 

27 

28 

29@gws.ext.object.legend('remote') 

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

31 """Remote legend.""" 

32 

33 urls: list[str] 

34 """URLs of the legend images.""" 

35 

36 def configure(self): 

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

38 

39 def render(self, args=None): 

40 lro_list = [] 

41 

42 def _fetch(url): 

43 res = gws.base.ows.client.request.get_url(url) 

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

45 raise gws.ExternalServiceError(f'wrong content type {res.content_type!r}') 

46 return res.content 

47 

48 for url in self.urls: 

49 try: 

50 content = gws.u.get_cached_object( 

51 f'legend_{gws.u.sha256(url)}', 

52 self.cacheMaxAge, 

53 lambda: _fetch(url), 

54 ) 

55 img = gws.lib.image.from_bytes(content) 

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

57 lro_list.append(lro) 

58 except gws.ExternalServiceError: 

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

60 

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

62 return gws.base.legend.combine_outputs(lro_list, self.options)