mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-06 06:27:26 -06:00
Typing fixes
Since I was stupid enough to touch it, I was also forced to boyscout the code.
This commit is contained in:
parent
2e529452dd
commit
adf8285d20
2 changed files with 115 additions and 80 deletions
|
@ -1,6 +1,8 @@
|
|||
# Copyright (c) 2015 Jaime van Kessel
|
||||
# Copyright (c) 2018 Ultimaker B.V.
|
||||
# The PostProcessingPlugin is released under the terms of the AGPLv3 or higher.
|
||||
from typing import Optional, Any, Dict, TYPE_CHECKING, List
|
||||
|
||||
from UM.Signal import Signal, signalemitter
|
||||
from UM.i18n import i18nCatalog
|
||||
|
||||
|
@ -17,16 +19,20 @@ import json
|
|||
import collections
|
||||
i18n_catalog = i18nCatalog("cura")
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from UM.Settings.Interfaces import DefinitionContainerInterface
|
||||
|
||||
|
||||
## Base class for scripts. All scripts should inherit the script class.
|
||||
@signalemitter
|
||||
class Script:
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._settings = None
|
||||
self._stack = None
|
||||
self._stack = None # type: Optional[ContainerStack]
|
||||
self._definition = None # type: Optional[DefinitionContainerInterface]
|
||||
self._instance = None # type: Optional[InstanceContainer]
|
||||
|
||||
def initialize(self):
|
||||
def initialize(self) -> None:
|
||||
setting_data = self.getSettingData()
|
||||
self._stack = ContainerStack(stack_id=str(id(self)))
|
||||
self._stack.setDirty(False) # This stack does not need to be saved.
|
||||
|
@ -45,6 +51,8 @@ class Script:
|
|||
except ContainerFormatError:
|
||||
self._definition = None
|
||||
return
|
||||
if self._definition is None:
|
||||
return
|
||||
self._stack.addContainer(self._definition)
|
||||
self._instance = InstanceContainer(container_id="ScriptInstanceContainer")
|
||||
self._instance.setDefinition(self._definition.getId())
|
||||
|
@ -58,16 +66,17 @@ class Script:
|
|||
settingsLoaded = Signal()
|
||||
valueChanged = Signal() # Signal emitted whenever a value of a setting is changed
|
||||
|
||||
def _onPropertyChanged(self, key, property_name):
|
||||
def _onPropertyChanged(self, key: str, property_name: str) -> None:
|
||||
if property_name == "value":
|
||||
self.valueChanged.emit()
|
||||
|
||||
# Property changed: trigger reslice
|
||||
# To do this we use the global container stack propertyChanged.
|
||||
# Reslicing is necessary for setting changes in this plugin, because the changes
|
||||
# Re-slicing is necessary for setting changes in this plugin, because the changes
|
||||
# are applied only once per "fresh" gcode
|
||||
global_container_stack = Application.getInstance().getGlobalContainerStack()
|
||||
global_container_stack.propertyChanged.emit(key, property_name)
|
||||
if global_container_stack is not None:
|
||||
global_container_stack.propertyChanged.emit(key, property_name)
|
||||
|
||||
## Needs to return a dict that can be used to construct a settingcategory file.
|
||||
# See the example script for an example.
|
||||
|
@ -75,30 +84,35 @@ class Script:
|
|||
# Scripts can either override getSettingData directly, or use getSettingDataString
|
||||
# to return a string that will be parsed as json. The latter has the benefit over
|
||||
# returning a dict in that the order of settings is maintained.
|
||||
def getSettingData(self):
|
||||
setting_data = self.getSettingDataString()
|
||||
if type(setting_data) == str:
|
||||
setting_data = json.loads(setting_data, object_pairs_hook = collections.OrderedDict)
|
||||
def getSettingData(self) -> Dict[str, Any]:
|
||||
setting_data_as_string = self.getSettingDataString()
|
||||
setting_data = json.loads(setting_data_as_string, object_pairs_hook = collections.OrderedDict)
|
||||
return setting_data
|
||||
|
||||
def getSettingDataString(self):
|
||||
def getSettingDataString(self) -> str:
|
||||
raise NotImplementedError()
|
||||
|
||||
def getDefinitionId(self):
|
||||
def getDefinitionId(self) -> Optional[str]:
|
||||
if self._stack:
|
||||
return self._stack.getBottom().getId()
|
||||
bottom = self._stack.getBottom()
|
||||
if bottom is not None:
|
||||
return bottom.getId()
|
||||
return None
|
||||
|
||||
def getStackId(self):
|
||||
def getStackId(self) -> Optional[str]:
|
||||
if self._stack:
|
||||
return self._stack.getId()
|
||||
return None
|
||||
|
||||
## Convenience function that retrieves value of a setting from the stack.
|
||||
def getSettingValueByKey(self, key):
|
||||
return self._stack.getProperty(key, "value")
|
||||
def getSettingValueByKey(self, key: str) -> Any:
|
||||
if self._stack is not None:
|
||||
return self._stack.getProperty(key, "value")
|
||||
return None
|
||||
|
||||
## Convenience function that finds the value in a line of g-code.
|
||||
# When requesting key = x from line "G1 X100" the value 100 is returned.
|
||||
def getValue(self, line, key, default = None):
|
||||
def getValue(self, line: str, key: str, default = None) -> Any:
|
||||
if not key in line or (';' in line and line.find(key) > line.find(';')):
|
||||
return default
|
||||
sub_part = line[line.find(key) + 1:]
|
||||
|
@ -126,7 +140,7 @@ class Script:
|
|||
# \param line The original g-code line that must be modified. If not
|
||||
# provided, an entirely new g-code line will be produced.
|
||||
# \return A line of g-code with the desired parameters filled in.
|
||||
def putValue(self, line = "", **kwargs):
|
||||
def putValue(self, line: str = "", **kwargs) -> str:
|
||||
#Strip the comment.
|
||||
comment = ""
|
||||
if ";" in line:
|
||||
|
@ -167,5 +181,5 @@ class Script:
|
|||
|
||||
## This is called when the script is executed.
|
||||
# It gets a list of g-code strings and needs to return a (modified) list.
|
||||
def execute(self, data):
|
||||
def execute(self, data: List[str]) -> List[str]:
|
||||
raise NotImplementedError()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue