Files
Artemis/src/versioncontroller.py
Alessandro 8e79bf6adf Fix a bug in the *.spec files and also apply the following major changes:
- Add support for adding the base folder to PATH.
- Only display one pop up window at a time in order to avoid confusion.
- Add automatic updates feature:
	- Windows and Linux versions will be shipped with an updater program used to
	  update both Artemis and the updater itself.
	- MacOs versions will not have the updater. Instead the user will be asked
	  to download the new software version (if present) via browser.
2019-10-15 19:14:29 +02:00

112 lines
3.2 KiB
Python

from io import BytesIO
import json
from constants import Constants
from os_utilities import get_os
from web_utilities import download_file
"""This module exposes just one class: VersionController.
All the relevant information can be accessed with the dot notation on an instance of such class, e.g.:
version_controller.software.hash_code
is the hash_code of the latest release of the software running on the current OS."""
def _download_versions_file():
"""Download the json file containing all the information
about the latest version of the software. Return a dictionary
containing only the information for the running OS.
Return a dictionary from a json with the following structure:
{
"windows": {
"software": {
"version": "...",
"url": "...",
"hash_code": "...",
"size": ...
},
"updater": {
"version": "...",
"url": "...",
"hash_code": "...",
"size": ...
}
},
"linux": {
"software": {
"version": "...",
"url": "...",
"hash_code": "...",
"size": ...
},
"updater": {
"version": "...",
"url": "...",
"hash_code": "...",
"size": ...
}
},
"mac": {
"software": {
"version": "...",
"url": "...",
"hash_code": "...",
"size": ...
},
"updater": {
"version": "...",
"url": "...",
"hash_code": "...",
"size": ...
}
}
}
"""
try:
version_dict = json.load(
BytesIO(download_file(Constants.VERSION_LINK))
)[get_os()]
except Exception:
return None
else:
return version_dict
class VersionController:
"""Dynamically create attributes corresponding to elements of a dictionary.
Used to get updates information."""
def __init__(self, dct=None):
"""Initialize the dictionary"""
super().__init__()
self._dct = dct
def __getattr__(self, attr):
"""Override super().__getattr__. Dynamically create new attributes
corresponding to elements of the diciotnary."""
if self._dct is None:
if not self.update():
return None
try:
dct_element = self._dct[attr]
except Exception("ERROR: Invalid attribute!"):
return None
else:
if isinstance(dct_element, dict):
setattr(self, attr, type(self)(dct_element))
else:
setattr(self, attr, dct_element)
return getattr(self, attr)
def update(self):
"""Reset the dictionary to the correspondig json file containing
the latest version information. Call this function inside a Qthread."""
dct = _download_versions_file()
if dct is not None:
self._dct = dct
return True
else:
return False