Coverage for src/botadi/mokadi/mokadi_info.py: 68%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Defines results for mokadi.
4"""
5import os
8class MokadiInfo:
9 """
10 Defines results for Mokadi.
11 """
13 _allowed_status = {"error", "ok", "empty"}
15 def __init__(self, status: str, info="", error="", image=None, sound=None, url=None):
16 """
17 Constructor
19 @param status message (string)
20 @param info text to display
21 @param error error message
22 @param image image name
23 @param sound sound
24 @param url one URL
25 """
26 self._status = status
27 self._info = info
28 self._error = error
29 self._image = image
30 self._sound = sound
31 self._url = url
33 if not isinstance(status, str):
34 raise TypeError("status must be a string")
35 if not isinstance(info, str):
36 raise TypeError("info must be a string")
37 if not isinstance(error, str):
38 raise TypeError("error must be a string")
39 if status not in MokadiInfo._allowed_status:
40 raise ValueError("status must be in {0}".format(
41 MokadiInfo._allowed_status))
42 if image is not None and len(image) > 0:
43 if not image.startswith("http") and not os.path.exists(image):
44 raise FileNotFoundError(image)
45 if sound is not None and len(sound) > 0:
46 if not os.path.exists(sound):
47 raise FileNotFoundError(sound)
49 @property
50 def has_sound(self):
51 """
52 Tells if there is sound.
53 """
54 return hasattr(self, "_sound") and self._sound is not None
56 @property
57 def has_image(self):
58 """
59 Tells if there is an image.
60 """
61 return hasattr(self, "_image") and self._image is not None
63 @property
64 def has_url(self):
65 """
66 Tells if there is an image.
67 """
68 return hasattr(self, "_url") and self._url is not None
70 @property
71 def Url(self):
72 """retrieve the url"""
73 return self._url
75 @property
76 def image(self):
77 """retrieve the image"""
78 return self._image
80 @property
81 def sound(self):
82 """retrieve the sound"""
83 return self._sound
85 def __str__(self):
86 """
87 message to string
88 """
89 if self._error:
90 return "%s - %s" % (self._status, self._error)
91 else:
92 return "%s - %s" % (self._status, self._info)
94 def __repr__(self):
95 """
96 message to string
97 """
98 return "MokadiInfo('%s', '%s', '%s')" % (self._status, self._info.replace("'", "\\'"), self._error.replace("'", "\\'"))
100 def _repr_html_(self):
101 """
102 for notebooks
103 """
104 if self._status == "ok":
105 if self._image:
106 return "<i>%s</i><br /><img src=\"%s\" />" % (self._info, self._image)
107 else:
108 return "<i>%s</i>" % self._info
109 else:
110 return "<b>%s<b> <i>%s</i>" % (self._status, self._info)
112 @property
113 def info(self):
114 """
115 property
116 """
117 return self._info
119 @property
120 def error(self):
121 """
122 property
123 """
124 return self._error
126 @property
127 def status(self):
128 """
129 property
130 """
131 return self._status