-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
827 lines (758 loc) · 37.6 KB
/
main.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
"""
i386ide is lightweight IDE for i386 assembly and C programming language.
Copyright (C) 2019 Dušan Erdeljan, Marko Njegomir
This file is part of i386ide.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
"""
import sys
import os
import pickle
import re
import platform
import subprocess
from PySide2.QtWidgets import QMainWindow, QLineEdit, QApplication, QFileDialog, QMessageBox, QDockWidget, QLabel, QInputDialog
from PySide2.QtCore import Qt, QDir, QTimer
from PySide2.QtGui import QIcon
from src.view.MenuBar import MenuBar
from src.view.Terminal import Terminal
from src.view.ToolBar import ToolBar
from src.view.StatusBar import StatusBar
from src.view.TreeView import TreeView
from src.view.HelpWidget import HelpWidget
from src.view.TabWidget import EditorTabWidget, EditorTab
from src.view.WorkspaceConfigurationEditor import WorkspaceConfigurationEditor
from src.view.DefaultWorkspaceEditor import DefaultWorkspaceEditor
from src.view.AsciiTableWidget import AsciiTableWidget
from src.view.SnippetEditor import SnippetEditor
from src.view.SettingsEditor import SettingsEditor
from src.view.AboutDialog import AboutDialog
from src.view.TabSwitcher import TabSwitcher
from src.view.ProjectSwitcher import ProjectSwitcher
from src.view.GettingStartedDialog import GettingStartedDialog
from src.util.AsemblerSintaksa import AsemblerSintaksa
from src.util.CSyntax import CSyntax
from src.model.ProjectNode import ProjectNode, ProjectProxy
from src.model.AssemblyFileNode import AssemblyFileNode
from src.model.CFileNode import CFileNode
from src.model.WorkspaceNode import WorkspaceNode, WorkspaceProxy
from src.model.FileNode import FileProxy
from src.controller.ConfigurationManager import ConfigurationManager
from src.controller.WorkspaceConfiguration import WorkspaceConfiguration
from src.controller.PathManager import PathManager
from src.controller.SnippetManager import SnippetManager
from src.controller.TooltipManager import TooltipManager
from time import localtime, strftime
class AsemblerIDE(QMainWindow):
def __init__(self):
super(AsemblerIDE, self).__init__()
self.workspace = None
self.backupTimer = 300000
PathManager.START_DIRECTORY = os.getcwd()
self.workspaceConfiguration = WorkspaceConfiguration.loadConfiguration()
self.snippetManager = SnippetManager.loadSnippetConfiguration()
self.tooltipManager = TooltipManager.loadTooltipConfiguration()
self.configurationManager = ConfigurationManager()
self.editorTabs = EditorTabWidget(self.snippetManager, self.tooltipManager)
self.menuBar = MenuBar()
self.terminal = Terminal()
self.toolBar = ToolBar(self.configurationManager)
self.statusBar = StatusBar()
self.treeView = TreeView(self.configurationManager)
self.help = HelpWidget()
self.ascii = AsciiTableWidget()
self.setStatusBar(self.statusBar)
self.addToolBar(self.toolBar)
self.addDockWidget(Qt.BottomDockWidgetArea, self.terminal)
self.addDockWidget(Qt.RightDockWidgetArea, self.help)
self.addDockWidget(Qt.RightDockWidgetArea, self.ascii)
self.splitDockWidget(self.help, self.ascii, Qt.Vertical)
self.treeDock = QDockWidget()
self.treeDock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.treeDock.setStyleSheet("background-color: #2D2D30; color: white;")
self.treeDock.setFeatures(QDockWidget.DockWidgetMovable| QDockWidget.DockWidgetClosable)
self.treeDock.setWindowTitle("Workspace explorer")
self.treeDock.setWidget(self.treeView)
header = QLabel("Workspace explorer")
# header.setStyleSheet("background-color: #007ACC;")
self.treeDock.setTitleBarWidget(header)
self.addDockWidget(Qt.LeftDockWidgetArea, self.treeDock)
self.setMenuBar(self.menuBar)
self.setMinimumSize(1200, 800)
self.setWindowTitle("i386 Assembly Integrated Development Environment")
self.setCentralWidget(self.editorTabs)
self.setStyleSheet("background-color: #3E3E42; color: white;")
self.setWindowIcon(QIcon(resource_path("resources/app_icon.ico")))
self.addTabWidgetEventHandlers()
self.addMenuBarEventHandlers()
self.addToolBarEventHandlers()
self.addTreeViewEventHandlers()
self.checkWorkspaceConfiguration()
#self.populateTreeView()
#self.statusBar.comboBox.currentTextChanged.connect(self.changeEditorSyntax)
self.statusBar.tabWidthComboBox.currentTextChanged.connect(self.changeEditorTabWidth)
self.timer = QTimer()
self.timer.start(self.backupTimer)
self.timer.timeout.connect(self.makeBackupSave)
self.terminal.console.setFocus()
self.tabSwitcher = TabSwitcher(self.editorTabs)
self.tabSwitcher.hide()
self.projectSwitcher = ProjectSwitcher(self.configurationManager, self.toolBar.projectComboBox)
self.projectSwitcher.hide()
self.terminal.projectSwitchRequested.connect(self.showProjectSwitcher)
self.terminal.tabSwitchRequested.connect(self.showTabSwitcher)
self.editorTabs.tabSwitchRequested.connect(self.showTabSwitcher)
self.editorTabs.projectSwitchRequested.connect(self.showProjectSwitcher)
self.helpDocsDialog = GettingStartedDialog()
self.helpDocsDialog.hide()
def makeBackupSave(self):
self.workspace.saveBackup()
self.timer.setInterval(self.backupTimer) # interval has to be reset cause the timer may have been paused
def changeEditorTabWidth(self, text):
currentTab: EditorTab = self.editorTabs.getCurrentTab()
if currentTab:
currentTab.widget.editor.tabSize = int(text)
def changeEditorSyntax(self, text):
currentTab: EditorTab = self.editorTabs.getCurrentTab()
if currentTab:
if text == "Assembly":
currentTab.widget.editor.sintaksa = AsemblerSintaksa(currentTab.widget.editor.document())
elif text == "C":
currentTab.widget.editor.sintaksa = CSyntax(currentTab.widget.editor.document())
currentTab.widget.editor.update()
def checkWorkspaceConfiguration(self):
defaultWorkspace = self.workspaceConfiguration.getDefaultWorkspace()
if defaultWorkspace:
if self.openWorkspaceAction(defaultWorkspace):
self.show()
return
else:
self.workspaceConfiguration.removeWorkspace(defaultWorkspace)
dialog = WorkspaceConfigurationEditor(self.workspaceConfiguration, self)
if dialog.exec_():
self.show()
else:
sys.exit(0)
def addTabWidgetEventHandlers(self):
self.editorTabs.currentChanged.connect(self.activeTabChanged)
def addTreeViewEventHandlers(self):
self.treeView.fileDoubleCliked.connect(self.loadFileText)
self.treeView.workspaceReload.connect(lambda wsProxy: self.openWorkspaceAction(wsProxy.path, updateWorkspace=True))
self.treeView.workspaceRename.connect(lambda oldPath, wsProxy: self.workspaceConfiguration.replaceWorkpsace(oldPath, wsProxy.path))
self.treeView.newProjectAdded.connect(lambda: self.toolBar.updateComboBox())
self.treeView.projectCompile.connect(lambda proxy: self.compileAction(proxy))
self.treeView.projectDebug.connect(lambda proxy: self.debugAction(proxy))
self.treeView.projectRun.connect(lambda proxy: self.runAction(proxy))
self.treeView.projectRemove.connect(lambda proxy: self.removeProject(proxy))
self.treeView.projectRename.connect(lambda oldPath, project: self.renameProject(oldPath, project))
self.treeView.fileRemove.connect(lambda fileProxy: self.removeFile(fileProxy))
self.treeView.fileRename.connect(lambda oldPath, fileProxy: self.renameFile(oldPath, fileProxy))
self.treeView.fileSave.connect(lambda fileProxy: self.updateEditorTrie(fileProxy))
self.treeView.invalidWorkspace.connect(self.invalidWorkspace)
self.treeView.projectSave.connect(self.saveProject)
self.treeView.quickAssemblyFile.connect(self.loadFileText)
self.treeView.newFile.connect(self.loadFileText)
def saveProject(self, projectProxy: ProjectProxy):
self.saveAllFiles(projectProxy)
def invalidWorkspace(self, workspace: WorkspaceNode):
workspace.deleted = True
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("Workspace '{}' has been deleted from the disk.".format(workspace.path))
msg.setWindowTitle("Invalid workspace")
msg.exec_()
if workspace.path in self.workspaceConfiguration.getWorkspaces():
self.workspaceConfiguration.removeWorkspace(workspace.path)
self.switchWorkspaceAction()
def renameFile(self, oldPath: str, fileProxy: FileProxy):
# fileProxy.text = None
key = "{}/{}".format(fileProxy.parent.path, oldPath)
if key in self.editorTabs.projectTabs:
newKey = "{}/{}".format(fileProxy.parent.path, fileProxy.path)
tab = self.editorTabs.projectTabs.pop(key)
self.editorTabs.projectTabs[newKey] = tab
tab.tabName = newKey
index = self.editorTabs.tabs.index(fileProxy)
tabText = newKey+"*" if fileProxy.hasUnsavedChanges else newKey
self.editorTabs.setTabText(index, tabText)
def renameProject(self, oldPath: str, project: ProjectNode):
self.toolBar.updateComboBox()
for fileProxy in project.proxy.files:
oldKey = "{}/{}".format(oldPath, fileProxy.path)
if oldKey in self.editorTabs.projectTabs:
newKey = "{}/{}".format(project.proxy.path, fileProxy.path)
tab = self.editorTabs.projectTabs.pop(oldKey)
self.editorTabs.projectTabs[newKey] = tab
tab.tabName = newKey
index = self.editorTabs.tabs.index(fileProxy)
tabText = newKey + "*" if fileProxy.hasUnsavedChanges else newKey
self.editorTabs.setTabText(index, tabText)
def removeFile(self, proxy: FileProxy):
key = "{}/{}".format(proxy.parent.path, proxy.path)
if key in self.editorTabs.projectTabs:
self.editorTabs.closeTab(self.editorTabs.tabs.index(proxy), askToSave=False)
def removeProject(self, proxy: ProjectProxy):
for file in proxy.files:
if file in self.editorTabs.tabs:
self.editorTabs.closeTab(self.editorTabs.tabs.index(file), askToSave=False)
self.toolBar.updateComboBox()
def checkIfWorkspaceDeleted(self):
if not os.path.exists(self.workspace.path):
self.workspace.deleted = True
def activeTabChanged(self, index):
if index == -1:
self.statusBar.tabWidthComboBox.setCurrentText('4')
return
syntax = "Assembly" if self.editorTabs.tabs[index].path[-1].lower() == "s" else "C"
proxy = self.editorTabs.tabs[index]
key = "{}/{}".format(proxy.parent.path, proxy.path)
self.statusBar.comboBox.setCurrentText(syntax)
self.statusBar.tabWidthComboBox.setCurrentText(str(self.editorTabs.projectTabs[key].widget.editor.tabSize))
#self.changeEditorSyntax(syntax)
def populateTreeView(self):
workspace = WorkspaceNode()
workspace.setText(0, "My workspace")
self.treeView.setRoot(workspace)
for i in range(5):
project = ProjectNode()
project.setText(0, "My Project {}".format(i+1))
assemblyFile = AssemblyFileNode()
assemblyFile.setText(0, "procedure_{}.S".format(i+1))
cFile = CFileNode()
cFile.setText(0, "main_{}.c".format(i+1))
self.treeView.addNode(workspace, project)
self.treeView.addNode(project, assemblyFile)
self.treeView.addNode(project, cFile)
project.setExpanded(True)
self.workspace = workspace
def closeEvent(self, event):
if self.tabSwitcher.isActiveWindow():
self.tabSwitcher.hide()
if self.helpDocsDialog.isVisible():
self.helpDocsDialog.hide()
for proxy in self.editorTabs.tabs:
if proxy.hasUnsavedChanges:
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setParent(None)
msg.setModal(True)
msg.setWindowTitle("Confirm Exit")
msg.setText("The file {}/{} has been modified.".format(proxy.parent.path, proxy.path))
msg.setInformativeText("Do you want to save the changes?")
msg.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
msg.setDefaultButton(QMessageBox.Save)
retValue = msg.exec_()
if retValue == QMessageBox.Save:
if not self.saveFileAction():
event.ignore()
return
elif retValue == QMessageBox.Discard:
pass
else:
event.ignore()
return
self.workspaceConfiguration.saveConfiguration()
self.snippetManager.saveConfiguration()
self.tooltipManager.saveConfiguration()
self.checkIfWorkspaceDeleted()
if not self.workspace.deleted:
self.workspace.proxy.closedNormally = True
self.saveWorkspaceAction()
else:
if self.workspace.path in self.workspaceConfiguration.getWorkspaces():
self.workspaceConfiguration.removeWorkspace(self.workspace.path)
super(AsemblerIDE, self).closeEvent(event)
def addMenuBarEventHandlers(self):
self.menuBar.quickAssemblyProjectAction.triggered.connect(self.quickAssemblyProject)
self.menuBar.newWorkspaceAction.triggered.connect(self.newWorkspaceAction)
self.menuBar.saveWorkspaceAction.triggered.connect(self.saveWorkpsaceAllFiles)
self.menuBar.openWorkspaceAction.triggered.connect(self.openWorkspaceAction)
self.menuBar.switchWorkspaceAction.triggered.connect(self.switchWorkspaceAction)
self.menuBar.saveAction.triggered.connect(self.saveFileAction)
self.menuBar.findAction.triggered.connect(self.findAction)
self.menuBar.editDefaultWorkspace.triggered.connect(self.editDefaultWorkspaceConfiguration)
self.menuBar.editCodeSnippets.triggered.connect(self.editCodeSnippets)
self.menuBar.editSettings.triggered.connect(self.editSettings)
self.menuBar.aboutAction.triggered.connect(self.showAbout)
self.menuBar.helpAction.triggered.connect(self.showGettingStarted)
self.menuBar.view.addAction(self.terminal.toggleViewAction())
self.menuBar.view.addAction(self.treeDock.toggleViewAction())
self.menuBar.view.addAction(self.help.toggleViewAction())
self.menuBar.view.addAction(self.ascii.toggleViewAction())
self.menuBar.view.addAction(self.toolBar.toggleViewAction())
def quickAssemblyProject(self):
if self.workspace:
self.workspace.createQuickAssemblyProject()
def showAbout(self):
dialog = AboutDialog()
dialog.exec_()
def showGettingStarted(self):
if self.helpDocsDialog.isHidden():
self.helpDocsDialog.show()
def findAction(self):
currentTab: EditorTabWidget = self.editorTabs.getCurrentTab()
if not currentTab:
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("Cannot open file and replace window because there is no open file at the moment.")
msg.setWindowTitle("Find & Replace error")
msg.exec_()
return
currentTab.widget.find.setVisible(True)
if currentTab.widget.editor.lastFind:
currentTab.widget.find.findLabel.setText(currentTab.widget.editor.lastFind)
currentTab.widget.find.findLabel.setFocus()
def switchWorkspaceAction(self):
remaining = self.timer.remainingTime()
self.timer.stop() # timer for creating backups needs to be paused when switching ws
dialog = WorkspaceConfigurationEditor(self.workspaceConfiguration, self, switch=True)
if dialog.exec_() and dialog.workspaceDirectory:
self.workspace.proxy.closedNormally = True
self.saveWorkspaceAction()
if not self.editorTabs.closeAllTabs():
self.timer.start(remaining) # timer for saving backups is resumed
return
self.openWorkspaceAction(dialog.workspaceDirectory)
self.timer.start(remaining) # timer for saving backups is resumed
def editDefaultWorkspaceConfiguration(self):
editor = DefaultWorkspaceEditor(self.workspaceConfiguration)
if editor.exec_():
self.workspaceConfiguration.saveConfiguration()
def editCodeSnippets(self):
editor = SnippetEditor(self.snippetManager)
if editor.exec_():
self.snippetManager.saveConfiguration()
def editSettings(self):
editor = SettingsEditor(self.tooltipManager)
if editor.exec_():
self.tooltipManager.saveConfiguration()
def newWorkspaceAction(self):
remaining = self.timer.remainingTime()
self.timer.stop() # timer for creating backups needs to be paused when switching ws
if not self.editorTabs.closeAllTabs():
self.timer.start(remaining) # timer for saving backups is resumed
return False
self.workspace.proxy.closedNormally = True
self.saveWorkspaceAction()
workspace = WorkspaceNode()
name = QFileDialog.getExistingDirectory(self, "New workspace", "select new workspace directory")
if name:
path = os.path.join(name, ".metadata")
backup_path = os.path.join(name, ".backup")
if os.path.isdir(path) or os.path.isdir(backup_path):
self.msgInvalidFolderError(name)
self.timer.start(remaining) # timer for saving backups is resumed
return
wsname = name[name.rindex(os.path.sep)+1:]
regex = re.compile('[@!#$%^&*()<>?/\|}{~:]')
if ' ' in name or regex.search(wsname):
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("Workspace path/name cannot contain whitespace or special characters.")
msg.setWindowTitle("Workspace creation error")
msg.exec_()
self.timer.start(remaining) # timer for saving backups is resumed
return False
workspace.path = name
proxy = WorkspaceProxy()
proxy.path = name
workspace.proxy = proxy
workspace.setIcon(0, QIcon(resource_path("resources/workspace.png")))
workspace.setText(0, wsname)
self.workspace = workspace
self.treeView.setRoot(self.workspace)
self.saveWorkspaceAction()
self.configurationManager.allProjects = []
self.configurationManager.currentProject = None
self.toolBar.updateComboBox()
self.terminal.executeCommand("cd {}".format(self.workspace.path))
self.workspaceConfiguration.addWorkspace(self.workspace.proxy.path)
self.timer.start(remaining) # timer for saving backups is resumed
return True
self.timer.start(remaining) # timer for saving backups is resumed
return False
def saveWorkspaceAction(self, workspacePath=None):
if self.workspace:
self.workspace.saveWorkspace(workspacePath)
def saveWorkpsaceAllFiles(self):
self.saveAllFiles()
self.saveWorkspaceAction()
def updateProjectList(self):
self.configurationManager.allProjects = []
self.configurationManager.currentProject = None
projects = self.treeView.getProjects()
if len(projects):
self.configurationManager.allProjects = self.treeView.getProjects()
self.configurationManager.currentProject = projects[0]
self.toolBar.updateComboBox()
def openWorkspaceAction(self, workspacePath=None, updateWorkspace=False):
if not self.editorTabs.closeAllTabs():
return
if not workspacePath:
workspacePath = QFileDialog.getExistingDirectory(self, "Open workspace", "select new workspace directory")
if not workspacePath:
return
regex = re.compile('[@!#$%^&*()<>?/\|}{~:]')
if ' ' in workspacePath or regex.search(os.path.basename(workspacePath)):
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("Workspace path/name cannot contain whitespace or special characters.")
msg.setWindowTitle("Workspace creation error")
msg.exec_()
return False
path = os.path.join(workspacePath, ".metadata")
backup_path = os.path.join(workspacePath, ".backup")
if os.path.isdir(path) or os.path.isdir(backup_path):
self.msgInvalidFolderError(workspacePath)
return
workspace = WorkspaceProxy()
self.workspace = WorkspaceNode()
if os.path.exists(path):
try: # in try block in case there is a corrupted .metadata file on the path
with open(path, 'rb') as file:
workspace = pickle.load(file)
except:
workspace.closedNormally = False # set it to false to trigger backup msg in case .metadata is corrupted
elif not os.path.exists(backup_path): # creates a .metadata file for a clean new workspace
self.workspace.proxy = workspace
self.applyWsCompatibilityFix(workspacePath)
self.saveWorkspaceAction(workspacePath)
self.workspace.proxy = workspace
self.applyWsCompatibilityFix(workspacePath)
attempted_backup = False
if not self.workspace.proxy.closedNormally:
if self.restoreBackupMessage(workspacePath, updateWorkspace=updateWorkspace):
attempted_backup = True
if self.loadWorkspaceAction(workspacePath, backup=True): # attempt to load backup
self.updateProjectList()
return True
else:
self.messageBackupError("closedAbruptly")
if self.loadWorkspaceAction(workspacePath, backup=False): # attempt to load regular ws file
self.updateProjectList()
return True
# If the regular file won't load for some reason and there was no backup attempt, ask to load the backup file
elif not attempted_backup and self.restoreBackupMessage(workspacePath, failedToLoad=True):
if self.loadWorkspaceAction(workspacePath, backup=True): # attempt to load the backup file
self.updateProjectList()
return True
else:
self.messageBackupError()
return False
def msgInvalidFolderError(self, path):
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText(
"Invalid folder for a workspace."
"\nEnsure there are no .metadata and .backup folders in \n{}".format(path))
msg.setWindowTitle("Failed to load workspace.")
msg.exec_()
def messageBackupError(self, msgType=None):
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
if msgType == "closedAbruptly":
msg.setText(
"Failed to load {}."
"\nRegular workspace save will be restored.".format(".backup workspace file"))
else:
msg.setText(
"Failed to load {}.".format(".backup workspace file"))
msg.setWindowTitle("Failed to load backup workspace.")
msg.exec_()
def applyWsCompatibilityFix(self, workspacePath):
try:
closedNormally = self.workspace.proxy.closedNormally
except AttributeError:
closedNormally = True
self.workspace.proxy.closedNormally = closedNormally # adds attribute to old ws files
self.workspace.path = workspacePath # changes the path to currently selected dir, in case it was moved
self.workspace.proxy.path = workspacePath
def restoreBackupMessage(self, wsName, failedToLoad=False, updateWorkspace=False):
try:
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setParent(None)
msg.setModal(True)
msg.setWindowTitle("Workspace recovery")
time = strftime('%m/%d/%Y %H:%M:%S', localtime(os.path.getmtime(os.path.join(wsName, ".backup"))))
if failedToLoad:
msg.setText("The workplace {} could not be loaded.\n"
"\nTime the backup was created: {}".format(wsName, time))
elif updateWorkspace:
msg.setText("Choose if you want to reload workspace or to recover from backup.\n"
"\nTime the backup was created: {}".format(wsName, time))
else:
msg.setText("The workplace {} was closed unexpectedly.\n"
"\nTime the backup was created: {}".format(wsName, time))
if not updateWorkspace:
msg.setInformativeText("Would you like to recover from backup?")
else:
msg.setInformativeText("Would you like to recover from backup? Select No if you just want to update the workspace.")
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msg.setDefaultButton(QMessageBox.Yes)
retValue = msg.exec_()
if retValue == QMessageBox.Yes:
return True
else:
return
except:
return False
def loadWorkspaceAction(self, workspacePath, backup=False):
if backup:
path = os.path.join(workspacePath, ".backup")
else:
path = os.path.join(workspacePath, ".metadata")
if os.path.exists(path):
try: # in try block in case there is a corrupted .metadata file on the path
with open(path, 'rb') as file:
workspace = pickle.load(file)
except:
return False
else:
return False
self.workspace = WorkspaceNode()
self.workspace.proxy = workspace
self.applyWsCompatibilityFix(workspacePath)
self.workspace.setIcon(0, QIcon(resource_path("resources/workspace.png")))
self.workspace.setText(0, workspacePath[workspacePath.rindex(os.path.sep) + 1:])
self.workspace.proxy.closedNormally = False
if backup:
success = self.workspace.loadBackupWorkspace(workspacePath)
else:
success = self.workspace.loadWorkspace()
if not success:
return False
self.treeView.setRoot(self.workspace)
projects = self.treeView.getProjects()
if projects:
self.configurationManager.allProjects.clear()
self.configurationManager.allProjects.extend(projects)
self.toolBar.updateComboBox()
#self.treeView.expandAll()
self.terminal.executeCommand("cd {}".format(self.workspace.path))
self.workspaceConfiguration.addWorkspace(self.workspace.proxy.path)
if workspacePath:
self.saveWorkspaceAction(workspacePath)
return True
def addToolBarEventHandlers(self):
self.toolBar.compile.triggered.connect(self.compileAction)
self.toolBar.run.triggered.connect(self.runAction)
self.toolBar.debug.triggered.connect(self.debugAction)
def debugAction(self, projectProxy=None):
currentProject: ProjectNode = self.configurationManager.currentProject
if not currentProject:
self.showNoCurrentProjectMessage("Debug")
return
if not os.path.exists(currentProject.proxy.getProjectPath()):
currentProject.eventManager.invalidProject.emit(currentProject)
return
proxy = None
if projectProxy:
proxy = projectProxy
else:
if currentProject:
proxy = currentProject.proxy
if proxy:
commandString = proxy.getProjectDebugCommand()
self.terminal.console.setFocus()
if self.terminal.executeCommand(proxy.getProjectCompileCommand()):
copmileString = proxy.getProjectCompileCommand()
if ' -g ' not in copmileString:
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Warning)
msg.setText("Please set '-g' option in compiler configuration to be able to debug your project.")
msg.setWindowTitle("Debug warning")
msg.exec_()
return False
self.terminal.executeCommand(commandString)
self.toolBar.projectComboBox.setCurrentText(proxy.path)
def runAction(self, projectProxy=None):
currentProject: ProjectNode = self.configurationManager.currentProject
if not currentProject:
self.showNoCurrentProjectMessage("Run")
return
if not os.path.exists(currentProject.proxy.getProjectPath()):
currentProject.eventManager.invalidProject.emit(currentProject)
return
proxy = None
if projectProxy:
proxy = projectProxy
else:
if currentProject:
proxy = currentProject.proxy
if proxy:
commandString = proxy.getProjectRunCommand()
self.terminal.console.setFocus()
if self.terminal.executeCommand(proxy.getProjectCompileCommand()):
self.terminal.executeCommand(commandString)
self.toolBar.projectComboBox.setCurrentText(proxy.path)
def compileAction(self, projectProxy=None):
currentProject: ProjectNode = self.configurationManager.currentProject
if not currentProject:
self.showNoCurrentProjectMessage("Compile")
return
if not os.path.exists(currentProject.proxy.getProjectPath()):
currentProject.eventManager.invalidProject.emit(currentProject)
return
proxy = None
if projectProxy:
proxy = projectProxy
else:
if currentProject:
proxy = currentProject.proxy
if proxy:
commandString = proxy.getProjectCompileCommand()
self.terminal.console.setFocus()
self.terminal.executeCommand(commandString)
self.toolBar.projectComboBox.setCurrentText(proxy.path)
def showNoCurrentProjectMessage(self, action: str):
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("You have to select a project first.")
msg.setWindowTitle("{} error".format(action.capitalize()))
msg.exec_()
def checkExecutable(self):
if self.editor.filePath:
destination = self.editor.filePath[:-1] + "out"
return os.path.exists(destination)
return None
def loadFileText(self, fileProxy):
key = "{}/{}".format(fileProxy.parent.path, fileProxy.path)
if key in self.editorTabs.projectTabs:
self.editorTabs.setCurrentIndex(self.editorTabs.tabs.index(fileProxy))
return
text = self.openFileAction(fileProxy)
fileProxy.text = text
fileProxy.hasUnsavedChanges = False
if fileProxy.getFilePath()[-1].lower() == "c":
currentText = "C"
else:
currentText = "Assembly"
update = True
if len(self.editorTabs.tabs) == 0:
self.editorTabs.tabs.append(fileProxy)
update = False
self.editorTabs.addNewTab(fileProxy, update)
self.statusBar.comboBox.setCurrentText(currentText)
if currentText == "Assembly":
self.editorTabs.projectTabs[key].widget.editor.sintaksa = AsemblerSintaksa(self.editorTabs.projectTabs[key].widget.editor.document())
elif currentText == "C":
self.editorTabs.projectTabs[key].widget.editor.sintaksa = CSyntax(self.editorTabs.projectTabs[key].widget.editor.document())
def updateEditorTrie(self, proxy: FileProxy):
key = "{}/{}".format(proxy.parent.path, proxy.path)
if key in self.editorTabs.projectTabs:
self.editorTabs.projectTabs[key].widget.editor.updateTrie()
self.editorTabs.removeChangeIdentificator(proxy)
def saveAllFiles(self, projectProxy=None):
couldNotBeSaved = []
for i in range(len(self.editorTabs.tabs)):
fileProxy = self.editorTabs.tabs[i]
try:
if fileProxy and fileProxy.hasUnsavedChanges:
if projectProxy and fileProxy.parent.path != projectProxy.path:
continue
with open(fileProxy.getFilePath(), 'w') as file:
file.write(fileProxy.text)
fileProxy.hasUnsavedChanges = False
self.updateEditorTrie(fileProxy)
except:
couldNotBeSaved.append(fileProxy.path)
self.saveWorkspaceAction()
if couldNotBeSaved:
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("The following file(s) could not be saved: {}".format(
','.join(couldNotBeSaved)))
msg.setWindowTitle("File save error")
msg.exec_()
def saveFileAction(self):
if len(self.editorTabs.tabs):
proxy = self.editorTabs.getCurrentFileProxy()
if proxy and proxy.hasUnsavedChanges:
try:
with open(proxy.getFilePath(), 'w') as file:
file.write(proxy.text)
proxy.hasUnsavedChanges = False
self.updateEditorTrie(proxy)
except:
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("The following file could not be saved: {}".format(proxy.path))
msg.setWindowTitle("File save error")
msg.exec_()
self.saveWorkspaceAction()
return True
def openFileAction(self, fileName: FileProxy):
text = None
# if fileName.text:
# return fileName.text
with open(fileName.getFilePath(), 'r') as file:
text = file.read()
return text
def keyPressEvent(self, event):
if event.modifiers() == Qt.ControlModifier:
if event.key() == Qt.Key_Tab:
self.showTabSwitcher()
elif event.key() == Qt.Key_E:
self.showProjectSwitcher()
super(AsemblerIDE, self).keyPressEvent(event)
def showProjectSwitcher(self):
if self.projectSwitcher.isHidden() and len(self.configurationManager.allProjects):
self.projectSwitcher.showSwitcher()
self.projectSwitcher.setFocus()
def showTabSwitcher(self):
if self.tabSwitcher.isHidden() and len(self.editorTabs.tabs):
self.tabSwitcher.showSwitcher()
self.tabSwitcher.setFocus()
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
# base_path = PathManager.START_DIRECTORY
except Exception:
# base_path = os.path.abspath(".")
base_path = PathManager.START_DIRECTORY
return os.path.join(base_path, relative_path)
if __name__ == '__main__':
app = QApplication(sys.argv)
if platform.system() != "Linux":
msg = QMessageBox()
msg.setStyleSheet("background-color: #2D2D30; color: white;")
msg.setModal(True)
msg.setIcon(QMessageBox.Critical)
msg.setText("You are using {} and the only currently supported operating system is Linux.".format(platform.system()))
msg.setWindowTitle("Unsupported operating system")
msg.exec_()
sys.exit(1)
else:
ide = AsemblerIDE()
app.exec_()