forked from TheCodeArtist/dtv-demo
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dtv.py
403 lines (318 loc) · 14.5 KB
/
dtv.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python3
import ast
import configparser
import hashlib
import os
import re
import string
import subprocess
from subprocess import PIPE
import sys
from includetree import includeTree
from helper import loadConfig, annotateDTS
from merge import mergeDts
from PyQt6.QtGui import QColor, QDesktopServices
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QApplication, QMainWindow, QFileDialog, QDialog, QHeaderView, QMessageBox
from PyQt6.uic import loadUi
import qdarktheme
DELETED_TAG = "__[|>*DELETED*<|]__"
def getTopLevelItem(trwDT):
return trwDT.topLevelItem(trwDT.topLevelItemCount()-1)
def populateDTS(trwDT, trwIncludedFiles, filename):
# Clear remnants from previously opened file
trwDT.clear()
trwIncludedFiles.expandAll()
with open (filename) as f:
# Read each line in the DTS file
lineNum = 1
for line in f:
# Look for the code (part before the "/*" comment)
idx = line.rfind("/*")
if idx < 0:
lineContents = line.strip()
else:
lineContents = line[:idx].rstrip()
if idx > 0:
# Now pick the comment part of the line
commentFileList = line[idx+2:].strip()[:-2]
# Remove false positive
if "<no-file>:<no-line>" in commentFileList:
commentFileList = None
else:
commentFileList = None
# If found, then clean-up
if commentFileList:
# The last (rightmost) file in the comma-separted list of filename:lineno
# Line numbers are made-up of integers after a ":" colon.
listOfSourcefiles = list(map(lambda f: os.path.realpath(f.strip()), commentFileList.split(',')))
fileWithLineNums = listOfSourcefiles[-1]
if fileWithLineNums:
# Filename is the last (rightmost) word in a forward-slash-separetd path string
includedFilename = fileWithLineNums.split(':', 1)[0].split('/')[-1]
else:
fileWithLineNums = ''
if not fileWithLineNums:
includedFilename = ''
# skip empty line
if not (lineContents.lstrip()):
lineNum += 1
continue
# find deleted tag
isDeleted = DELETED_TAG in lineContents
if isDeleted:
# remove deleted tag and uncomment content
lineContents = lineContents.replace('/* ' + DELETED_TAG + ' */ ', '')
lineContents = re.sub('/\*(.*)?\*/\s*', r'\g<1>', lineContents, flags=re.S)
# Add line to the list
rowItem = QtWidgets.QTreeWidgetItem([str(lineNum), lineContents, includedFilename, fileWithLineNums])
trwDT.addTopLevelItem(rowItem)
# Pick a different background color for each filename
if includedFilename:
colorHash = (int(hashlib.sha1(includedFilename.encode('utf-8')).hexdigest(), 16) % 16) * 4
prevColorHash = colorHash
bgColor = QColor(255-colorHash*2, 240, 192+colorHash)
else:
bgColor = QColor(255, 255, 255)
rowItem.setBackground(1, bgColor)
if isDeleted:
rowItem.setForeground(1, QColor(255, 0, 0))
f = rowItem.font(0)
f.setStrikeOut(True)
f.setBold(True)
rowItem.setFont(1, f)
# Include parents
if commentFileList:
# Skip add parents for close bracket of node
if not (isDeleted and "};" in lineContents.strip()):
for fileWithLineNums in listOfSourcefiles[-2::-1]:
strippedLineNums = fileWithLineNums.split(':', 1)[0]
includedFilename = strippedLineNums.split('/')[-1]
rowItem = QtWidgets.QTreeWidgetItem([str(lineNum), "", includedFilename, fileWithLineNums])
trwDT.addTopLevelItem(rowItem)
item = getTopLevelItem(trwDT)
item.setForeground(0, QColor(255, 255, 255));
elif not isDeleted:
item = getTopLevelItem(trwDT)
item.setForeground(1, QColor(175, 175, 175))
f = item.font(0)
item.setFont(1, f)
lineNum += 1
def populateIncludedFiles(trwIncludedFiles, dtsFile, inputIncludeDirs):
trwIncludedFiles.clear()
dtsIncludeTree = includeTree(dtsFile, inputIncludeDirs)
dummyItem = QtWidgets.QTreeWidgetItem()
dtsIncludeTree.populateChildrenFileNames(dummyItem)
trwIncludedFiles.addTopLevelItem(dummyItem.child(0).clone())
def highlightFileInTree(trwIncludedFiles, fileWithLineNums):
filePath = fileWithLineNums.split(':', 1)[0]
fileName = filePath.split('/')[-1]
items = trwIncludedFiles.findItems(fileName, QtCore.Qt.MatchFlag.MatchRecursive)
currItem = next(item for item in items if item.toolTip(0) == filePath)
# highlight/select current item
trwIncludedFiles.setCurrentItem(currItem)
# highlight/select all its parent items
while (currItem.parent()):
currItem = currItem.parent()
currItem.setSelected(True)
def getLines(fileName, startLineNum, endLineNum):
lines = ''
with open(fileName) as f:
fileLines = f.readlines()
if (startLineNum == endLineNum):
lines = fileLines[startLineNum-1]
else:
for line in range(startLineNum-1, endLineNum):
lines += fileLines[line]
return lines
def showOriginalLineinLabel(lblDT, lineNum, fileWithLineNums):
filePath = fileWithLineNums.split(':', 1)[0]
# extract line numbers in source-file
# TODO: Special Handling for opening and closing braces in DTS
# (no need to show ENTIRE node, right?)
startLineNum = int(re.split('[[:-]', fileWithLineNums)[-4].strip())
endLineNum = int(re.split('[[:-]', fileWithLineNums)[-2].strip())
#print('Line='+str(lineNum), 'Source='+filePath, startLineNum, 'to', endLineNum)
lblDT.setText(getLines(filePath, startLineNum, endLineNum))
def center(window):
# Determine the center of mainwindow
centerPoint = QtCore.QPoint()
centerPoint.setX(main.x() + (main.width()/2))
centerPoint.setY(main.y() + (main.height()/2))
# Calculate the current window's top-left such that
# its center co-incides with the mainwindow's center
frameGm = window.frameGeometry()
frameGm.moveCenter(centerPoint)
# Align current window as per above calculations
window.move(frameGm.topLeft())
class main(QMainWindow):
def __init__(self):
super().__init__()
self.ui = None
self.load_ui()
self.load_signals()
self.findStr = None
self.foundList = []
self.foundIndex = 0
argc = len(sys.argv)
if argc > 1:
dts_file = sys.argv[1]
if argc == 2:
self.openDTSFile(dts_file)
else:
self.openDTSFile(mergeDts(sys.argv[1:]), dts_file)
def openDTSFileUI(self):
fileName, _ = QFileDialog.getOpenFileName(self,
"Select a DTS file to visualise...",
"", "All DTS Files (*.dts)",
)
self.openDTSFile(fileName)
def openDTSFile(self, fileName, baseDtsFileName = None):
# If user selected a file then process it...
if fileName:
# Don't resolve symlinks in path
fileName = os.path.abspath(fileName)
self.ui.setWindowTitle("DTV - " + fileName)
self.findStr = None
self.foundList = []
self.foundIndex = 0
annotatedTmpDTSFileName = None
try:
if baseDtsFileName:
incIncludes = loadConfig(baseDtsFileName)
else:
incIncludes = loadConfig(fileName)
# Resolve symlinks in path
fileName = os.path.realpath(fileName)
annotatedTmpDTSFileName = annotateDTS(fileName, incIncludes)
populateIncludedFiles(self.ui.trwIncludedFiles, fileName, incIncludes)
populateDTS(self.ui.trwDT, self.ui.trwIncludedFiles, annotatedTmpDTSFileName)
except Exception as e:
print('EXCEPTION!', e)
exit(1)
finally:
# Delete temporary file if created
if annotatedTmpDTSFileName:
try:
os.remove(annotatedTmpDTSFileName)
except OSError:
pass
self.trwDT.header().setSectionResizeMode(0, QHeaderView.ResizeMode.ResizeToContents)
self.trwDT.header().setSectionResizeMode(2, QHeaderView.ResizeMode.ResizeToContents)
self.trwDT.header().setSectionHidden(3, True)
self.trwDT.header().resizeSection(1, 500)
def highlightSourceFile(self):
# Skip if no "current" row
if self.ui.trwDT.currentItem() is None:
return
# Skip if current row is "whitespace"
if self.ui.trwDT.currentItem().text(2) == '':
self.ui.lblDT.setText('')
return
# Else identify and highlight the source file of the current row
if self.ui.trwDT.currentItem():
highlightFileInTree(self.ui.trwIncludedFiles, self.ui.trwDT.currentItem().text(3))
showOriginalLineinLabel(self.ui.lblDT, int(self.ui.trwDT.currentItem().text(0)), self.ui.trwDT.currentItem().text(3))
def launchEditor(self, srcFileName, srcLineNum):
# Load configuration for the conf file
config = configparser.ConfigParser()
config.read('dtv.conf')
# Launch user-specified editor
editorCommand = ast.literal_eval(config.get('dtv', 'editor_cmd'))
editorCommandEvaluated = string.Template(editorCommand).substitute(locals())
try:
launchEditor = subprocess.Popen(editorCommandEvaluated.split(),
stdin=None, stdout=None, stderr=None,
close_fds=True)
except FileNotFoundError:
QMessageBox.warning(self,
'DTV',
'Failed to launch editor!\n\n' +
editorCommandEvaluated +
'\n\nPlease modify "dtv.conf" using any text editor.',
QMessageBox.StandardButton.Ok)
def editSourceFile(self):
# TODO: Refactor. Same logic used by showOriginalLineinLabel() too
lineNum = int(self.ui.trwDT.currentItem().text(0))
fileWithLineNums = self.ui.trwDT.currentItem().text(3)
dtsiFileName = fileWithLineNums.split(':')[0].strip()
if dtsiFileName == '':
QMessageBox.information(self,
'DTV',
'No file for the curent line',
QMessageBox.StandardButton.Ok)
return
dtsiLineNum = int(re.split('[[:-]', fileWithLineNums)[-4].strip())
self.launchEditor(dtsiFileName, dtsiLineNum)
def editIncludedFile(self):
includedFileName = self.ui.trwIncludedFiles.currentItem().toolTip(0)
self.launchEditor(includedFileName, '0')
def findTextinDTS(self):
findStr = self.txtFindText.text()
# Very common for use to click Find on empty string
if findStr == "":
return
# New search string ?
if findStr != self.findStr:
self.findStr = findStr
self.foundList = self.trwDT.findItems(self.findStr, QtCore.Qt.MatchContains | QtCore.Qt.MatchRecursive, column=1)
self.foundIndex = 0
numFound = len(self.foundList)
else:
numFound = len(self.foundList)
if numFound:
if ('Prev' in self.sender().objectName()):
# handles btnFindPrev
self.foundIndex = (self.foundIndex - 1) % numFound
else:
# handles btnFindNext and <Enter> on txtFindText
self.foundIndex = (self.foundIndex + 1) % numFound
if numFound:
self.trwDT.setCurrentItem(self.foundList[self.foundIndex])
def showSettings(self):
QMessageBox.information(self,
'DTV',
'Settings GUI NOT supported yet.\n'
'Please modify "dtv.conf" using any text editor.',
QMessageBox.StandardButton.Ok)
return
def center(self):
frameGm = self.frameGeometry()
centerPoint = self.screen().availableGeometry().center()
frameGm.moveCenter(centerPoint)
self.move(frameGm.topLeft())
def load_ui(self):
self.ui = loadUi('dtv.ui', self)
self.ui.openDTS.triggered.connect(self.openDTSFileUI)
self.ui.exitApp.triggered.connect(self.close)
self.ui.optionsSettings.triggered.connect(self.showSettings)
self.ui.trwDT.currentItemChanged.connect(self.highlightSourceFile)
self.ui.trwDT.itemDoubleClicked.connect(self.editSourceFile)
self.ui.trwIncludedFiles.itemDoubleClicked.connect(self.editIncludedFile)
self.ui.btnFindPrev.clicked.connect(self.findTextinDTS)
self.ui.btnFindNext.clicked.connect(self.findTextinDTS)
self.ui.txtFindText.returnPressed.connect(self.findTextinDTS)
self.trwDT.setHeaderLabels(['Line No.', 'DTS content ....', 'Source File', 'Full path'])
self.center()
self.show()
def load_signals(self):
pass
try:
subprocess.run('which cpp dtc', stdout=PIPE, stderr=PIPE, shell=True, check=True)
except subprocess.CalledProcessError as e:
print('EXCEPTION!', e)
print('stdout: {}'.format(e.output.decode(sys.getfilesystemencoding())))
print('stderr: {}'.format(e.stderr.decode(sys.getfilesystemencoding())))
exit(e.returncode)
try:
subprocess.run('dtc --annotate -h', stdout=PIPE, stderr=PIPE, shell=True, check=True)
except subprocess.CalledProcessError as e:
print('EXCEPTION!', e)
print('EXCEPTION!', 'dtc version it too old and it doesn\'t support "annotate" option')
exit(e.returncode)
app = QApplication(sys.argv)
qdarktheme.setup_theme("light")
main = main()
# Blocks till Qt app is running, returns err code if any
qtReturnVal = app.exec()
sys.exit(qtReturnVal)