-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·124 lines (98 loc) · 3.51 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
#!/usr/bin/env python2
import sys
import simulator
from PyQt4 import QtCore,QtGui, QtOpenGL
from channels import biChannel
from qSimulator import Ui_MainWindow
from simulator import runner, instanciator
from PyQt4.Qt import QApplication
class qInstanciator(instanciator):
def __init__(self, scene):
instanciator.__init__(self)
self.scene = scene
def make(self,file):
self.components = {}
self.ncomponents = {}
glob = globals()
glob['scene'] = self.scene
glob['self'] = self
execfile(file, glob, locals())
# Create a class for our main window
class qRunner(runner, QtGui.QMainWindow):
def __init__(self, file = None):
QtGui.QMainWindow.__init__(self)
# This is always the same
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Since the UI is a QGraphicsView, I create a Scene
# so it has something to show
self.scene = QtGui.QGraphicsScene()
self.ui.view.setScene(self.scene)
# This makes the view OpenGL-accelerated. Usually makes
# things much faster, but it *is* optional.
self.ui.view.setViewport(QtOpenGL.QGLWidget())
self.populate(file)
runner.__init__(self, self.components)
self.scene.setSceneRect(self.scene.itemsBoundingRect())
self.setWindowState(QtCore.Qt.WindowMaximized)
# Create a timer
self.animator=QtCore.QTimer()
# And when it triggers, it calls the animate method
self.animator.timeout.connect(self.clock)
# And I animate it once manually.
self.animator.start(1000)
def clock(self):
runner.clock(self)
if len(self.unfinished_procs) == 0:
print "Finished in %d cycles." % simulator.cycle
QApplication.quit()
for c in self.components:
c.update_color()
def populate(self, file):
inst = qInstanciator(self.scene)
inst.make(file)
self.components = inst.get_components()
def qMain(args):
# Again, this is boilerplate, it's going to be the same on
# almost every app you write
app = QtGui.QApplication(sys.argv)
window = qRunner(args.file)
window.show()
# It's exec_ because exec is a reserved word in Python
sys.exit(app.exec_())
def main(args):
inst = qInstanciator(None)
inst.make(args.file)
run = runner(inst.get_components())
if args.ncycles is None:
run.run()
else:
run.run(int(args.ncycles))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="""Architecture simulator.
Uses a description of the architecture in Python.""")
subparsers = parser.add_subparsers()
cmdline = subparsers.add_parser('cmdline',
help = "Runs the program without a graphical interface.")
cmdline.add_argument(
"-n",
action = "store",
dest = "ncycles",
help = "Number of cycles to run (default : infinity)",
default = None)
cmdline.add_argument(
"file",
action = "store",
help = "Architecture description.")
graphical = subparsers.add_parser('graphical',
help = "Displays the network in a graphical interface (requires PyQt)")
graphical.add_argument(
"file",
action = "store",
help = "Architecture description.")
cmdline.set_defaults(func=main)
graphical.set_defaults(func=qMain)
args = parser.parse_args()
args.func(args)