Add typing

This commit is contained in:
Ghostkeeper 2019-06-18 11:48:57 +02:00
parent 6a65045587
commit 7fbdccffdb
No known key found for this signature in database
GPG key ID: 86BEF881AE2CF276
2 changed files with 26 additions and 23 deletions

View file

@ -1,6 +1,8 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2019 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.
import configparser import configparser
from typing import List, Optional, Tuple
from UM.PluginRegistry import PluginRegistry from UM.PluginRegistry import PluginRegistry
from UM.Logger import Logger from UM.Logger import Logger
@ -17,19 +19,19 @@ import zipfile
class CuraProfileReader(ProfileReader): class CuraProfileReader(ProfileReader):
## Initialises the cura profile reader. ## Initialises the cura profile reader.
# This does nothing since the only other function is basically stateless. # This does nothing since the only other function is basically stateless.
def __init__(self): def __init__(self) -> None:
super().__init__() super().__init__()
## Reads a cura profile from a file and returns it. ## Reads a cura profile from a file and returns it.
# #
# \param file_name The file to read the cura profile from. # \param file_name The file to read the cura profile from.
# \return The cura profile that was in the file, if any. If the file could # \return The cura profiles that were in the file, if any. If the file
# not be read or didn't contain a valid profile, \code None \endcode is # could not be read or didn't contain a valid profile, ``None`` is
# returned. # returned.
def read(self, file_name): def read(self, file_name: str) -> List[Optional[InstanceContainer]]:
try: try:
with zipfile.ZipFile(file_name, "r") as archive: with zipfile.ZipFile(file_name, "r") as archive:
results = [] results = [] #type: List[Optional[InstanceContainer]]
for profile_id in archive.namelist(): for profile_id in archive.namelist():
with archive.open(profile_id) as f: with archive.open(profile_id) as f:
serialized = f.read() serialized = f.read()
@ -41,15 +43,16 @@ class CuraProfileReader(ProfileReader):
except zipfile.BadZipFile: except zipfile.BadZipFile:
# It must be an older profile from Cura 2.1. # It must be an older profile from Cura 2.1.
with open(file_name, encoding = "utf-8") as fhandle: with open(file_name, encoding = "utf-8") as fhandle:
serialized = fhandle.read() serialized_bytes = fhandle.read()
return [self._loadProfile(serialized, profile_id) for serialized, profile_id in self._upgradeProfile(serialized, file_name)] return [self._loadProfile(serialized, profile_id) for serialized, profile_id in self._upgradeProfile(serialized_bytes, file_name)]
## Convert a profile from an old Cura to this Cura if needed. ## Convert a profile from an old Cura to this Cura if needed.
# #
# \param serialized \type{str} The profile data to convert in the serialized on-disk format. # \param serialized The profile data to convert in the serialized on-disk
# \param profile_id \type{str} The name of the profile. # format.
# \return \type{List[Tuple[str,str]]} List of serialized profile strings and matching profile names. # \param profile_id The name of the profile.
def _upgradeProfile(self, serialized, profile_id): # \return List of serialized profile strings and matching profile names.
def _upgradeProfile(self, serialized: str, profile_id: str) -> List[Tuple[str, str]]:
parser = configparser.ConfigParser(interpolation = None) parser = configparser.ConfigParser(interpolation = None)
parser.read_string(serialized) parser.read_string(serialized)
@ -69,10 +72,10 @@ class CuraProfileReader(ProfileReader):
## Load a profile from a serialized string. ## Load a profile from a serialized string.
# #
# \param serialized \type{str} The profile data to read. # \param serialized The profile data to read.
# \param profile_id \type{str} The name of the profile. # \param profile_id The name of the profile.
# \return \type{InstanceContainer|None} # \return The profile that was stored in the string.
def _loadProfile(self, serialized, profile_id): def _loadProfile(self, serialized: str, profile_id: str) -> Optional[InstanceContainer]:
# Create an empty profile. # Create an empty profile.
profile = InstanceContainer(profile_id) profile = InstanceContainer(profile_id)
profile.setMetaDataEntry("type", "quality_changes") profile.setMetaDataEntry("type", "quality_changes")
@ -88,11 +91,11 @@ class CuraProfileReader(ProfileReader):
## Upgrade a serialized profile to the current profile format. ## Upgrade a serialized profile to the current profile format.
# #
# \param serialized \type{str} The profile data to convert. # \param serialized The profile data to convert.
# \param profile_id \type{str} The name of the profile. # \param profile_id The name of the profile.
# \param source_version \type{int} The profile version of 'serialized'. # \param source_version The profile version of 'serialized'.
# \return \type{List[Tuple[str,str]]} List of serialized profile strings and matching profile names. # \return List of serialized profile strings and matching profile names.
def _upgradeProfileVersion(self, serialized, profile_id, source_version): def _upgradeProfileVersion(self, serialized: str, profile_id: str, source_version: int) -> List[Tuple[str, str]]:
converter_plugins = PluginRegistry.getInstance().getAllMetaData(filter = {"version_upgrade": {} }, active_only = True) converter_plugins = PluginRegistry.getInstance().getAllMetaData(filter = {"version_upgrade": {} }, active_only = True)
source_format = ("profile", source_version) source_format = ("profile", source_version)