MachineNameValidator.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtSlot, pyqtProperty, QObject, pyqtSignal, QRegExp
  4. from PyQt5.QtGui import QValidator
  5. import os #For statvfs.
  6. import urllib #To escape machine names for how they're saved to file.
  7. from UM.Resources import Resources
  8. from UM.Settings.ContainerRegistry import ContainerRegistry
  9. from UM.Settings.InstanceContainer import InstanceContainer
  10. class MachineNameValidator(QObject):
  11. """Are machine names valid?
  12. Performs checks based on the length of the name.
  13. """
  14. def __init__(self, parent = None):
  15. super().__init__(parent)
  16. #Compute the validation regex for printer names. This is limited by the maximum file name length.
  17. try:
  18. filename_max_length = os.statvfs(Resources.getDataStoragePath()).f_namemax
  19. except AttributeError: #Doesn't support statvfs. Probably because it's not a Unix system.
  20. filename_max_length = 255 #Assume it's Windows on NTFS.
  21. machine_name_max_length = filename_max_length - len("_current_settings.") - len(ContainerRegistry.getMimeTypeForContainer(InstanceContainer).preferredSuffix)
  22. # Characters that urllib.parse.quote_plus escapes count for 12! So now
  23. # we must devise a regex that allows only 12 normal characters or 1
  24. # special character, and that up to [machine_name_max_length / 12] times.
  25. maximum_special_characters = int(machine_name_max_length / 12)
  26. unescaped = r"[a-zA-Z0-9_\-\.\/]"
  27. self.machine_name_regex = r"^((" + unescaped + "){0,12}|.){0," + str(maximum_special_characters) + r"}$"
  28. validationChanged = pyqtSignal()
  29. def validate(self, name):
  30. """Check if a specified machine name is allowed.
  31. :param name: The machine name to check.
  32. :return: ``QValidator.Invalid`` if it's disallowed, or ``QValidator.Acceptable`` if it's allowed.
  33. """
  34. #Check for file name length of the current settings container (which is the longest file we're saving with the name).
  35. try:
  36. filename_max_length = os.statvfs(Resources.getDataStoragePath()).f_namemax
  37. except AttributeError: #Doesn't support statvfs. Probably because it's not a Unix system.
  38. filename_max_length = 255 #Assume it's Windows on NTFS.
  39. escaped_name = urllib.parse.quote_plus(name)
  40. current_settings_filename = escaped_name + "_current_settings." + ContainerRegistry.getMimeTypeForContainer(InstanceContainer).preferredSuffix
  41. if len(current_settings_filename) > filename_max_length:
  42. return QValidator.Invalid
  43. return QValidator.Acceptable #All checks succeeded.
  44. @pyqtSlot(str)
  45. def updateValidation(self, new_name):
  46. """Updates the validation state of a machine name text field."""
  47. is_valid = self.validate(new_name)
  48. if is_valid == QValidator.Acceptable:
  49. self.validation_regex = "^.*$" #Matches anything.
  50. else:
  51. self.validation_regex = "a^" #Never matches (unless you manage to get "a" before the start of the string... good luck).
  52. self.validationChanged.emit()
  53. @pyqtProperty("QRegExp", notify=validationChanged)
  54. def machineNameRegex(self):
  55. return QRegExp(self.machine_name_regex)