Files
hipa/sendeloghipa.py
2023-05-01 09:59:31 +02:00

326 lines
13 KiB
Python

import inspect
import os
import socket
import time
from qtpy.QtCore import Qt
from qtpy.QtWidgets import (QComboBox, QDialog, QFileDialog, QHBoxLayout,
QLabel, QLineEdit, QPushButton, QTextEdit,
QVBoxLayout)
import elog # https://github.com/paulscherrerinstitute/py_elog
from pyqtacc.bdbase.enumkind import MsgSeverity
from pyqtacc.bdbase.sendelogframe import QSendToELOGFrame
_version = "1.0.0"
_pymodule = os.path.basename(__file__)
_appname, _appext = _pymodule.split(".")
_hostname = socket.gethostname().split(".")[0]
def _line():
"""Macro to return the current line number.
The current line number within the file is used when
reporting messages to the message logging window.
Returns:
int: Current line number.
"""
return inspect.currentframe().f_back.f_lineno
class QSendToELOG(QSendToELOGFrame):
""" Graphical interface to elog
"""
def __init__(self, parent, logbook=None, projektIdx=0, eintragIdx=0,
systemIdx=0, statusIdx=0, ortIdx=2, effektIdx=0, title=None,
message=None, attachFile=None, destination_folder=None):
super().__init__(parent, logbook=logbook, title=title, message=message)
#First check what is the logbook being requested?
#find layout items
self.layout_items = self.get_logbook_specific_items(self.logbook)
self.layout_to_widget_dict = {}
self.projekt_idx = projektIdx
self.eintrag_idx = eintragIdx
self.system_idx = systemIdx
self.status_idx = statusIdx
self.ort_idx = ortIdx
self.effekt_idx = effektIdx
#print("indices", self.projekt_idx, self.eintrag_idx, self.system_idx, self.ort_idx, flush=True)
self.projekt = None
self.eintrag = None
self.system = None
self.effekt = None
self.estatus = None
self.ort = None
self.konsole = None
#print("LAYOUT ITEMS: ", self.layout_items)
self.sim_list = ["Sand", "test"]
self.initialize_layout(self.logbook)
self.exec()
def reset_layout(self):
def remove_wgt(wgt):
self.layout.removeItem(wgt)
while wgt.count():
item = wgt.takeAt(0)
widget = item.widget()
widget.deleteLater()
for layout in self.get_logbook_specific_items(self.logbook):
remove_wgt(self.layout_to_widget_dict[layout])
def create_layout_widgets(self):
if not self.projekt:
self.projekt = QHBoxLayout()
self.projekt.addWidget(QLabel('Projekt: '))
self.projekt_items = QComboBox()
self.projekt_items.setObjectName("Elog")
self.projekt.addWidget(self.projekt_items)
self.layout_to_widget_dict['Projekt'] = self.projekt
if not self.eintrag:
self.eintrag = QHBoxLayout()
self.eintrag.addWidget(QLabel('Eintrag: '))
self.eintrag_items = QComboBox()
self.eintrag_items.setObjectName("Elog")
self.eintrag.addWidget(self.eintrag_items)
self.layout_to_widget_dict['Eintrag'] = self.eintrag
if not self.system:
self.system = QHBoxLayout()
self.system.addWidget(QLabel('System: '))
self.system_items = QComboBox()
self.system_items.setObjectName("Elog")
self.system.addWidget(self.system_items)
self.layout_to_widget_dict['System'] = self.system
if not self.effekt:
self.effekt = QHBoxLayout()
self.effekt.addWidget(QLabel('Effekt: '))
self.effekt_items = QComboBox()
self.effekt_items.setObjectName('Elog')
self.effekt.addWidget(self.effekt_items)
self.layout_to_widget_dict['Effekt'] = self.effekt
if not self.estatus:
self.estatus = QHBoxLayout()
self.estatus.addWidget(QLabel('Status: '))
self.estatus_items = QComboBox()
self.estatus_items.setObjectName("Elog")
self.estatus.addWidget(self.estatus_items)
self.layout_to_widget_dict['Status'] = self.estatus
if not self.ort:
self.ort = QHBoxLayout()
self.ort.addWidget(QLabel('Ort: '))
self.ort_items = QComboBox()
self.ort_items.setObjectName("Elog")
self.ort.addWidget(self.ort_items)
self.layout_to_widget_dict['Ort'] = self.ort
if not self.konsole:
self.konsole = QHBoxLayout()
self.konsole.addWidget(QLabel('Konsole: '))
self.konsole_le = QLineEdit()
self.konsole_le.setObjectName('Elog')
self.konsole_le.setText(_hostname)
self.konsole_le.setFixedWidth(100)
self.konsole_le.setAlignment(Qt.AlignCenter)
self.konsole.addWidget(self.konsole_le)
self.layout_to_widget_dict['Konsole'] = self.konsole
'''
self.konsole = QHBoxLayout()
self.konsole.addWidget(QLabel('Konsole: '))
self.konsole_items = QComboBox()
self.konsole_items.setObjectName("Elog")
self.konsole.addWidget(self.konsole_items)
self.layout_to_widget_dict['Konsole'] = self.konsole
'''
def initialize_layout(self, logbook):
print("initialize_layout", self.layout_items, flush=True)
#Decide on layout
self.create_layout_widgets()
item_no = 2
if 'Projekt' in self.layout_items:
self.projekt_items.clear()
self.projekt_items.addItems(list(self.parent.settings.data[
"ElogBooks"][logbook]['Required']['Projekt']))
self.projekt_items.setCurrentIndex(self.projekt_idx)
self.layout.insertLayout(item_no, self.projekt)
if 'Eintrag' in self.layout_items:
self.eintrag_items.clear()
self.eintrag_items.addItems(list(self.parent.settings.data[
'ElogBooks'][logbook]['Required']['Eintrag']))
self.eintrag_items.setCurrentIndex(self.eintrag_idx)
self.layout.insertLayout(item_no, self.eintrag)
if 'Effekt' in self.layout_items:
item_no += 1
self.effekt_items.clear()
self.effekt_items.addItems(list(self.parent.settings.data[
'ElogBooks'][logbook]['Optional']['Effekt']))
self.effekt_items.setCurrentIndex(self.effekt_idx)
self.layout.insertLayout(item_no, self.effekt)
if 'System' in self.layout_items:
item_no += 1
self.system_items.clear()
self.system_items.addItems(list(self.parent.settings.data[
'ElogBooks'][logbook]['Optional']['System']))
self.system_items.setCurrentIndex(self.system_idx)
self.layout.insertLayout(item_no, self.system)
if 'Status' in self.layout_items:
item_no += 1
self.estatus_items.clear()
self.estatus_items.addItems(list(self.parent.settings.data[
'ElogBooks'][logbook]['Optional']['Status']))
self.estatus_items.setCurrentIndex(self.status_idx)
self.layout.insertLayout(item_no, self.estatus)
if 'Ort' in self.layout_items:
#Optional for HIPA, Required for HIPA-Bestandesaufnahme
item_no += 1
self.ort_items.clear()
_target = "Optional"
if 'Required' in self.parent.settings.data['ElogBooks'][logbook]:
if 'Ort' in self.parent.settings.data['ElogBooks'][logbook][
'Required']:
_target = "Required"
#print("_target", _target, self.ort_idx, flush=True)
#print(list(self.parent.settings.data['ElogBooks'][
# logbook][_target]['Ort']), flush=True)
self.ort_items.addItems(list(self.parent.settings.data['ElogBooks'][
logbook][_target]['Ort']))
#HIPA-Bestandesaufnahme does not have the first two entries that HIPA has
offset = 2 if "Bestand" in logbook else 0
self.ort_items.setCurrentIndex(self.ort_idx - offset)
self.layout.insertLayout(item_no, self.ort)
if 'Konsole' in self.layout_items:
item_no += 1
self.layout.insertLayout(item_no, self.konsole)
'''
self.konsole_items.clear()
self.konsole_items.addItems(list(self.parent.settings.data[
'ElogBooks'][logbook]['Required']['Konsole']))
self.konsole_items.setCurrentIndex(self.konsole_idx)
self.layout.insertLayout(item_no, self.konsole)
'''
print("self.attachFile==>", self.parent.attach_files)
self.attachFile = self.parent.attach_files
self.filesE.clear()
self.files_text = ''
if self.attachFile is not None:
_attachFile = []
if isinstance(self.attachFile, str):
_attachFile.append(self.attachFile)
elif isinstance(self.attachFile, list):
_attachFile = self.attachFile
for i, file in enumerate(_attachFile):
_attach_base = os.path.basename(file)
if i > 0:
self.files_text += "\n"
self.files_text += str(_attach_base)
self.filesE.setText(self.files_text)
self.fflag = True
if any(substring.upper() in logbook.upper() \
for substring in self.sim_list):
_bgcolor = "QComboBox {background: plum; color : black;}"
else:
_bgcolor = "QComboBox {background: lightblue; color : black;}"
self.elog_items.setStyleSheet(_bgcolor)
#have to remove widgets within layout too!
#https://riverbankcomputing.com/pipermail/pyqt/2009-November/025214.html
def on_elog_change(self, idx):
#print(self.elog_items.currentText(), "new=", idx)
#print(self.elog_items.itemText(idx))
#print(self.logbook)
new_logbook = self.elog_items.itemText(idx)
#Meet the new logbook. Same as the old logbook
if new_logbook == self.logbook:
return
self.layout_items = self.get_logbook_specific_items(new_logbook)
print("self.layout_items", self.layout_items)
'''
#Acquire New Layout List
new_items = self.get_logbook_specific_items(new_logbook)
#Acquire Current Layout List
old_items = self.get_logbook_specific_items(self.logbook)
#Compare lists and remove Layouts that do not belong
common_items = list(set(new_items) & set(old_items))
print("common items", common_items)
#remove common_items from old_items
#unwanted_items = [ele for ele in old_items if ele not in common_items]
#print("unwamted items", unwanted_items)
unwanted_items = [ele for ele in old_items if ele not in new_items]
print("unwamted items", unwanted_items)
#Add Layouts that are new
wanted_items = [ele for ele in new_items if ele not in old_items]
print("wanted items", wanted_items)
'''
if any(substring.upper() in new_logbook.upper() \
for substring in self.sim_list):
_bgcolor = "QComboBox {background: plum; color : black;}"
else:
_bgcolor = "QComboBox {background: lightblue; color : black;}"
self.reset_layout()
self.initialize_layout(new_logbook)
self.elog_items.setStyleSheet(_bgcolor)
self.logbook = new_logbook
def ok(self):
el = self.elog_items.currentText()
if 'Projekt' in self.layout_items:
self.attributes['Projekt'] = self.projekt_items.currentText()
if 'Eintrag' in self.layout_items:
self.attributes['Eintrag'] = self.eintrag_items.currentText()
if 'Effekt' in self.layout_items:
self.attributes['Effekt'] = self.effekt_items.currentText()
if 'System' in self.layout_items:
self.attributes['System'] = self.system_items.currentText()
if 'Status' in self.layout_items:
self.attributes['Status'] = self.estatus_items.currentText()
if 'auto' in self.layout_items:
self.attributes['auto'] = self.auto_items.currentText()
if 'Ort' in self.layout_items:
self.attributes['Ort'] = self.ort_items.currentText()
if 'Konsole' in self.layout_items:
self.attributes['Konsole'] = self.konsole_le.text()
QSendToELOGFrame.ok(self)