-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathFW_Market.py
309 lines (235 loc) · 11.9 KB
/
FW_Market.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import sys
import io
import os
import struct
import time
import zlib
import hashlib
import requests
from contextlib import closing
from lxml import etree
import re
import markdown2
from PyQt5.QtWidgets import QPushButton,QLineEdit,QWidget,QTextEdit,QVBoxLayout,QHBoxLayout,QFileDialog,QLabel
from PyQt5.QtWidgets import QTableWidget,QTableWidgetItem,QAbstractItemView,QFrame,QHeaderView,QMessageBox
from PyQt5.QtCore import Qt,QThread,pyqtSignal
from PyQt5.QtGui import QIcon,QPalette,QColor,QFont
from Markdown_CSS import html_head
from Markdown_CSS import html_tail
from Markdown_CSS import html_test
CMD_SHOW_FORM = 0x00
CMD_CLOSE_FORM = 0x01
CMD_UPDATA_OK = 0x02
CMD_DOWNLOAD_OK = 0x03
class FwThread(QThread):
formSignal = pyqtSignal(int)
textSignal = pyqtSignal(str)
presSignal = pyqtSignal(int)
def __init__(self, action, url, fileName=''):
super(FwThread, self).__init__()
self.action = action
self.url = url
self.fileName = fileName
self.headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}
print(action + " : " + url)
def run(self):
if self.action == "down_bin":#下载Bin文件
with closing(requests.get(self.url, headers=self.headers,stream=True)) as response:
chunkSize = 1024
dateCount = 0
with open(self.fileName,"wb") as file:
for data in response.iter_content(chunk_size=chunkSize):
file.write(data)
dateCount = dateCount + len(data)
self.formSignal.emit(CMD_DOWNLOAD_OK)
return
r = None
try:
r = requests.get(self.url, timeout=10, headers=self.headers)
r.encoding = 'utf-8'#r.apparent_encoding
except Exception as e:
self.formSignal.emit(CMD_CLOSE_FORM)
if r == None:
self.formSignal.emit(CMD_CLOSE_FORM)
return
if self.action == "get_fw_list":#获取gitee上固件文件列表
if r.status_code == 200:
tbodys = re.findall('<div class=\'grid list selection([\w\W]+?)<div class=\'ui tree_progress\'',r.text)
tbody = '<div class=\'grid list selection' + tbodys[0]
self.textSignal.emit(tbody)
elif self.action == "get_bin_url":#获取Bin文件的下载地址
if r.status_code == 200:
tbodys = re.findall('<div class=\'grid list selection([\w\W]+?)<div class=\'ui tree_progress\'',r.text)
tbody = '<div class=\'grid list selection' + tbodys[0]
selector = etree.HTML(tbody) # 转换为lxml解析的对象
titles = selector.xpath('//div/div[@data-type="file"]/div[@data-type="file"]/a/@href') # 这里返回的是一个列表
for each in titles:
title = each.strip() # 去掉字符左右的空格
if title.find('.bin') > 0:
self.textSignal.emit(title)
break
self.formSignal.emit(CMD_CLOSE_FORM) # 获取下载地址失败
elif self.action == "get_readme":#获取说明文档
if r.status_code == 200:
self.textSignal.emit(r.text)
class Doc_From(QWidget): # 用来显示文档的窗口
def __init__(self,parent=None):
super().__init__(parent)
if os.path.exists('combine/aithinker.png'): self.setWindowIcon(QIcon("combine/aithinker.png"))
self.docPage=QTextEdit(self)
self.docPage.setGeometry(5, 5, 600, 350)
self.docPage.setReadOnly(True)
self.docPage.setStyleSheet("background:transparent;border-width:0;border-style:outset")
def set_readme(self,readme):
exts = extras = ['code-friendly', 'fenced-code-blocks', 'footnotes','tables','code-color','pyshell','nofollow','cuddled-lists','header ids','nofollow']
self.docPage.setHtml(html_head + markdown2.markdown(readme, extras=exts) + html_tail)
# self.docPage.setHtml(markdown2.markdown(readme, extras=exts))
# self.docPage.setText(html_test)
def set_title(self, title):
self.setWindowTitle(title)
class FW_Market(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
self.layout=QVBoxLayout(self)
palette = QPalette()
palette.setColor(QPalette.Window, QColor(192,253,123,100))
self.waitPage=QLabel(self)
self.waitPage.setGeometry(0, 0, 600, 350)
self.waitPage.setAlignment(Qt.AlignVCenter)
self.waitPage.setText("<center><font color='red' size='6' line-height='50px';><red>正在获取固件列表......</font></center>")
self.waitPage.setAutoFillBackground(True)
self.waitPage.setPalette(palette)
self.is_First_Show = True
def get_fw_list(self):
if not self.is_First_Show: return
self.mThread = FwThread(action="get_fw_list", url='https://gitee.com/Ai-Thinker-Open/TB_FW_Market')
self.mThread.textSignal.connect(self.show_bin_list)
self.mThread.formSignal.connect(self.waitPag_State)
self.mThread.start()
self.is_First_Show = False
def show_bin_list(self, tbody):
selector = etree.HTML(tbody) # 转换为lxml解析的对象
contents = selector.xpath('//div/div[@data-type="folder"]/div[@data-type="folder"]/a/text()') # 这里返回的是一个列表
messages = selector.xpath('//div/div[@data-type="folder"]/div/div[@class="commit-details"]/a/text()') # 这里返回的是一个列表
timeagos = selector.xpath('//div/div[@data-type="folder"]/div/span[@class="timeago"]/@datetime') # 这里返回的是一个列表
print(contents)
print(messages)
print(timeagos)
self.TableWidget=QTableWidget()
self.TableWidget.setColumnCount(4) # 表格共有四列
self.TableWidget.verticalHeader().setVisible(False) # 隐藏垂直表头
self.TableWidget.horizontalHeader().setVisible(True) # 显示水平表头
font = QFont('微软雅黑', 10)
font.setBold(True) #设置字体加粗
self.TableWidget.horizontalHeader().setFont(font) #设置表头字体
# self.TableWidget.setFrameShape(QFrame.NoFrame) ##设置无表格的外框
self.TableWidget.horizontalHeader().setFixedHeight(25) ##设置表头高度
self.TableWidget.horizontalHeader().setSectionResizeMode(0,QHeaderView.Stretch)#设置第一列宽度自动调整,充满屏幕
# self.TableWidget.horizontalHeader().setStretchLastSection(True) ##设置最后一列拉伸至最大
self.TableWidget.setHorizontalHeaderLabels(['固件名称','固件版本','更新日期','操作']) #设置表头内容
self.TableWidget.horizontalHeader().setSectionsClickable(False)
self.TableWidget.horizontalHeader().setStyleSheet('QHeaderView::section{background:green}')#设置表头的背景色为绿色
self.TableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers) # 不可编辑
self.TableWidget.setSelectionBehavior(QAbstractItemView.SelectRows) #只能选择整行
self.TableWidget.setColumnWidth(1,100)
self.TableWidget.setColumnWidth(2,130)
self.TableWidget.setColumnWidth(3,100)
rows = self.TableWidget.rowCount()
rows_index = 0
if not messages :
for i in range(len(contents)):
messages.append("获取失败")
if not timeagos :
for i in range(len(contents)):
timeagos.append("获取失败")
for content, message, timeago in zip(contents, messages, timeagos):
content = content.strip() # 去掉字符左右的空格
if content.find('@') > 0:
message = message.strip()
timeago = timeago.strip()
self.TableWidget.setRowCount(rows_index + 1)
self.TableWidget.setItem(rows_index, 0, QTableWidgetItem(content))
self.TableWidget.setItem(rows_index, 1, QTableWidgetItem(message))
self.TableWidget.setItem(rows_index, 2, QTableWidgetItem(timeago))
self.TableWidget.setCellWidget(rows_index, 3, self.buttonForRow(rows_index))
rows_index += 1
self.TableWidget.setGeometry(15, 10,300,300)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.addWidget(self.TableWidget)
return 0
def buttonForRow(self,id):
widget=QWidget()
# 修改
downloadBtn = QPushButton('下载')
downloadBtn.setStyleSheet(''' text-align : center;
background-color : NavajoWhite;
height : 30px;
border-style: outset;
font : 13px ''')
downloadBtn.clicked.connect(lambda:self.download(id))
# 查看
docBtn = QPushButton('文档')
docBtn.setStyleSheet(''' text-align : center;
background-color : DarkSeaGreen;
height : 30px;
border-style: outset;
font : 13px; ''')
docBtn.clicked.connect(lambda: self.document(id))
hLayout = QHBoxLayout()
hLayout.addWidget(downloadBtn)
hLayout.addWidget(docBtn)
hLayout.setContentsMargins(5,2,5,2)
widget.setLayout(hLayout)
return widget
def download(self, id):
self.mThread = FwThread(action="get_bin_url", url='https://gitee.com/Ai-Thinker-Open/TB_FW_Market/tree/master/' + self.TableWidget.item(id, 0).text())
self.mThread.textSignal.connect(self.save_File)
self.mThread.formSignal.connect(self.waitPag_State)
self.mThread.start()
self.waitPage.setText("<center><font color='red' size='6' line-height='50px';><red>正在解析下载地址......</font></center>")
self.waitPage.show()
self.waitPage.raise_()
def save_File(self, fileUrl):
tmp = re.findall('master/([\w\W]+?).bin', fileUrl)
tmp = tmp[0].strip() + '.bin'
tmp = re.findall('/([\w\W]+?).bin', tmp)
raw_fileName = tmp[0].strip() + '.bin'
fileName, ok = QFileDialog.getSaveFileName(self, "文件保存", "./combine/" + raw_fileName, "All Files (*);;Bin Files (*.bin)")
fileUrl = fileUrl.replace('/blob/','/raw/')
if ok:
print(fileName)
self.mThread = FwThread(action="down_bin", url="https://gitee.com" + fileUrl, fileName = fileName)
self.mThread.formSignal.connect(self.waitPag_State)
self.mThread.start()
self.waitPage.setText("<center><font color='red' size='6' line-height='50px';><red>正在下载文档......</font></center>")
else:
self.waitPage.hide()
def document(self, id):
self.mThread = FwThread(action="get_readme", url="https://gitee.com/Ai-Thinker-Open/TB_FW_Market/raw/master/" + self.TableWidget.item(id, 0).text() + "/README.md")
self.mThread.textSignal.connect(self.show_document)
self.mThread.formSignal.connect(self.waitPag_State)
self.mThread.start()
self.waitPage.setText("<center><font color='red' size='6' line-height='50px';><red>正在获取文档......</font></center>")
self.waitPage.show()
self.waitPage.raise_()
self.docPage_Title = self.TableWidget.item(id, 0).text() + " 固件使用说明"
def show_document(self, readme):
self.waitPage.hide()
self.win = Doc_From()
self.win.set_readme(readme)
self.win.set_title(self.docPage_Title)
self.win.show()
def waitPag_State(self, state):
if state == CMD_CLOSE_FORM:
self.waitPage.hide()
elif state == CMD_DOWNLOAD_OK:
self.waitPage.hide()
QMessageBox.information(self,"温馨提示","下载成功!")
if __name__ == '__main__':
app=QApplication(sys.argv)
win=FW_Market()
win.show()
sys.exit(app.exec_())