-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (57 loc) · 2.31 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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QVBoxLayout, QWidget, QFileDialog, \
QAction
from airspeed import Template
class VelocityTemplatePreviewer(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
self.templateEditor = QTextEdit()
self.outputViewer = QTextEdit()
self.renderButton = QPushButton("Render")
layout.addWidget(self.templateEditor)
layout.addWidget(self.renderButton)
layout.addWidget(self.outputViewer)
# Create menu bar and actions
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('File')
openAction = QAction('Open', self)
openAction.triggered.connect(self.openFile)
fileMenu.addAction(openAction)
saveAction = QAction('Save', self)
saveAction.triggered.connect(self.saveFile)
fileMenu.addAction(saveAction)
centralWidget = QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
self.renderButton.clicked.connect(self.renderTemplate)
def openFile(self):
# Function to open and read a file
fileName, _ = QFileDialog.getOpenFileName(self, "Open Template File", "", "Velocity Templates (*.vm)")
if fileName:
with open(fileName, 'r') as file:
self.templateEditor.setText(file.read())
def saveFile(self):
# Function to save the current content to a file
fileName, _ = QFileDialog.getSaveFileName(self, "Save Template File", "", "Velocity Templates (*.vm)")
if fileName:
with open(fileName, 'w') as file:
file.write(self.templateEditor.toPlainText())
def renderTemplate(self):
template_str = self.templateEditor.toPlainText()
try:
# Directly use Template class to parse the string
template = Template(template_str)
rendered = template.merge({})
self.outputViewer.setText(rendered)
except Exception as e:
self.outputViewer.setText(f"Error: {e}")
def main():
app = QApplication(sys.argv)
mainWin = VelocityTemplatePreviewer()
mainWin.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()