Merge "CLI for OSM"
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / tasklets / rwlaunchpad / message.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import logging
19 import time
20
21
22 class MessageException(Exception):
23 def __init__(self, msg):
24 if not isinstance(msg, Message):
25 raise ValueError("{} is not a message".format(msg.__class__.__name__))
26
27 self.msg = msg
28
29
30 class Message(object):
31 """
32 Messages are events that describe stages of the onboarding process, and
33 any event that may occur during the onboarding process.
34 """
35
36 def __init__(self, level, name, text):
37 self._level = level
38 self._name = name
39 self._text = text
40 self._timestamp = time.time()
41
42 def __repr__(self):
43 return "{} {}:{}:{}".format(
44 self.timestamp,
45 logging._levelNames.get(self.level, self.level),
46 self.name,
47 self.text,
48 )
49
50 @property
51 def level(self):
52 return self._level
53
54 @property
55 def name(self):
56 return self._name
57
58 @property
59 def text(self):
60 return self._text
61
62 @property
63 def timestamp(self):
64 return self._timestamp
65
66 def log(self, logger):
67 logger.log(self.level, self.text)
68
69
70 class WarningMessage(Message):
71 """
72 A warning is a message that does not prevent the onboarding process for
73 continuing, but may not be the intention of the user when they initiated
74 the process.
75 """
76
77 def __init__(self, name, text):
78 super().__init__(logging.WARNING, name, text)
79
80
81 class ErrorMessage(Message):
82 """
83 An error message alerts the user to an event that prevent the continuation
84 of the onboarding process.
85 """
86
87 def __init__(self, name, text):
88 super().__init__(logging.ERROR, name, text)
89
90
91 class StatusMessage(Message):
92 """
93 A status message informs the user of an expected stage in the onboarding
94 process.
95 """
96
97 def __init__(self, name, text):
98 super().__init__(logging.INFO, name, text)
99
100
101 class FilenameMessage(Message):
102 """
103 A status message informs the user of a download file.
104 """
105
106 def __init__(self, filename):
107 super().__init__(logging.INFO, 'filename', filename)
108
109
110 class Logger(object):
111 """
112 This class is used to augment a python logger class so that messages can be
113 passed to it. Messages are recorded so that the uploader application can
114 provide this information to the client, and the messages are also recorded
115 on the server via the standard logging facilities.
116 """
117
118 def __init__(self, logger, messages):
119 self._rift_logger = logger
120 self._messages = messages
121
122 @property
123 def messages(self):
124 return self._messages
125
126 def message(self, msg):
127 msg.log(self._rift_logger)
128 self._messages.append(msg)
129
130 def __getattr__(self, name):
131 """ Return the rift logger attribute
132
133 By returning the rift logger attribute back to the client,
134 the line logged by rwlogger corresponds to the actual file/line
135 logged by the application instead of one in this class. This makes
136 debugging easier and prevents rwlogd from inadvertantly triggering
137 dup detection (which uses event & line information).
138 """
139 return getattr(self._rift_logger, name)
140
141
142
143 class OnboardError(ErrorMessage):
144 def __init__(self, msg):
145 super().__init__("onboard-error", msg)
146
147
148 class OnboardWarning(ErrorMessage):
149 def __init__(self, msg):
150 super().__init__("onboard-warning", msg)
151
152
153 class OnboardDescriptorValidation(StatusMessage):
154 def __init__(self):
155 super().__init__("onboard-dsc-validation", "descriptor validation")
156
157
158 class OnboardDescriptorTimeout(OnboardError):
159 def __init__(self):
160 super().__init__("descriptor timeout")
161
162
163 class OnboardDescriptorError(OnboardError):
164 def __init__(self, filename):
165 super().__init__("unable to onboard {}".format(filename))
166
167
168 class OnboardDescriptorFormatError(OnboardError):
169 def __init__(self, filename):
170 super().__init__("{} has unrecognized format".format(filename))
171
172
173 class OnboardMissingContentType(OnboardError):
174 def __init__(self):
175 super().__init__("missing content-type header")
176
177
178 class OnboardUnsupportedMediaType(OnboardError):
179 def __init__(self):
180 super().__init__("multipart/form-data required")
181
182
183 class OnboardMissingContentBoundary(OnboardError):
184 def __init__(self):
185 super().__init__("missing content boundary")
186
187
188 class OnboardMissingTerminalBoundary(OnboardError):
189 def __init__(self):
190 super().__init__("Unable to find terminal content boundary")
191
192
193 class OnboardUnreadableHeaders(OnboardError):
194 def __init__(self):
195 super().__init__("Unable to read message headers")
196
197
198 class OnboardUnreadablePackage(OnboardError):
199 def __init__(self):
200 super().__init__("Unable to read package")
201
202
203 class OnboardExtractionError(OnboardError):
204 def __init__(self):
205 super().__init__("Unable to extract package contents")
206
207
208 class OnboardImageUploadError(OnboardError):
209 def __init__(self, message=""):
210 super().__init__("Unable to upload images: %s" % message)
211
212
213 class OnboardMissingChecksumsFile(OnboardError):
214 def __init__(self):
215 super().__init__("Package does not contain checksums.txt")
216
217
218 class OnboardChecksumMismatch(OnboardError):
219 def __init__(self, filename):
220 super().__init__("checksum mismatch for {}".format(filename))
221
222
223 class OnboardDescriptorExistsError(OnboardError):
224 def __init__(self, descriptor_id):
225 super().__init__("descriptor id {} already onboarded".format(descriptor_id))
226
227
228
229 class OnboardStart(StatusMessage):
230 def __init__(self):
231 super().__init__("onboard-started", "onboarding process started")
232
233
234 class OnboardDescriptorOnboard(StatusMessage):
235 def __init__(self):
236 super().__init__("onboard-dsc-onboard", "onboarding descriptors")
237
238
239 class OnboardSuccess(StatusMessage):
240 def __init__(self):
241 super().__init__("onboard-success", "onboarding process successfully completed")
242
243
244 class OnboardFailure(StatusMessage):
245 def __init__(self):
246 super().__init__("onboard-failure", "onboarding process failed")
247
248
249 class OnboardPackageUpload(StatusMessage):
250 def __init__(self):
251 super().__init__("onboard-pkg-upload", "uploading package")
252
253
254 class OnboardImageUpload(StatusMessage):
255 def __init__(self):
256 super().__init__("onboard-img-upload", "uploading image")
257
258
259 class OnboardPackageValidation(StatusMessage):
260 def __init__(self):
261 super().__init__("onboard-pkg-validation", "package contents validation")
262
263
264
265 class UpdateError(ErrorMessage):
266 def __init__(self, msg):
267 super().__init__("update-error", msg)
268
269
270 class UpdateMissingContentType(UpdateError):
271 def __init__(self):
272 super().__init__("missing content-type header")
273
274
275 class UpdateUnsupportedMediaType(UpdateError):
276 def __init__(self):
277 super().__init__("multipart/form-data required")
278
279
280 class UpdateMissingContentBoundary(UpdateError):
281 def __init__(self):
282 super().__init__("missing content boundary")
283
284
285 class UpdateDescriptorError(UpdateError):
286 def __init__(self, filename):
287 super().__init__("unable to update {}".format(filename))
288
289
290 class UpdatePackageNotFoundError(UpdateError):
291 def __init__(self, descriptor_id):
292 super().__init__("package {} not found".format(descriptor_id))
293
294
295 class UpdateDescriptorFormatError(UpdateError):
296 def __init__(self, filename):
297 super().__init__("{} has unrecognized format".format(filename))
298
299
300 class UpdateExtractionError(UpdateError):
301 def __init__(self):
302 super().__init__("Unable to extract package contents")
303
304
305 class UpdateDescriptorTimeout(UpdateError):
306 def __init__(self):
307 super().__init__("descriptor timeout")
308
309
310 class UpdateUnreadableHeaders(UpdateError):
311 def __init__(self):
312 super().__init__("Unable to read message headers")
313
314
315 class UpdateUnreadablePackage(UpdateError):
316 def __init__(self):
317 super().__init__("Unable to read package")
318
319
320 class UpdateChecksumMismatch(UpdateError):
321 def __init__(self, filename):
322 super().__init__("checksum mismatch for {}".format(filename))
323
324
325 class UpdateImageUploadError(UpdateError):
326 def __init__(self):
327 super().__init__("Unable to upload images")
328
329
330 class UpdateStart(StatusMessage):
331 def __init__(self):
332 super().__init__("update-started", "update process started")
333
334
335 class UpdateSuccess(StatusMessage):
336 def __init__(self):
337 super().__init__("update-success", "updating process successfully completed")
338
339
340 class UpdateFailure(StatusMessage):
341 def __init__(self):
342 super().__init__("update-failure", "updating process failed")
343
344
345 class UpdatePackageUpload(StatusMessage):
346 def __init__(self):
347 super().__init__("update-pkg-upload", "uploading package")
348
349
350 class UpdateDescriptorUpdate(StatusMessage):
351 def __init__(self):
352 super().__init__("update-dsc-onboard", "updating descriptors")
353
354
355 class UpdateDescriptorUpdated(StatusMessage):
356 def __init__(self):
357 super().__init__("update-dsc-updated", "updated descriptors")
358
359
360