dicom-extractor-2023/warchive.py

264 lines
9.3 KiB
Python

import os
import glob
import threading
import time
import pydicom
import shutil
import random
import ctypes
import wmi
from registry import Registry
from PyQt6.QtWidgets import QDialog
from PyQt6.QtWidgets import QLabel
from PyQt6.QtWidgets import QPushButton
from PyQt6.QtWidgets import QStyle
from PyQt6.QtWidgets import QProgressBar
class wArchive(QDialog):
def __init__(self):
super().__init__()
self.setFixedSize(400, 400)
self.setWindowTitle("Archiviere...")
self.setStyleSheet("background-color: white")
pixmap = QStyle.StandardPixmap.SP_DriveNetIcon
icon = self.style().standardIcon(pixmap)
self.setWindowIcon(icon)
column1w = 180
column2s = column1w + 20
column2w = 400 - column2s - 10
self.lLine1 = QLabel("Starte Archivierung:", self)
self.lLine1.setGeometry(10, 10, column1w, 20)
self.tLine1 = QLabel("", self)
self.tLine1.setGeometry(column2s, 10, column2w, 20)
self.lLine2 = QLabel("Pfad zum Archiv:", self)
self.lLine2.setGeometry(10, 30, column1w, 20)
self.tLine2 = QLabel("", self)
self.tLine2.setGeometry(column2s, 30, column2w, 20)
self.lLine3 = QLabel("CD-Rom-Laufwerk:", self)
self.lLine3.setGeometry(10, 50, column1w, 20)
self.tLine3 = QLabel("", self)
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.setGeometry(10, 90, column1w, 20)
self.tLine4 = QLabel("", self)
self.tLine4.setGeometry(column2s, 90, column2w, 20)
self.lLine5 = QLabel("DICOM-Verzeichnis gefunden:", self)
self.lLine5.setGeometry(10, 110, column1w, 20)
self.tLine5 = QLabel("", self)
self.tLine5.setGeometry(column2s, 110, column2w, 20)
self.lLine6 = QLabel("Archiv beschreibbar:", self)
self.lLine6.setGeometry(10, 130, column1w, 20)
self.tLine6 = QLabel("", self)
self.tLine6.setGeometry(column2s, 130, column2w, 20)
self.lLine7 = QLabel("DICOM-Dateien gefunden:", self)
self.lLine7.setGeometry(10, 150, column1w, 20)
self.tLine7 = QLabel("", self)
self.tLine7.setGeometry(column2s, 150, column2w, 20)
self.lLine8 = QLabel("Name:", self)
self.lLine8.setGeometry(10, 170, column1w, 20)
self.tLine8 = QLabel("", self)
self.tLine8.setGeometry(column2s, 170, column2w, 20)
self.lLine9 = QLabel("Geburtsdatum:", self)
self.lLine9.setGeometry(10, 190, column1w, 20)
self.tLine9 = QLabel("", self)
self.tLine9.setGeometry(column2s, 190, column2w, 20)
self.lLine10 = QLabel("Dateien:", self)
self.lLine10.setGeometry(10, 210, column1w, 20)
self.tLine10 = QLabel("", self)
self.tLine10.setGeometry(column2s, 210, column2w, 20)
self.bArchive = QPushButton("Archivieren", self)
self.bArchive.setGeometry(10, 350, 380, 40)
self.bArchive.setEnabled(False)
self.bArchive.clicked.connect(self.copyDataThread)
self.progressBar = QProgressBar(self)
self.progressBar.setGeometry(10, 320, 380, 20)
archive = threading.Thread(target=self.archive)
archive.start()
time.sleep(0.1)
self.exec()
def archive(self):
okay = ""
notokay = ""
self.tLine1.setText(okay)
self.tLine1.setStyleSheet("color: green")
reg = Registry()
self.ArchivePath, self.CDRomPath = reg.regRead()
self.tLine2.setText(self.ArchivePath)
self.tLine3.setText(self.CDRomPath + ":\\")
# Logic
cd_check = False
dicomfile = False
dicomdir = False
target_writable = False
has_dicom_files = False
has_pName = False
has_pBirth = False
# Check CD drive
cds = wmi.WMI()
for cd in cds.Win32_CDROMDrive():
if cd.MediaLoaded:
cd_check = True
if cd_check:
self.tLine3a.setText(okay)
self.tLine3a.setStyleSheet("color: green")
else:
self.tLine3a.setText(notokay)
self.tLine3a.setStyleSheet("color: red")
if cd_check:
# Check if DICOMDIR file exists
if os.path.isfile(os.path.join(self.CDRomPath + ":", os.sep, "DICOMDIR")):
dicomfile = True
self.tLine4.setText(okay)
self.tLine4.setStyleSheet("color: green")
else:
self.tLine4.setText(notokay)
self.tLine4.setStyleSheet("color: red")
# Check if DICOM directory exists
if os.path.isdir(os.path.join(self.CDRomPath + ":", os.sep, "DICOM")):
dicomdir = True
self.tLine5.setText(okay)
self.tLine5.setStyleSheet("color: green")
else:
self.tLine5.setText(notokay)
self.tLine5.setStyleSheet("color: red")
# Check if ArchivePath is writable
if os.access(self.ArchivePath, os.W_OK) is True:
target_writable = True
self.tLine6.setText(okay)
self.tLine6.setStyleSheet("color: green")
else:
self.tLine6.setText(notokay)
self.tLine6.setStyleSheet("color: red")
# List of all files under DICOM directory
path = os.path.join(self.CDRomPath + ":", os.sep, "DICOM")
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)
# Get PatientsName from first file
self.pName = ""
self.pBirth = ""
try:
dicom = pydicom.read_file(self.files[0])
self.pName = str(dicom.PatientName)
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
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")
# Output file count
self.tLine10.setText(str(len(self.files)))
# 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):
threads = []
self.copyDataThread = threading.Thread(target=copyData, args=(self,
self.bArchive,
self.progressBar,
self.files,
self.ArchivePath,
self.pName,
self.pBirth,))
threads.append(self.copyDataThread)
self.copyDataThread.start()
class copyData:
def __init__(self, warchive, barchive, progressbar, files, archivepath, pname, pbirth):
self.warchive = warchive
self.barchive = barchive
self.progressbar = progressbar
self.files = files
self.archivepath = archivepath
self.pname = pname
self.pbirth = pbirth
self.barchive.setEnabled(False)
mainFolder = os.path.join(self.archivepath, self.pname + "^" + self.pbirth)
try:
os.mkdir(mainFolder)
except:
pass
subFolder = randomString(4)
os.mkdir(os.path.join(mainFolder, subFolder))
filename = 1
for file in self.files:
shutil.copy(file, os.path.join(mainFolder, subFolder, str(filename)))
self.progressbar.setValue(int(filename / len(self.files) * 100))
filename += 1
ctypes.windll.WINMM.mciSendStringW(u"set cdaudio door open", None, 0, None)
self.barchive.setEnabled(True)
self.warchive.hide()
def randomString(length):
string = ""
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
for _ in range(length):
char = random.choice(chars)
string = string + char
return string