mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-12 09:17:50 -06:00
Added tests for SendMaterialJob and refactored SendMaterialJob for
better testability. This is part of a larger project to create tests for the UM3NetworkPrinting plugin in preparation for printing from the cloud
This commit is contained in:
parent
bfe5ae6f9c
commit
e9e8c49b2d
3 changed files with 491 additions and 47 deletions
87
plugins/UM3NetworkPrinting/src/Models.py
Normal file
87
plugins/UM3NetworkPrinting/src/Models.py
Normal file
|
@ -0,0 +1,87 @@
|
|||
# Copyright (c) 2018 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class BaseModel:
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.__dict__ == other.__dict__ if type(self) == type(other) else False
|
||||
|
||||
|
||||
## Represents an item in the cluster API response for installed materials.
|
||||
class ClusterMaterial(BaseModel):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.version = int(self.version)
|
||||
self.density = float(self.density)
|
||||
|
||||
guid: None # type: Optional[str]
|
||||
|
||||
material: None # type: Optional[str]
|
||||
|
||||
brand: None # type: Optional[str]
|
||||
|
||||
version = None # type: Optional[int]
|
||||
|
||||
color: None # type: Optional[str]
|
||||
|
||||
density: None # type: Optional[float]
|
||||
|
||||
|
||||
class LocalMaterialProperties(BaseModel):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.density = float(self.density)
|
||||
self.diameter = float(self.diameter)
|
||||
self.weight = float(self.weight)
|
||||
|
||||
density: None # type: Optional[float]
|
||||
|
||||
diameter: None # type: Optional[float]
|
||||
|
||||
weight: None # type: Optional[int]
|
||||
|
||||
|
||||
class LocalMaterial(BaseModel):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.properties = LocalMaterialProperties(**self.properties)
|
||||
self.approximate_diameter = float(self.approximate_diameter)
|
||||
self.version = int(self.version)
|
||||
|
||||
GUID: None # type: Optional[str]
|
||||
|
||||
id: None # type: Optional[str]
|
||||
|
||||
type: None # type: Optional[str]
|
||||
|
||||
status: None # type: Optional[str]
|
||||
|
||||
base_file: None # type: Optional[str]
|
||||
|
||||
setting_version: None # type: Optional[str]
|
||||
|
||||
version = None # type: Optional[int]
|
||||
|
||||
name: None # type: Optional[str]
|
||||
|
||||
brand: None # type: Optional[str]
|
||||
|
||||
material: None # type: Optional[str]
|
||||
|
||||
color_name: None # type: Optional[str]
|
||||
|
||||
description: None # type: Optional[str]
|
||||
|
||||
adhesion_info: None # type: Optional[str]
|
||||
|
||||
approximate_diameter: None # type: Optional[float]
|
||||
|
||||
properties: None # type: LocalMaterialProperties
|
||||
|
||||
definition: None # type: Optional[str]
|
||||
|
||||
compatible: None # type: Optional[bool]
|
|
@ -1,99 +1,129 @@
|
|||
# Copyright (c) 2018 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
|
||||
import json #To understand the list of materials from the printer reply.
|
||||
import os #To walk over material files.
|
||||
import os.path #To filter on material files.
|
||||
from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest #To listen to the reply from the printer.
|
||||
from typing import Any, Dict, Set, TYPE_CHECKING
|
||||
import urllib.parse #For getting material IDs from their file names.
|
||||
import json # To understand the list of materials from the printer reply.
|
||||
import os # To walk over material files.
|
||||
import os.path # To filter on material files.
|
||||
import urllib.parse # For getting material IDs from their file names.
|
||||
from typing import Dict, TYPE_CHECKING
|
||||
|
||||
from UM.Job import Job #The interface we're implementing.
|
||||
from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest # To listen to the reply from the printer.
|
||||
|
||||
from UM.Job import Job # The interface we're implementing.
|
||||
from UM.Logger import Logger
|
||||
from UM.MimeTypeDatabase import MimeTypeDatabase #To strip the extensions of the material profile files.
|
||||
from UM.MimeTypeDatabase import MimeTypeDatabase # To strip the extensions of the material profile files.
|
||||
from UM.Resources import Resources
|
||||
from UM.Settings.ContainerRegistry import ContainerRegistry #To find the GUIDs of materials.
|
||||
|
||||
from cura.CuraApplication import CuraApplication #For the resource types.
|
||||
from UM.Settings.ContainerRegistry import ContainerRegistry # To find the GUIDs of materials.
|
||||
from cura.CuraApplication import CuraApplication # For the resource types.
|
||||
from plugins.UM3NetworkPrinting.src.Models import ClusterMaterial, LocalMaterial
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .ClusterUM3OutputDevice import ClusterUM3OutputDevice
|
||||
|
||||
|
||||
## Asynchronous job to send material profiles to the printer.
|
||||
#
|
||||
# This way it won't freeze up the interface while sending those materials.
|
||||
class SendMaterialJob(Job):
|
||||
def __init__(self, device: "ClusterUM3OutputDevice") -> None:
|
||||
super().__init__()
|
||||
self.device = device #type: ClusterUM3OutputDevice
|
||||
self.device = device # type: ClusterUM3OutputDevice
|
||||
|
||||
def run(self) -> None:
|
||||
self.device.get("materials/", on_finished = self.sendMissingMaterials)
|
||||
self.device.get("materials/", on_finished=self.sendMissingMaterials)
|
||||
|
||||
def sendMissingMaterials(self, reply: QNetworkReply) -> None:
|
||||
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) != 200: #Got an error from the HTTP request.
|
||||
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) != 200: # Got an error from the HTTP request.
|
||||
Logger.log("e", "Couldn't request current material storage on printer. Not syncing materials.")
|
||||
return
|
||||
|
||||
remote_materials_list = reply.readAll().data().decode("utf-8")
|
||||
# Collect materials from the printer's reply
|
||||
try:
|
||||
remote_materials_list = json.loads(remote_materials_list)
|
||||
remote_materials_by_guid = self._parseReply(reply)
|
||||
except json.JSONDecodeError:
|
||||
Logger.log("e", "Request material storage on printer: I didn't understand the printer's answer.")
|
||||
return
|
||||
try:
|
||||
remote_materials_by_guid = {material["guid"]: material for material in remote_materials_list} #Index by GUID.
|
||||
except KeyError:
|
||||
Logger.log("e", "Request material storage on printer: Printer's answer was missing GUIDs.")
|
||||
return
|
||||
|
||||
container_registry = ContainerRegistry.getInstance()
|
||||
local_materials_list = filter(lambda material: ("GUID" in material and "version" in material and "id" in material), container_registry.findContainersMetadata(type = "material"))
|
||||
local_materials_by_guid = {material["GUID"]: material for material in local_materials_list if material["id"] == material["base_file"]}
|
||||
for material in local_materials_list: #For each GUID get the material with the highest version number.
|
||||
try:
|
||||
if int(material["version"]) > local_materials_by_guid[material["GUID"]]["version"]:
|
||||
local_materials_by_guid[material["GUID"]] = material
|
||||
except ValueError:
|
||||
Logger.log("e", "Material {material_id} has invalid version number {number}.".format(material_id = material["id"], number = material["version"]))
|
||||
continue
|
||||
# Collect local materials
|
||||
local_materials_by_guid = self._getLocalMaterials()
|
||||
|
||||
materials_to_send = set() #type: Set[Dict[str, Any]]
|
||||
for guid, material in local_materials_by_guid.items():
|
||||
if guid not in remote_materials_by_guid:
|
||||
materials_to_send.add(material["id"])
|
||||
continue
|
||||
try:
|
||||
if int(material["version"]) > remote_materials_by_guid[guid]["version"]:
|
||||
materials_to_send.add(material["id"])
|
||||
continue
|
||||
except KeyError:
|
||||
Logger.log("e", "Current material storage on printer was an invalid reply (missing version).")
|
||||
return
|
||||
# Find out what materials are new or updated annd must be sent to the printer
|
||||
materials_to_send = {
|
||||
material.id
|
||||
for guid, material in local_materials_by_guid.items()
|
||||
if guid not in remote_materials_by_guid or
|
||||
material.version > remote_materials_by_guid[guid].version
|
||||
}
|
||||
|
||||
# Send materials to the printer
|
||||
self.sendMaterialsToPrinter(materials_to_send)
|
||||
|
||||
def sendMaterialsToPrinter(self, materials_to_send):
|
||||
for file_path in Resources.getAllResourcesOfType(CuraApplication.ResourceTypes.MaterialInstanceContainer):
|
||||
try:
|
||||
mime_type = MimeTypeDatabase.getMimeTypeForFile(file_path)
|
||||
except MimeTypeDatabase.MimeTypeNotFoundError:
|
||||
continue #Not the sort of file we'd like to send then.
|
||||
continue # Not the sort of file we'd like to send then.
|
||||
|
||||
_, file_name = os.path.split(file_path)
|
||||
material_id = urllib.parse.unquote_plus(mime_type.stripExtension(file_name))
|
||||
|
||||
if material_id not in materials_to_send:
|
||||
continue
|
||||
|
||||
parts = []
|
||||
with open(file_path, "rb") as f:
|
||||
parts.append(self.device._createFormPart("name=\"file\"; filename=\"{file_name}\"".format(file_name = file_name), f.read()))
|
||||
parts.append(
|
||||
self.device._createFormPart("name=\"file\"; filename=\"{file_name}\"".format(file_name=file_name),
|
||||
f.read()))
|
||||
signature_file_path = file_path + ".sig"
|
||||
if os.path.exists(signature_file_path):
|
||||
_, signature_file_name = os.path.split(signature_file_path)
|
||||
with open(signature_file_path, "rb") as f:
|
||||
parts.append(self.device._createFormPart("name=\"signature_file\"; filename=\"{file_name}\"".format(file_name = signature_file_name), f.read()))
|
||||
parts.append(self.device._createFormPart(
|
||||
"name=\"signature_file\"; filename=\"{file_name}\"".format(file_name=signature_file_name),
|
||||
f.read()))
|
||||
|
||||
Logger.log("d", "Syncing material {material_id} with cluster.".format(material_id = material_id))
|
||||
self.device.postFormWithParts(target = "materials/", parts = parts, on_finished = self.sendingFinished)
|
||||
Logger.log("d", "Syncing material {material_id} with cluster.".format(material_id=material_id))
|
||||
self.device.postFormWithParts(target="materials/", parts=parts, on_finished=self.sendingFinished)
|
||||
|
||||
def sendingFinished(self, reply: QNetworkReply):
|
||||
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) != 200:
|
||||
Logger.log("e", "Received error code from printer when syncing material: {code}".format(code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)))
|
||||
Logger.log("e", reply.readAll().data().decode("utf-8"))
|
||||
Logger.log("e", "Received error code from printer when syncing material: {code}".format(
|
||||
code=reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)))
|
||||
Logger.log("e", reply.readAll().data().decode("utf-8"))
|
||||
|
||||
## Parse the reply from the printer
|
||||
#
|
||||
# Parses the reply to a "/materials" request to the printer
|
||||
#
|
||||
# \return a dictionary of ClustMaterial objects by GUID
|
||||
# \throw json.JSONDecodeError Raised when the reply does not contain a valid json string
|
||||
# \throw KeyErrror Raised when on of the materials does not include a valid guid
|
||||
@classmethod
|
||||
def _parseReply(cls, reply: QNetworkReply) -> Dict[str, ClusterMaterial]:
|
||||
remote_materials_list = json.loads(reply.readAll().data().decode("utf-8"))
|
||||
return {material["guid"]: ClusterMaterial(**material) for material in remote_materials_list}
|
||||
|
||||
## Retrieves a list of local materials
|
||||
#
|
||||
# Only the new newest version of the local materials is returned
|
||||
#
|
||||
# \return a dictionary of LocalMaterial objects by GUID
|
||||
@classmethod
|
||||
def _getLocalMaterials(cls):
|
||||
result = {}
|
||||
for material in ContainerRegistry.getInstance().findContainersMetadata(type="material"):
|
||||
try:
|
||||
localMaterial = LocalMaterial(**material)
|
||||
|
||||
if localMaterial.GUID not in result or localMaterial.version > result.get(localMaterial.GUID).version:
|
||||
result[localMaterial.GUID] = localMaterial
|
||||
except (ValueError):
|
||||
Logger.log("e", "Material {material_id} has invalid version number {number}.".format(
|
||||
material_id=material["id"], number=material["version"]))
|
||||
|
||||
return result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue