consolidator
/
GuidsWindow.py
196 строк · 7.7 Кб
1import os
2import sys
3import subprocess
4from PySide6.QtGui import QBrush,QColor
5from PySide6.QtCore import (Slot,Qt,QAbstractTableModel)
6from PySide6.QtWidgets import (
7QTableView,
8QTableWidget,
9QPushButton,
10QTableWidgetItem,
11QDialog,
12QLabel,
13QTableWidgetSelectionRange)
14from PySide6.QtUiTools import QUiLoader
15from MyConfig import MyConfig
16from GuidsSync import Synchronizer
17import pandas as pd
18from ExcelPath import ExcelPath
19
20class TableModel(QAbstractTableModel):
21def __init__(self, data:pd.DataFrame):
22super(TableModel, self).__init__()
23self._data = data
24
25def data(self, index, role):
26if role == Qt.DisplayRole:
27value = self._data.iloc[index.row(), index.column()]
28return str(value)
29
30def rowCount(self, index):
31return self._data.shape[0]
32
33def columnCount(self, index):
34return self._data.shape[1]
35
36def headerData(self, section, orientation, role):
37# section is the index of the column/row.
38if role == Qt.DisplayRole:
39if orientation == Qt.Horizontal:
40return str(self._data.columns[section])
41
42if orientation == Qt.Vertical:
43return str(self._data.index[section])
44
45class GuidsWindow(QDialog):
46
47normalButtonColor = "background-color: rgb(236, 236, 236);"
48warningButtonColor = "background-color: rgb(255, 179, 48);"
49warningBrush:QBrush = QBrush(QColor("darkCyan"),bs=Qt.BrushStyle.SolidPattern)
50
51
52def __init__(self) -> None:
53try:
54try:
55self.cur_path = sys._MEIPASS
56except:
57self.cur_path = os.path.dirname(os.path.realpath(__file__))
58super().__init__()
59self.guids_changed = False
60loader = QUiLoader()
61
62# init config
63self.settings = MyConfig(f"{self.cur_path}\\config.json")
64self.guidsDir:str = self.settings.get("GuidsDir_",defaultValue=f"{self.cur_path}\\guids") #bug fix GuidsDir -> GuidsDir_
65self.RemoteGuidsDir:str = self.settings.get("RemoteGuidsDir",defaultValue=None)
66# self.ExcelPath:str = self.settings.get("ExcelPath",defaultValue=None)
67## issue http://vsys01775:8282/flea/pyexcelcons3/-/issues/8
68self.ExcelPath:str = ExcelPath.get()
69
70# init components
71self.GuidsDialog:QDialog = loader.load(f"{self.cur_path}\\GuidsWindow.ui", None)
72self.remoteGuidsButton:QPushButton = self.GuidsDialog.remoteGuidsButton
73self.localGuidsButton:QPushButton = self.GuidsDialog.localGuidsButton
74self.syncWarningLabel:QLabel = self.GuidsDialog.syncWarningLabel
75self.guidsTable:QTableWidget = self.GuidsDialog.guidsTable
76self.dataView:QTableView = self.GuidsDialog.dataView
77
78# init slot connections
79self.remoteGuidsButton.clicked.connect(self.remoteGuidsButton_clicked)
80self.localGuidsButton.clicked.connect(self.localGuidsButton_clicked)
81self.guidsTable.itemSelectionChanged.connect(self.guid_select)
82
83
84# load guids
85self.sync:Synchronizer = None
86self.reload_guids()
87
88except : raise
89
90def show(self):
91try:
92self.GuidsDialog.exec()
93except: raise
94
95def dialog_accepted(self):
96print("accepted")
97
98def init_sync(self):
99try:
100self.sync = Synchronizer(self.RemoteGuidsDir,self.guidsDir)
101self.syncWarningLabel.setVisible(False)
102except Exception as exp:
103if str(exp)=="Bad repo":
104self.syncWarningLabel.setVisible(True)
105pass
106else:
107raise exp
108
109def remoteGuidsButton_clicked(self):
110""" Получить файлы с сервера """
111try:
112if self.sync.local_files_empty: self.sync.init_local()
113else:
114self.sync.sync_server()
115self.guids_changed = True
116self.reload_guids()
117except: raise
118
119def localGuidsButton_clicked(self):
120""" Отправить файлы на сервер """
121try:
122self.sync.sync_local()
123self.reload_guids()
124except: raise
125
126@Slot()
127def open_excel(self):
128"""Открыть excel-файл\n
129Путь к excel.exe должен быть указан в config.json в секции ExcelPath
130"""
131try:
132btn:QPushButton = self.sender()
133cmd = btn.property("cmd")
134print(cmd)
135# res = os.system(cmd)
136res = subprocess.run([self.ExcelPath,cmd],check=True)
137print(f"result: {res.returncode}")
138if res.returncode==0: self.reload_guids()
139if self.sync.newer_local_exist: self.guids_changed = True
140except: raise
141
142def reload_guids(self):
143""" Перезагрузить список справочников """
144try:
145self.init_sync()
146self.guidsTable.setColumnWidth(0,310)
147self.guidsTable.setColumnWidth(1,20)
148self.guidsTable.setColumnWidth(2,20)
149self.guidsTable.setColumnWidth(3,20)
150self.guidsTable.clearContents()
151self.guidsTable.setRowCount(0)
152for key in self.sync.local_files.files.keys():
153row = self.guidsTable.rowCount()
154self.guidsTable.insertRow(row)
155self.guidsTable.setItem(row,0,QTableWidgetItem(key))
156btn:QPushButton = QPushButton(self.guidsTable)
157fullPath = os.path.abspath(f"{self.guidsDir}\\{key}")
158cmd = f'{fullPath}'
159btn.setProperty("cmd",cmd)
160btn.setText("...")
161btn.clicked.connect(self.open_excel)
162self.guidsTable.setCellWidget(row,3,btn)
163
164if key in self.sync.newer_server.keys():
165twi1 =QTableWidgetItem(" ")
166twi1.setBackground(self.warningBrush)
167self.guidsTable.setItem(row,1,twi1)
168if key in self.sync.newer_local.keys():
169twi2 =QTableWidgetItem(" ")
170twi2.setBackground(self.warningBrush)
171self.guidsTable.setItem(row,2,twi2)
172
173if self.sync.newer_server_exist: self.remoteGuidsButton.setStyleSheet(self.warningButtonColor)
174else: self.remoteGuidsButton.setStyleSheet(self.normalButtonColor)
175if self.sync.newer_local_exist: self.localGuidsButton.setStyleSheet(self.warningButtonColor)
176else: self.localGuidsButton.setStyleSheet(self.normalButtonColor)
177if self.sync.local_files_empty and not self.sync.server_files_empty: self.remoteGuidsButton.setStyleSheet(self.warningButtonColor)
178except: raise
179
180@Slot()
181def guid_select(self):
182""" Выбор справочника """
183try:
184table:QTableWidget = self.sender()
185selectedRange = table.selectedRanges()
186if selectedRange is not None and selectedRange.__len__()>0 :
187selected:QTableWidgetSelectionRange = selectedRange[0]
188item = table.item(selected.topRow(),0)
189if item is not None:
190file=f"{self.guidsDir}\\{item.data(0)}"
191print(file)
192df = pd.read_excel(file)
193model = TableModel(df)
194self.dataView.setModel(model)
195
196except: raise