Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/components/synoptic.py: 46%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.5, created at 2024-11-15 02:12 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3import io 

4import logging 

5from flask import Response 

6from daiquiri.core import marshal 

7from daiquiri.core.components import Component, ComponentResource 

8from daiquiri.core.schema import ErrorSchema 

9from daiquiri.resources.utils import get_resource_provider 

10 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15class SynopticResource(ComponentResource): 

16 @marshal(out=[[404, ErrorSchema(), "Could not find svg"]]) 

17 def get(self, **kwargs): 

18 """Get a synoptic image""" 

19 try: 

20 synoptic = self._parent.get_synoptic(kwargs["sid"]) 

21 if synoptic: 

22 provider = get_resource_provider() 

23 with provider.open_resource(synoptic, "b") as svg: 

24 data = io.BytesIO(svg.read()) 

25 resp = Response(response=data, mimetype="image/svg+xml", status=200) 

26 resp.headers.add("Content-Length", data.getbuffer().nbytes) 

27 

28 return resp 

29 

30 else: 

31 return {"error": "Could not find synoptic svg"}, 400 

32 

33 except RuntimeError: 

34 logger.exception("Could not find synoptic") 

35 return {"error": "Could not find synoptic svg"}, 400 

36 

37 

38class Synoptic(Component): 

39 _config_export = ["synoptics"] 

40 

41 def setup(self): 

42 self.register_route(SynopticResource, "/<int:sid>") 

43 

44 def get_synoptic(self, sid): 

45 if sid < 0 or sid > len(self._config["synoptics"]) - 1: 

46 return 

47 

48 provider = get_resource_provider() 

49 fname = self._config["synoptics"][sid]["synoptic"] 

50 resource = provider.resource("synoptic", fname) 

51 return resource