Skip to content

Commit

Permalink
SW-1269 usage file and backup file will break if version number is no…
Browse files Browse the repository at this point in the history
…t text only #1491
  • Loading branch information
Josef-MrBeam authored May 19, 2022
2 parents b1fdcce + 0a5a843 commit efbd0d2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
4 changes: 4 additions & 0 deletions octoprint_mrbeam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import datetime
import collections
import logging
import unicodedata
from subprocess import check_output

import octoprint.plugin
Expand All @@ -38,6 +39,9 @@
from ._version import get_versions

__version__ = get_versions()["version"]
if isinstance(__version__, unicode):
__version__ = unicodedata.normalize('NFKD', __version__).encode('ascii', 'ignore')

del get_versions

from octoprint_mrbeam.iobeam.iobeam_handler import ioBeamHandler, IoBeamEvents
Expand Down
62 changes: 47 additions & 15 deletions octoprint_mrbeam/analytics/usage_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import time
import unicodedata

import yaml

from octoprint_mrbeam.mrb_logger import mrb_logger
Expand Down Expand Up @@ -356,36 +358,66 @@ def _load_usage_data(self):
"Trying to recover from _backup_file file: %s", self._backup_file
)
recovery_try = True
if os.path.isfile(self._backup_file):
try:
with open(self._backup_file, "r") as stream:
data = yaml.safe_load(stream)
if self._validate_data(data):
data["restored"] = (
data["restored"] + 1 if "restored" in data else 1
)
self._usage_data = data
self._write_usage_data()
success = True
self._logger.info("Recovered from _backup_file file. Yayy!")
except yaml.constructor.ConstructorError:
try:
data = None
with open(self._backup_file, "r") as stream:
data = yaml.safe_load(stream)
if self._validate_data(data):
data["restored"] = (
data["restored"] + 1 if "restored" in data else 1
)
self._usage_data = data
success = True
self._write_usage_data()
self._logger.info("Recovered from _backup_file file. Yayy!")
except:
self._logger.error("Can't read _backup_file file.")
success = self._repair_backup_usage_data()
except Exception:
self._logger.error("Repair of the _backup_file failed.")
except OSError:
self._logger.error("There is no _backup_file file.")
except yaml.YAMLError:
self._logger.error("There was a YAMLError with the _backup_file file.")
except:
self._logger.error("Can't read _backup_file file.")

if not success:
self._logger.warn("Resetting usage data. (marking as incomplete)")
self._usage_data = self._get_usage_data_template()
if recovery_try:
self._write_usage_data()

def _repair_backup_usage_data(self):
"""
repairs a broken usage backup file, where the version is saved in unicode
Returns:
boolean: successfull
"""
success = False
with open(self._backup_file, "r") as stream:
data = yaml.load(stream)
if self._validate_data(data):
#checks if the version is saved in unicode and converts it into a string see SW-1269
if isinstance(data["version"], unicode):
data["version"] = unicodedata.normalize('NFKD', data["version"]).encode('ascii', 'ignore')
data["restored"] = (
data["restored"] + 1 if "restored" in data else 1
)
self._usage_data = data
success = True
self._write_usage_data()
self._logger.info("Could repair _backup_file file. Yayy!")
return success

def _write_usage_data(self, file=None):
self._usage_data["version"] = self._plugin_version
self._usage_data["ts"] = time.time()
self._usage_data["serial"] = self._device_serial
file = self._storage_file if file is None else file
try:
with open(file, "w") as outfile:
yaml.dump(self._usage_data, outfile, default_flow_style=False)
yaml.safe_dump(self._usage_data, outfile, default_flow_style=False)
except:
self._logger.exception("Can't write file %s due to an exception: ", file)

Expand Down

0 comments on commit efbd0d2

Please sign in to comment.