Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# (c) Stefan Countryman, August 2017 

2 

3""" 

4A FileHandler that indicates whether an event was tagged ADVOK via LVAlert. 

5""" 

6 

7import logging 

8from json import loads, JSONDecodeError 

9from llama.utils import GenerationError 

10from llama.filehandler import EventTriggeredFileHandler, JSONFile 

11 

12LOGGER = logging.getLogger(__name__) 

13 

14 

15@JSONFile.set_class_attributes 

16class LVAlertAdvok(EventTriggeredFileHandler, JSONFile): 

17 """ 

18 If the event was tagged ADVOK via an LVAlert, this file will 

19 contain the LVAlert JSON data. 

20 """ 

21 

22 FILENAME = 'lvalert_advok.json' 

23 

24 def _generate(self, payload): # pylint: disable=arguments-differ 

25 """Save an LVAlert to file in canonical form. 

26  

27 Parameters 

28 ---------- 

29 payload : str or dict 

30 If a ``str``, ``payload`` is converted into a ``dict`` anyway (in 

31 part to make sure it's valid JSON). The payload is then written to 

32 file in canonical form using ``JSONFile._write_json``. 

33 

34 Raises 

35 ------ 

36 GenerationError 

37 If ``payload`` is an invalid JSON string. 

38 """ 

39 if not hasattr(payload, 'keys'): 

40 try: 

41 payload = loads(payload) 

42 except JSONDecodeError as exc: 

43 msg = (f"Could not parse payload JSON: {exc}\nRAW " 

44 f"PAYLOAD:\n{payload}") 

45 LOGGER.error(msg) 

46 raise GenerationError(msg) 

47 self._write_json(payload)