Andere Methode um zu überprüfen, ob eine CD im Laufwerk ist

This commit is contained in:
Stefan Ritter 2023-10-20 10:57:04 +02:00
parent 1c9ffcf342
commit 1943f7d3dc
2 changed files with 107 additions and 84 deletions

View File

@ -1,3 +1,4 @@
PyQt6 PyQt6
pydicom pydicom
pyinstaller pyinstaller
wmi

View File

@ -6,6 +6,7 @@ import pydicom
import shutil import shutil
import random import random
import ctypes import ctypes
import wmi
from registry import Registry from registry import Registry
from PyQt6.QtWidgets import QDialog from PyQt6.QtWidgets import QDialog
from PyQt6.QtWidgets import QLabel from PyQt6.QtWidgets import QLabel
@ -45,40 +46,45 @@ class wArchive(QDialog):
self.tLine3 = QLabel("", self) self.tLine3 = QLabel("", self)
self.tLine3.setGeometry(column2s, 50, column2w, 20) self.tLine3.setGeometry(column2s, 50, column2w, 20)
self.lLine3a = QLabel("CD im Laufwerk:", self)
self.lLine3a.setGeometry(10, 70, column1w, 20)
self.tLine3a = QLabel("", self)
self.tLine3a.setGeometry(column2s, 70, column2w, 20)
self.lLine4 = QLabel("DICOMDIR-Datei gefunden:", self) self.lLine4 = QLabel("DICOMDIR-Datei gefunden:", self)
self.lLine4.setGeometry(10, 70, column1w, 20) self.lLine4.setGeometry(10, 90, column1w, 20)
self.tLine4 = QLabel("", self) self.tLine4 = QLabel("", self)
self.tLine4.setGeometry(column2s, 70, column2w, 20) self.tLine4.setGeometry(column2s, 90, column2w, 20)
self.lLine5 = QLabel("DICOM-Verzeichnis gefunden:", self) self.lLine5 = QLabel("DICOM-Verzeichnis gefunden:", self)
self.lLine5.setGeometry(10, 90, column1w, 20) self.lLine5.setGeometry(10, 110, column1w, 20)
self.tLine5 = QLabel("", self) self.tLine5 = QLabel("", self)
self.tLine5.setGeometry(column2s, 90, column2w, 20) self.tLine5.setGeometry(column2s, 110, column2w, 20)
self.lLine6 = QLabel("Archiv beschreibbar:", self) self.lLine6 = QLabel("Archiv beschreibbar:", self)
self.lLine6.setGeometry(10, 110, column1w, 20) self.lLine6.setGeometry(10, 130, column1w, 20)
self.tLine6 = QLabel("", self) self.tLine6 = QLabel("", self)
self.tLine6.setGeometry(column2s, 110, column2w, 20) self.tLine6.setGeometry(column2s, 130, column2w, 20)
self.lLine7 = QLabel("DICOM-Dateien gefunden:", self) self.lLine7 = QLabel("DICOM-Dateien gefunden:", self)
self.lLine7.setGeometry(10, 130, column1w, 20) self.lLine7.setGeometry(10, 150, column1w, 20)
self.tLine7 = QLabel("", self) self.tLine7 = QLabel("", self)
self.tLine7.setGeometry(column2s, 130, column2w, 20) self.tLine7.setGeometry(column2s, 150, column2w, 20)
self.lLine8 = QLabel("Name:", self) self.lLine8 = QLabel("Name:", self)
self.lLine8.setGeometry(10, 150, column1w, 20) self.lLine8.setGeometry(10, 170, column1w, 20)
self.tLine8 = QLabel("", self) self.tLine8 = QLabel("", self)
self.tLine8.setGeometry(column2s, 150, column2w, 20) self.tLine8.setGeometry(column2s, 170, column2w, 20)
self.lLine9 = QLabel("Geburtsdatum:", self) self.lLine9 = QLabel("Geburtsdatum:", self)
self.lLine9.setGeometry(10, 170, column1w, 20) self.lLine9.setGeometry(10, 190, column1w, 20)
self.tLine9 = QLabel("", self) self.tLine9 = QLabel("", self)
self.tLine9.setGeometry(column2s, 170, column2w, 20) self.tLine9.setGeometry(column2s, 190, column2w, 20)
self.lLine10 = QLabel("Dateien:", self) self.lLine10 = QLabel("Dateien:", self)
self.lLine10.setGeometry(10, 190, column1w, 20) self.lLine10.setGeometry(10, 210, column1w, 20)
self.tLine10 = QLabel("", self) self.tLine10 = QLabel("", self)
self.tLine10.setGeometry(column2s, 190, column2w, 20) self.tLine10.setGeometry(column2s, 210, column2w, 20)
self.bArchive = QPushButton("Archivieren", self) self.bArchive = QPushButton("Archivieren", self)
self.bArchive.setGeometry(10, 350, 380, 40) self.bArchive.setGeometry(10, 350, 380, 40)
@ -107,6 +113,7 @@ class wArchive(QDialog):
self.tLine3.setText(self.CDRomPath + ":\\") self.tLine3.setText(self.CDRomPath + ":\\")
# Logic # Logic
cd_check = False
dicomfile = False dicomfile = False
dicomdir = False dicomdir = False
target_writable = False target_writable = False
@ -114,76 +121,90 @@ class wArchive(QDialog):
has_pName = False has_pName = False
has_pBirth = False has_pBirth = False
# Check if DICOMDIR file exists # Check CD drive
if os.path.isfile(os.path.join(self.CDRomPath + ":", os.sep, "DICOMDIR")): cds = wmi.WMI()
dicomfile = True for cd in cds.Win32_CDROMDrive():
self.tLine4.setText(okay) if cd.MediaLoaded:
self.tLine4.setStyleSheet("color: green") cd_check = True
if cd_check:
self.tLine3a.setText(okay)
self.tLine3a.setStyleSheet("color: green")
else: else:
self.tLine4.setText(notokay) self.tLine3a.setText(notokay)
self.tLine4.setStyleSheet("color: red") self.tLine3a.setStyleSheet("color: red")
# Check if DICOM directory exists if cd_check:
if os.path.isdir(os.path.join(self.CDRomPath + ":", os.sep, "DICOM")): # Check if DICOMDIR file exists
dicomdir = True if os.path.isfile(os.path.join(self.CDRomPath + ":", os.sep, "DICOMDIR")):
self.tLine5.setText(okay) dicomfile = True
self.tLine5.setStyleSheet("color: green") self.tLine4.setText(okay)
else: self.tLine4.setStyleSheet("color: green")
self.tLine5.setText(notokay) else:
self.tLine5.setStyleSheet("color: red") self.tLine4.setText(notokay)
self.tLine4.setStyleSheet("color: red")
# Check if ArchivePath is writable # Check if DICOM directory exists
if os.access(self.ArchivePath, os.W_OK) is True: if os.path.isdir(os.path.join(self.CDRomPath + ":", os.sep, "DICOM")):
target_writable = True dicomdir = True
self.tLine6.setText(okay) self.tLine5.setText(okay)
self.tLine6.setStyleSheet("color: green") self.tLine5.setStyleSheet("color: green")
else: else:
self.tLine6.setText(notokay) self.tLine5.setText(notokay)
self.tLine6.setStyleSheet("color: red") self.tLine5.setStyleSheet("color: red")
# List of all files under DICOM directory # Check if ArchivePath is writable
path = os.path.join(self.CDRomPath + ":", os.sep, "DICOM") if os.access(self.ArchivePath, os.W_OK) is True:
self.files = [] target_writable = True
for x in os.walk(path): self.tLine6.setText(okay)
for y in glob.glob(os.path.join(x[0], "*")): self.tLine6.setStyleSheet("color: green")
if os.path.isfile(y): else:
self.files.append(y) self.tLine6.setText(notokay)
self.tLine6.setStyleSheet("color: red")
# Get PatientsName from first file # List of all files under DICOM directory
self.pName = "" path = os.path.join(self.CDRomPath + ":", os.sep, "DICOM")
self.pBirth = "" self.files = []
for x in os.walk(path):
for y in glob.glob(os.path.join(x[0], "*")):
if os.path.isfile(y):
self.files.append(y)
try: # Get PatientsName from first file
dicom = pydicom.read_file(self.files[0]) self.pName = ""
self.pName = str(dicom.PatientName) self.pBirth = ""
self.pBirth = str(dicom.PatientBirthDate)
has_dicom_files = True
has_pName = True
has_pBirth = True
self.tLine7.setText(okay)
self.tLine7.setStyleSheet("color: green")
except:
self.tLine7.setText(notokay)
self.tLine7.setStyleSheet("color: red")
# Output pName and pBirth try:
if self.pName and self.pBirth: dicom = pydicom.read_file(self.files[0])
self.tLine8.setText(self.pName) self.pName = str(dicom.PatientName)
self.tLine8.setStyleSheet("color: green") self.pBirth = str(dicom.PatientBirthDate)
self.tLine9.setText(self.pBirth) has_dicom_files = True
self.tLine9.setStyleSheet("color: green") has_pName = True
else: has_pBirth = True
self.tLine8.setText(notokay) self.tLine7.setText(okay)
self.tLine8.setStyleSheet("color: red") self.tLine7.setStyleSheet("color: green")
self.tLine9.setText(notokay) except:
self.tLine9.setStyleSheet("color: red") self.tLine7.setText(notokay)
self.tLine7.setStyleSheet("color: red")
# Output file count # Output pName and pBirth
self.tLine10.setText(str(len(self.files))) if self.pName and self.pBirth:
self.tLine8.setText(self.pName)
self.tLine8.setStyleSheet("color: green")
self.tLine9.setText(self.pBirth)
self.tLine9.setStyleSheet("color: green")
else:
self.tLine8.setText(notokay)
self.tLine8.setStyleSheet("color: red")
self.tLine9.setText(notokay)
self.tLine9.setStyleSheet("color: red")
# Activate bArchive and show progress bar # Output file count
if dicomfile and dicomdir and target_writable and has_dicom_files and has_pBirth and has_pName: self.tLine10.setText(str(len(self.files)))
self.bArchive.setEnabled(True)
# Activate bArchive and show progress bar
if dicomfile and dicomdir and target_writable and has_dicom_files and has_pBirth and has_pName:
self.bArchive.setEnabled(True)
def copyDataThread(self): def copyDataThread(self):
threads = [] threads = []
@ -197,7 +218,8 @@ class wArchive(QDialog):
threads.append(self.copyDataThread) threads.append(self.copyDataThread)
self.copyDataThread.start() self.copyDataThread.start()
class copyData():
class copyData:
def __init__(self, warchive, barchive, progressbar, files, archivepath, pname, pbirth): def __init__(self, warchive, barchive, progressbar, files, archivepath, pname, pbirth):
self.warchive = warchive self.warchive = warchive
self.barchive = barchive self.barchive = barchive
@ -229,7 +251,7 @@ class copyData():
self.barchive.setEnabled(True) self.barchive.setEnabled(True)
self.warchive.hide() self.warchive.hide()
@staticmethod
def randomString(length): def randomString(length):
string = "" string = ""
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"