mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-10 16:27:51 -06:00
STAR-322: Using findChanges method to simplify code
This commit is contained in:
parent
7e76913736
commit
657e763318
3 changed files with 27 additions and 24 deletions
|
@ -24,6 +24,7 @@ from .Models.CloudPrintResponse import CloudPrintResponse
|
||||||
from .Models.CloudJobResponse import CloudJobResponse
|
from .Models.CloudJobResponse import CloudJobResponse
|
||||||
from .Models.CloudClusterPrinter import CloudClusterPrinter
|
from .Models.CloudClusterPrinter import CloudClusterPrinter
|
||||||
from .Models.CloudClusterPrintJob import CloudClusterPrintJob
|
from .Models.CloudClusterPrintJob import CloudClusterPrintJob
|
||||||
|
from .Utils import findChanges
|
||||||
|
|
||||||
|
|
||||||
## Class that contains all the translations for this module.
|
## Class that contains all the translations for this module.
|
||||||
|
@ -198,45 +199,41 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice):
|
||||||
self._updatePrintJobs(status.print_jobs)
|
self._updatePrintJobs(status.print_jobs)
|
||||||
|
|
||||||
def _updatePrinters(self, printers: List[CloudClusterPrinter]) -> None:
|
def _updatePrinters(self, printers: List[CloudClusterPrinter]) -> None:
|
||||||
remote_printers = {p.uuid: p for p in printers} # type: Dict[str, CloudClusterPrinter]
|
previous = {p.key: p for p in self._printers} # type: Dict[str, PrinterOutputModel]
|
||||||
current_printers = {p.key: p for p in self._printers} # type: Dict[str, PrinterOutputModel]
|
received = {p.uuid: p for p in printers} # type: Dict[str, CloudClusterPrinter]
|
||||||
|
|
||||||
remote_printer_ids = set(remote_printers) # type: Set[str]
|
removed_printers, added_printers, updated_printers = findChanges(previous, received)
|
||||||
current_printer_ids = set(current_printers) # type: Set[str]
|
|
||||||
|
|
||||||
for removed_printer_id in current_printer_ids.difference(remote_printer_ids):
|
for removed_printer in removed_printers:
|
||||||
removed_printer = current_printers[removed_printer_id]
|
|
||||||
self._printers.remove(removed_printer)
|
self._printers.remove(removed_printer)
|
||||||
|
|
||||||
for new_printer_id in remote_printer_ids.difference(current_printer_ids):
|
for added_printer in added_printers:
|
||||||
new_printer = remote_printers[new_printer_id]
|
|
||||||
controller = PrinterOutputController(self)
|
controller = PrinterOutputController(self)
|
||||||
self._printers.append(new_printer.createOutputModel(controller))
|
self._printers.append(added_printer.createOutputModel(controller))
|
||||||
|
|
||||||
for updated_printer_guid in current_printer_ids.intersection(remote_printer_ids):
|
for model, printer in updated_printers:
|
||||||
remote_printers[updated_printer_guid].updateOutputModel(current_printers[updated_printer_guid])
|
printer.updateOutputModel(model)
|
||||||
|
|
||||||
self._clusterPrintersChanged.emit()
|
self._clusterPrintersChanged.emit()
|
||||||
|
|
||||||
def _updatePrintJobs(self, jobs: List[CloudClusterPrintJob]) -> None:
|
def _updatePrintJobs(self, jobs: List[CloudClusterPrintJob]) -> None:
|
||||||
remote_jobs = {j.uuid: j for j in jobs} # type: Dict[str, CloudClusterPrintJob]
|
received = {j.uuid: j for j in jobs} # type: Dict[str, CloudClusterPrintJob]
|
||||||
current_jobs = {j.key: j for j in self._print_jobs} # type: Dict[str, UM3PrintJobOutputModel]
|
previous = {j.key: j for j in self._print_jobs} # type: Dict[str, UM3PrintJobOutputModel]
|
||||||
|
|
||||||
remote_job_ids = set(remote_jobs) # type: Set[str]
|
removed_jobs, added_jobs, updated_jobs = findChanges(previous, received)
|
||||||
current_job_ids = set(current_jobs) # type: Set[str]
|
|
||||||
|
|
||||||
for removed_job_id in current_job_ids.difference(remote_job_ids):
|
for removed_job in removed_jobs:
|
||||||
self._print_jobs.remove(current_jobs[removed_job_id])
|
self._print_jobs.remove(removed_job)
|
||||||
|
|
||||||
for new_job_id in remote_job_ids.difference(current_job_ids):
|
for added_job in added_jobs:
|
||||||
self._addPrintJob(remote_jobs[new_job_id])
|
self._addPrintJob(added_job)
|
||||||
|
|
||||||
for updated_job_id in current_job_ids.intersection(remote_job_ids):
|
for model, job in updated_jobs:
|
||||||
remote_jobs[updated_job_id].updateOutputModel(current_jobs[updated_job_id])
|
job.updateOutputModel(model)
|
||||||
|
|
||||||
# We only have to update when jobs are added or removed
|
# We only have to update when jobs are added or removed
|
||||||
# updated jobs push their changes via their output model
|
# updated jobs push their changes via their output model
|
||||||
if remote_job_ids != current_job_ids:
|
if added_jobs or removed_jobs:
|
||||||
self.printJobsChanged.emit()
|
self.printJobsChanged.emit()
|
||||||
|
|
||||||
def _addPrintJob(self, job: CloudClusterPrintJob) -> None:
|
def _addPrintJob(self, job: CloudClusterPrintJob) -> None:
|
||||||
|
|
|
@ -4,6 +4,13 @@ T = TypeVar("T")
|
||||||
U = TypeVar("U")
|
U = TypeVar("U")
|
||||||
|
|
||||||
|
|
||||||
|
## Splits the given dictionaries into three lists (in a tuple):
|
||||||
|
# - `removed`: Items that were in the first argument but removed in the second one.
|
||||||
|
# - `added`: Items that were not in the first argument but were included in the second one.
|
||||||
|
# - `updated`: Items that were in both dictionaries. Both values are given in a tuple.
|
||||||
|
# \param previous: The previous items
|
||||||
|
# \param received: The received items
|
||||||
|
# \return: The tuple (removed, added, updated) as explained above.
|
||||||
def findChanges(previous: Dict[str, T], received: Dict[str, U]) -> Tuple[List[T], List[U], List[Tuple[T, U]]]:
|
def findChanges(previous: Dict[str, T], received: Dict[str, U]) -> Tuple[List[T], List[U], List[Tuple[T, U]]]:
|
||||||
previous_ids = set(previous)
|
previous_ids = set(previous)
|
||||||
received_ids = set(received)
|
received_ids = set(received)
|
||||||
|
|
|
@ -15,8 +15,7 @@ from cura.CuraApplication import CuraApplication
|
||||||
|
|
||||||
## Class that contains all the translations for this module.
|
## Class that contains all the translations for this module.
|
||||||
class T:
|
class T:
|
||||||
# The translation catalog for this device.
|
# The translation catalog for this module.
|
||||||
|
|
||||||
_I18N_CATALOG = i18nCatalog("cura")
|
_I18N_CATALOG = i18nCatalog("cura")
|
||||||
NO_FORMATS_AVAILABLE = _I18N_CATALOG.i18nc("@info:status", "There are no file formats available to write with!")
|
NO_FORMATS_AVAILABLE = _I18N_CATALOG.i18nc("@info:status", "There are no file formats available to write with!")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue