Files
sls/sendelogsls.py
2023-02-21 15:13:20 +01:00

339 lines
13 KiB
Python

import getpass
import inspect
import os
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(".")
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, autoIdx=2, title=None, message=None,
attachFile=None, destination_folder=None):
#super(QSendToELOGFrame, self).__init__(
# parent, logbook=logbook, title=title, message=message,
# attachFile=attachFile,
# destination_folder=destination_folder)
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.auto_idx = autoIdx
print("indices", self.projekt_idx, self.eintrag_idx, self.system_idx, self.auto_idx)
self.projekt = None
self.eintrag = None
self.system = None
self.effekt = None
self.estatus = None
self.auto = 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_le = QLineEdit()
self.effekt_le.setObjectName('Elog')
self.effekt_le.setText(str(""))
self.effekt_le.setFixedWidth(300)
self.effekt_le.setAlignment(Qt.AlignCenter)
self.effekt.addWidget(self.effekt_le)
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.auto:
self.auto = QHBoxLayout()
self.auto.addWidget(QLabel('auto: '))
self.auto_items = QComboBox()
self.auto_items.setObjectName("Elog")
self.auto.addWidget(self.auto_items)
self.layout_to_widget_dict['auto'] = self.auto
def initialize_layout(self, logbook):
#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.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 'auto' in self.layout_items:
item_no += 1
self.auto_items.clear()
self.auto_items.addItems(list(self.parent.settings.data[
'ElogBooks'][logbook]['Optional']['auto']))
self.auto_items.setCurrentIndex(self.auto_idx)
self.layout.insertLayout(item_no, self.auto)
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):
#proj = self.projekt_items.currentText()
#if not proj:
# self.messagelbl.setText('Projekt attribute is required')
# return
#self.attributes['Projekt'] = proj
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_le.text()
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 el == 'SLS':
self.attributes['Eintrag'] = self.eintrag_items.currentText()
self.attributes['Effekt'] = self.effekt_le.text()
self.attributes['System'] = self.system_items.currentText()
elif el == 'Sandkasten':
self.attributes['Eintrag'] = self.eintrag_items.currentText()
self.attributes['Status'] = "Geschlossen"
self.attributes['When'] = str(time.time())
self.attributes['Wann'] = str(time.time())
'''
QSendToELOGFrame.ok(self)
#self.close()
'''
def clearFiles(self):
self.attachFile = []
self.filesE.clear()
self.files = []
self.files_text = ''
self.fflag = False
def openFiles(self):
# Notethat openFiles also gets called when qDialog cacnel is clicked!
qFileDialog = QFileDialog()
extensions = ("Images (*.bmp *.eps *.gif *.jpeg *.jpg *.pdf *.png " +
"*.ps *.tiff *.xpm);;Text files (*.txt *.text);;" +
"JSON/XML files (*.json *.xml)")
flocal = qFileDialog.getOpenFileNames(
self, 'Open File', self.destination, extensions)[0]
if not flocal:
return
self.files.append(flocal[0])
if self.fflag:
self.files_text += '\n'
self.files_text += str(flocal[0].split('/')[-1])
self.fflag = True
self.filesE.setText(self.files_text)
def receive_elog_notification(self, is_accepted, logbook_url, elog_message):
#Receive notification from ELOG, and report to log window
yes_no = "made" if is_accepted else "failed"
mess = "Entry into ELOG: {0} {1}".format(logbook_url, yes_no)
if is_accepted:
self.parent.show_log_message(MsgSeverity.INFO, self.pymodule,
_line(), mess)
else:
mess += ".\n" + elog_message
self.parent.show_log_message(MsgSeverity.WARN, self.pymodule,
_line(), mess)
self.parent.statusbar.showMessage(mess)
'''