|
1 | 1 | import os |
2 | 2 | import time |
| 3 | +import unicodedata |
| 4 | + |
3 | 5 | import yaml |
4 | 6 |
|
5 | 7 | from octoprint_mrbeam.mrb_logger import mrb_logger |
@@ -356,36 +358,59 @@ def _load_usage_data(self): |
356 | 358 | "Trying to recover from _backup_file file: %s", self._backup_file |
357 | 359 | ) |
358 | 360 | recovery_try = True |
359 | | - if os.path.isfile(self._backup_file): |
| 361 | + try: |
| 362 | + with open(self._backup_file, "r") as stream: |
| 363 | + data = yaml.safe_load(stream) |
| 364 | + if self._validate_data(data): |
| 365 | + data["restored"] = ( |
| 366 | + data["restored"] + 1 if "restored" in data else 1 |
| 367 | + ) |
| 368 | + self._usage_data = data |
| 369 | + success = True |
| 370 | + self._write_usage_data() |
| 371 | + self._logger.info("Recovered from _backup_file file. Yayy!") |
| 372 | + except yaml.constructor.ConstructorError: |
360 | 373 | try: |
361 | | - data = None |
362 | | - with open(self._backup_file, "r") as stream: |
363 | | - data = yaml.safe_load(stream) |
364 | | - if self._validate_data(data): |
365 | | - data["restored"] = ( |
366 | | - data["restored"] + 1 if "restored" in data else 1 |
367 | | - ) |
368 | | - self._usage_data = data |
369 | | - success = True |
370 | | - self._write_usage_data() |
371 | | - self._logger.info("Recovered from _backup_file file. Yayy!") |
372 | | - except: |
373 | | - self._logger.error("Can't read _backup_file file.") |
| 374 | + success = self._repair_backup_usage_data() |
| 375 | + except Exception: |
| 376 | + self._logger.error("Repair of the _backup_file failed.") |
| 377 | + except OSError: |
| 378 | + self._logger.error("There is no _backup_file file.") |
| 379 | + except yaml.YAMLError: |
| 380 | + self._logger.error("There was a YAMLError with the _backup_file file.") |
| 381 | + except: |
| 382 | + self._logger.error("Can't read _backup_file file.") |
374 | 383 |
|
375 | 384 | if not success: |
376 | 385 | self._logger.warn("Resetting usage data. (marking as incomplete)") |
377 | 386 | self._usage_data = self._get_usage_data_template() |
378 | 387 | if recovery_try: |
379 | 388 | self._write_usage_data() |
380 | 389 |
|
| 390 | + def _repair_backup_usage_data(self): |
| 391 | + success = False |
| 392 | + with open(self._backup_file, "r") as stream: |
| 393 | + data = yaml.load(stream) |
| 394 | + if self._validate_data(data): |
| 395 | + if isinstance(data["version"], unicode): |
| 396 | + data["version"] = unicodedata.normalize('NFKD', data["version"]).encode('ascii', 'ignore') |
| 397 | + data["restored"] = ( |
| 398 | + data["restored"] + 1 if "restored" in data else 1 |
| 399 | + ) |
| 400 | + self._usage_data = data |
| 401 | + success = True |
| 402 | + self._write_usage_data() |
| 403 | + self._logger.info("Could repair _backup_file file. Yayy!") |
| 404 | + return success |
| 405 | + |
381 | 406 | def _write_usage_data(self, file=None): |
382 | 407 | self._usage_data["version"] = self._plugin_version |
383 | 408 | self._usage_data["ts"] = time.time() |
384 | 409 | self._usage_data["serial"] = self._device_serial |
385 | 410 | file = self._storage_file if file is None else file |
386 | 411 | try: |
387 | 412 | with open(file, "w") as outfile: |
388 | | - yaml.dump(self._usage_data, outfile, default_flow_style=False) |
| 413 | + yaml.safe_dump(self._usage_data, outfile, default_flow_style=False) |
389 | 414 | except: |
390 | 415 | self._logger.exception("Can't write file %s due to an exception: ", file) |
391 | 416 |
|
|
0 commit comments