1#!/usr/bin/env python3
 2
 3"""Implementation of Event specific exceptions."""
 4
 5
 6class InvalidEvent(Exception):
 7    """Event is invalid due and cannot be created from database, or XML file,
 8    or directly from code."""
 9
10    def __init__(self, event, message, elem=None):
11        """Args:
12            `event` (Event): The event throwing the error.
13            `message` (str): A custom message for the error
14            `elem` (Element): If not None, the exception message will show the file
15            and source line for the error.
16
17        Returns:
18            none
19
20        Raises:
21            none
22        """
23
24        super(InvalidEvent, self).__init__()
25        self.event = event
26        self.message = message
27        self.elem = elem
28
29    def __str__(self):
30        if self.elem is None:
31            location = ''
32
33        else:
34            location = '({filename}:{line}) '.format(
35                filename=self.elem.getroottree().docinfo.URL,
36                line=self.elem.sourceline)
37
38        if self.event is not None and self.event.event_id is not None:
39            return 'Event {event_id} {loc}({event_class}): {message}'.format(
40                event_id=self.event.event_id,
41                event_class=self.event.event_classname,
42                message=self.message,
43                loc=location)
44
45        else:
46            return location + self.message
47
48
49class NoSuchEventClass(Exception):
50    """Attempt to read an event from the database with an ID which does not exist."""
51
52    def __init__(self, classname):
53        super(NoSuchEventClass, self).__init__()
54        self.classname = classname
55
56    def __str__(self):
57        return 'Event class {cls} not known'.format(cls=self.classname)