Fix typing in the FirmwareUpdateChecker plugin.

This commit is contained in:
Remco Burema 2018-10-11 21:54:27 +02:00
parent 839016d2f3
commit f2b50c748c
3 changed files with 28 additions and 21 deletions

View file

@ -1,10 +1,12 @@
# Copyright (c) 2017 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import json, os
import os
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from typing import List
from UM.Extension import Extension
from UM.Application import Application
from UM.Logger import Logger
@ -19,12 +21,13 @@ from .FirmwareUpdateCheckerLookup import FirmwareUpdateCheckerLookup, get_settin
i18n_catalog = i18nCatalog("cura")
## This Extension checks for new versions of the firmware based on the latest checked version number.
# The plugin is currently only usable for applications maintained by Ultimaker. But it should be relatively easy
# to change it to work for other applications.
class FirmwareUpdateChecker(Extension):
def __init__(self):
def __init__(self) -> None:
super().__init__()
# Listen to a Signal that indicates a change in the list of printers, just if the user has enabled the
@ -36,7 +39,8 @@ class FirmwareUpdateChecker(Extension):
self._late_init = True # Init some things after creation, since we need the path from the plugin-mgr.
self._download_url = None
self._check_job = None
self._name_cache = []
self._name_cache = [] # type: List[str]
self._lookups = None
## Callback for the message that is spawned when there is a new version.
def _onActionTriggered(self, message, action):
@ -46,8 +50,8 @@ class FirmwareUpdateChecker(Extension):
QDesktopServices.openUrl(QUrl(download_url))
else:
Logger.log('e', "Can't find URL for {0}".format(action))
except:
Logger.log('e', "Don't know what to do with {0}".format(action))
except Exception as ex:
Logger.log('e', "Don't know what to do with '{0}' because {1}".format(action, ex))
def _onContainerAdded(self, container):
# Only take care when a new GlobalStack was added

View file

@ -9,6 +9,7 @@ from UM.Version import Version
import urllib.request
from urllib.error import URLError
from typing import Dict
import codecs
from .FirmwareUpdateCheckerLookup import FirmwareUpdateCheckerLookup, get_settings_key_for_machine
@ -24,14 +25,14 @@ class FirmwareUpdateCheckerJob(Job):
ZERO_VERSION = Version(STRING_ZERO_VERSION)
EPSILON_VERSION = Version(STRING_EPSILON_VERSION)
def __init__(self, container=None, silent=False, lookups:FirmwareUpdateCheckerLookup=None, callback=None):
def __init__(self, container, silent, lookups: FirmwareUpdateCheckerLookup, callback) -> None:
super().__init__()
self._container = container
self.silent = silent
self._callback = callback
self._lookups = lookups
self._headers = {} # Don't set headers yet.
self._headers = {} # type:Dict[str, str] # Don't set headers yet.
def getUrlResponse(self, url: str) -> str:
result = self.STRING_ZERO_VERSION

View file

@ -1,7 +1,9 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import json, os
import json
from typing import Callable, Dict, List, Optional
from UM.Logger import Logger
from UM.Version import Version
@ -22,7 +24,7 @@ def default_parse_version_response(response: str) -> Version:
class FirmwareUpdateCheckerLookup:
JSON_NAME_TO_VERSION_PARSE_FUNCTION = {"default": default_parse_version_response}
def __init__(self, json_path):
def __init__(self, json_path) -> None:
# Open the .json file with the needed lookup-lists for each machine(/model) and retrieve 'raw' json.
machines_json = None
with open(json_path, "r", encoding="utf-8") as json_file:
@ -32,11 +34,11 @@ class FirmwareUpdateCheckerLookup:
return
# Parse all the needed lookup-tables from the '.json' file(s) in the resources folder.
self._machine_ids = []
self._machine_per_name = {}
self._parse_version_url_per_machine = {}
self._check_urls_per_machine = {}
self._redirect_user_per_machine = {}
self._machine_ids = [] # type:List[int]
self._machine_per_name = {} # type:Dict[str, int]
self._parse_version_url_per_machine = {} # type:Dict[int, Callable]
self._check_urls_per_machine = {} # type:Dict[int, List[str]]
self._redirect_user_per_machine = {} # type:Dict[int, str]
try:
for machine_json in machines_json:
machine_id = machine_json.get("id")
@ -53,20 +55,20 @@ class FirmwareUpdateCheckerLookup:
for check_url in machine_json.get("check_urls"):
self._check_urls_per_machine[machine_id].append(check_url)
self._redirect_user_per_machine[machine_id] = machine_json.get("update_url")
except:
Logger.log('e', "Couldn't parse firmware-update-check loopup-lists from file.")
except Exception as ex:
Logger.log('e', "Couldn't parse firmware-update-check loopup-lists from file because {0}.".format(ex))
def getMachineIds(self) -> [int]:
def getMachineIds(self) -> List[int]:
return self._machine_ids
def getMachineByName(self, machine_name: str) -> int:
def getMachineByName(self, machine_name: str) -> Optional[int]:
return self._machine_per_name.get(machine_name)
def getParseVersionUrlFor(self, machine_id: int) -> str:
def getParseVersionUrlFor(self, machine_id: int) -> Optional[Callable]:
return self._parse_version_url_per_machine.get(machine_id)
def getCheckUrlsFor(self, machine_id: int) -> [str]:
def getCheckUrlsFor(self, machine_id: int) -> Optional[List[str]]:
return self._check_urls_per_machine.get(machine_id)
def getRedirectUserFor(self, machine_id: int) -> str:
def getRedirectUserFor(self, machine_id: int) -> Optional[str]:
return self._redirect_user_per_machine.get(machine_id)