Used NamedTuple from typing iso namedtuple from collections so we can at least give type hints

This commit is contained in:
Marijn Deé 2018-11-21 10:12:53 +01:00
parent c1c3f3abf3
commit 9e8be286af
3 changed files with 55 additions and 54 deletions

View file

@ -1,33 +1,33 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from collections import namedtuple from typing import NamedTuple
ClusterMaterial = namedtuple('ClusterMaterial', [ ClusterMaterial = NamedTuple("ClusterMaterial", [
'guid', ("guid", str),
'material', ("material", str),
'brand', ("brand", str),
'version', ("version", int),
'color', ("color", str),
'density' ("density", str),
]) ])
LocalMaterial = namedtuple('LocalMaterial', [ LocalMaterial = NamedTuple("LocalMaterial", [
'GUID', ("GUID", str),
'id', ("id", str),
'type', ("type", str),
'status', ("status", str),
'base_file', ("base_file", str),
'setting_version', ("setting_version", int),
'version', ("version", int),
'name', ("name", str),
'brand', ("brand", str),
'material', ("material", str),
'color_name', ("color_name", str),
'color_code', ("color_code", str),
'description', ("description", str),
'adhesion_info', ("adhesion_info", str),
'approximate_diameter', ("approximate_diameter", str),
'properties', ("properties", str),
'definition', ("definition", str),
'compatible' ("compatible", str),
]) ])

View file

@ -2,7 +2,6 @@
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import json import json
import os import os
import re
import urllib.parse import urllib.parse
from typing import Dict, TYPE_CHECKING, Set from typing import Dict, TYPE_CHECKING, Set
@ -13,13 +12,13 @@ from UM.Logger import Logger
from UM.MimeTypeDatabase import MimeTypeDatabase from UM.MimeTypeDatabase import MimeTypeDatabase
from UM.Resources import Resources from UM.Resources import Resources
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
# Absolute imports don't work in plugins # Absolute imports don't work in plugins
from .Models import ClusterMaterial, LocalMaterial from .Models import ClusterMaterial, LocalMaterial
if TYPE_CHECKING: if TYPE_CHECKING:
from .ClusterUM3OutputDevice import ClusterUM3OutputDevice from .ClusterUM3OutputDevice import ClusterUM3OutputDevice
## Asynchronous job to send material profiles to the printer. ## Asynchronous job to send material profiles to the printer.
# #
# This way it won't freeze up the interface while sending those materials. # This way it won't freeze up the interface while sending those materials.
@ -86,7 +85,7 @@ class SendMaterialJob(Job):
return { return {
material.id material.id
for guid, material in local_materials.items() for guid, material in local_materials.items()
if guid not in remote_materials or int(material.version) > remote_materials[guid].version if guid not in remote_materials or material.version > remote_materials[guid].version
} }
## Send the materials to the printer. ## Send the materials to the printer.
@ -174,16 +173,18 @@ class SendMaterialJob(Job):
# Find the latest version of all material containers in the registry. # Find the latest version of all material containers in the registry.
for material in material_containers: for material in material_containers:
try: try:
material = LocalMaterial(**material)
# material version must be an int # material version must be an int
if not re.match("\d+", material.version): material["version"] = int(material["version"])
Logger.logException("w", "Local material {} has invalid version '{}'."
.format(material["id"], material.version))
continue
if material.GUID not in result or material.version > result.get(material.GUID).version: # Create a new local material
result[material.GUID] = material local_material = LocalMaterial(**material)
if local_material.GUID not in result or \
local_material.version > result.get(local_material.GUID).version:
result[local_material.GUID] = local_material
except KeyError:
Logger.logException("w", "Local material {} has missing values.".format(material["id"]))
except ValueError: except ValueError:
Logger.logException("w", "Local material {} has invalid values.".format(material["id"])) Logger.logException("w", "Local material {} has invalid values.".format(material["id"]))

View file

@ -19,7 +19,7 @@ from plugins.UM3NetworkPrinting.src.SendMaterialJob import SendMaterialJob
@patch("UM.Resources.Resources.getAllResourcesOfType", lambda _: ["/materials/generic_pla_white.xml.fdm_material"]) @patch("UM.Resources.Resources.getAllResourcesOfType", lambda _: ["/materials/generic_pla_white.xml.fdm_material"])
class TestSendMaterialJob(TestCase): class TestSendMaterialJob(TestCase):
_LOCAL_MATERIAL_WHITE = {"type": "material", "status": "unknown", "id": "generic_pla_white", _LOCAL_MATERIAL_WHITE = {"type": "material", "status": "unknown", "id": "generic_pla_white",
"base_file": "generic_pla_white", "setting_version": 5, "name": "White PLA", "base_file": "generic_pla_white", "setting_version": "5", "name": "White PLA",
"brand": "Generic", "material": "PLA", "color_name": "White", "brand": "Generic", "material": "PLA", "color_name": "White",
"GUID": "badb0ee7-87c8-4f3f-9398-938587b67dce", "version": "1", "color_code": "#ffffff", "GUID": "badb0ee7-87c8-4f3f-9398-938587b67dce", "version": "1", "color_code": "#ffffff",
"description": "Test PLA White", "adhesion_info": "Use glue.", "approximate_diameter": "3", "description": "Test PLA White", "adhesion_info": "Use glue.", "approximate_diameter": "3",
@ -27,7 +27,7 @@ class TestSendMaterialJob(TestCase):
"definition": "fdmprinter", "compatible": True} "definition": "fdmprinter", "compatible": True}
_LOCAL_MATERIAL_BLACK = {"type": "material", "status": "unknown", "id": "generic_pla_black", _LOCAL_MATERIAL_BLACK = {"type": "material", "status": "unknown", "id": "generic_pla_black",
"base_file": "generic_pla_black", "setting_version": 5, "name": "Yellow CPE", "base_file": "generic_pla_black", "setting_version": "5", "name": "Yellow CPE",
"brand": "Ultimaker", "material": "CPE", "color_name": "Black", "brand": "Ultimaker", "material": "CPE", "color_name": "Black",
"GUID": "5fbb362a-41f9-4818-bb43-15ea6df34aa4", "version": "1", "color_code": "#000000", "GUID": "5fbb362a-41f9-4818-bb43-15ea6df34aa4", "version": "1", "color_code": "#000000",
"description": "Test PLA Black", "adhesion_info": "Use glue.", "approximate_diameter": "3", "description": "Test PLA Black", "adhesion_info": "Use glue.", "approximate_diameter": "3",