MachineNameValidator.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. ## Are machine names valid?
  11. #
  12. # Performs checks based on the length of the name.
  13. class MachineNameValidator(QObject):
  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. ## Check if a specified machine name is allowed.
  30. #
  31. # \param name The machine name to check.
  32. # \param position The current position of the cursor in the text box.
  33. # \return ``QValidator.Invalid`` if it's disallowed, or
  34. # ``QValidator.Acceptable`` if it's allowed.
  35. def validate(self, name, position):
  36. #Check for file name length of the current settings container (which is the longest file we're saving with the name).
  37. try:
  38. filename_max_length = os.statvfs(Resources.getDataStoragePath()).f_namemax
  39. except AttributeError: #Doesn't support statvfs. Probably because it's not a Unix system.
  40. filename_max_length = 255 #Assume it's Windows on NTFS.
  41. escaped_name = urllib.parse.quote_plus(name)
  42. current_settings_filename = escaped_name + "_current_settings." + ContainerRegistry.getMimeTypeForContainer(InstanceContainer).preferredSuffix
  43. if len(current_settings_filename) > filename_max_length:
  44. return QValidator.Invalid
  45. return QValidator.Acceptable #All checks succeeded.
  46. ## Updates the validation state of a machine name text field.
  47. @pyqtSlot(str)
  48. def updateValidation(self, new_name):
  49. is_valid = self.validate(new_name, 0)
  50. if is_valid == QValidator.Acceptable:
  51. self.validation_regex = "^.*$" #Matches anything.
  52. else:
  53. self.validation_regex = "a^" #Never matches (unless you manage to get "a" before the start of the string... good luck).
  54. self.validationChanged.emit()
  55. @pyqtProperty("QRegExp", notify=validationChanged)
  56. def machineNameRegex(self):
  57. return QRegExp(self.machine_name_regex)