begin migrating code that can be reused from first iteration of this idea

This commit is contained in:
Brennen Raimer
2019-04-16 00:04:23 -04:00
parent cb0374e858
commit fb5131e480
9 changed files with 503 additions and 1 deletions

View File

@@ -0,0 +1,67 @@
from PyQt5 import QtCore, QtGui, QtWidgets
import psutil
import os
import pathlib
class Location_Validator(QtGui.QValidator):
def __init__(self, parent=None):
super().__init__(parent)
def _onNTFSDrive(self, location):
"""Tests if location is a path on an NTFS-formatted drive. Returns True if path is on NTFS-formatted drive.
Arguments:
location {str} -- A string representing a folder path
"""
match = [drive for drive in psutil.disk_partitions() if (drive.device == pathlib.Path(location).anchor and drive.fstype == "NTFS")]
if not match:
QtWidgets.QMessageBox(
icon=QtWidgets.QMessageBox.Critical,
parent=self.parent(),
text=f"{pathlib.Path(location).anchor} is not an NTFS-formatted volume. Please reformat your drive to NTFS or pick an install path on a different volume"
).show()
return False
else:
return True
def validate(self, input_, pos):
"""Called by Portable_CS_Toolkit_Installer via QtWidget.QLineEdit.TextChanged signal to validate installation location input of user
Arguments:
input_ {str} -- The text entered in the installation location QLineEdit widget
pos {int} -- The current location of the cursor in the QLineEdit widget
"""
if input_.endswith(r":"):
return (
QtGui.QValidator.Intermediate,
input_,
pos,
) # bugfix. _onNTFSDrive triggers when typing the : in a drive name e.g. C:\
# because this class method is invoked via signal, it cannot handle python exceptions
# testing writability in Python is difficult and involves actually writing a temporary file
# to the directory, however, if that fails and raises an exception, it happens in Qt/C++ world
# and the python interpreter can't catch it, the program just crashes without any output,
# therefore, we will use Qt to do checking
input_ = os.path.expandvars(input_) # expand any environment variables in input
location = QtCore.QFileInfo(input_)
if location.exists() and location.isDir():
if input_ and not self._onNTFSDrive(input_):
# clear the location, reset cursor, but allow continued edits
return (QtGui.QValidator.Intermediate, "", 0)
test_file = QtCore.QTemporaryFile(input_ + r"\testXXXXXX.tmp")
if test_file.open(QtCore.QIODevice.ReadWrite):
test_file.close()
test_file.remove()
return (QtGui.QValidator.Acceptable, input_, pos)
else:
return (QtGui.QValidator.Intermediate, input_, pos)
else:
# I wanted to return invalid, but that would prevent input of anything not instantly acceptable
# or editing something acceptable to something else acceptable, passing through an invalid on the way
return (QtGui.QValidator.Intermediate, input_, pos)