-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogsim.py
executable file
·175 lines (142 loc) · 5.16 KB
/
logsim.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
#!/usr/bin/env python3
"""Parse command line options and arguments for the Logic Simulator.
This script parses options and arguments specified on the command line, and
runs either the command line user interface or the graphical user interface.
Usage
-----
Show help: logsim.py -h
Command line user interface: logsim.py -c <file path>
Graphical user interface: logsim.py <file path>
"""
import getopt
import os
import sys
import builtins
import wx
from wx.lib.mixins.inspection import InspectionMixin
from names import Names
from devices import Devices
from network import Network
from monitors import Monitors
from scanner import Scanner
from parse import Parser
from userint import UserInterface
from gui import Gui
# language domain
langDomain = "LOGIC SIMULATOR"
supportedLangs = {
u"en": wx.LANGUAGE_ENGLISH,
u"es": wx.LANGUAGE_SPANISH
}
def _hook(obj):
if obj is not None:
print(repr(obj))
builtins.__dict__['_'] = wx.GetTranslation
class App(wx.App, InspectionMixin):
"""Set up App class for GUI.
Necessary to update GUI language.
"""
def OnInit(self):
"""Set display hook and update language."""
self.Init() # InspectionMixin
sys.displayhook = _hook
self.appName = "Logic Simulator"
self.doConfig()
self.locale = None
wx.Locale.AddCatalogLookupPathPrefix('locale')
self.updateLanguage(self.appConfig.Read(u"Language"))
return True
def doConfig(self):
"""Set up an application configuration file."""
# configuration folder
sp = wx.StandardPaths.Get()
self.configLoc = sp.GetUserConfigDir()
self.configLoc = os.path.join(self.configLoc, self.appName)
# win: C:\Users\userid\AppData\Roaming\appName
# nix: \home\userid\appName
if not os.path.exists(self.configLoc):
os.mkdir(self.configLoc)
# AppConfig stuff is here
self.appConfig = wx.FileConfig(appName=self.appName,
vendorName=u'who you wish',
localFilename=os.path.join(
self.configLoc, "AppConfig"))
if not self.appConfig.HasEntry(u'Language'):
# on first run we default to English
self.appConfig.Write(key=u'Language', value=u'en')
self.appConfig.Flush()
def updateLanguage(self, lang):
"""
Update the language to the requested one.
Make *sure* any existing locale is deleted before the new
one is created.
"""
# if an unsupported language is requested default to English
if lang in supportedLangs:
setLang = supportedLangs[lang]
else:
setLang = wx.LANGUAGE_ENGLISH
if self.locale:
assert sys.getrefcount(self.locale) <= 2
del self.locale
# create a locale object for this language
self.locale = wx.Locale(setLang)
if self.locale.IsOk():
self.locale.AddCatalog(langDomain)
else:
self.locale = None
def main(arg_list):
"""Parse the command line options and arguments specified in arg_list.
Run either the command line user interface, the graphical user interface,
or display the usage message.
"""
usage_message = ("Usage:\n"
"Show help: logsim.py -h\n"
"Command line user interface: logsim.py -c <file path>\n"
"Graphical user interface: logsim.py <file path>")
try:
options, arguments = getopt.getopt(arg_list, "hc:")
except getopt.GetoptError:
print("Error: invalid command line arguments\n")
print(usage_message)
sys.exit()
# Initialise instances of the four inner simulator classes
names = Names()
devices = Devices(names)
network = Network(names, devices)
monitors = Monitors(names, devices, network)
for option, path in options:
if option == "-h": # print the usage message
print(usage_message)
sys.exit()
elif option == "-c": # use the command line user interface
scanner = Scanner(path, names)
parser = Parser(names, devices, network, monitors, scanner)
if parser.parse_network():
# Initialise an instance of the userint.UserInterface() class
userint = UserInterface(names, devices, network, monitors)
userint.command_interface()
if not options: # no option given, use the graphical user interface
if len(arguments) != 1: # wrong number of arguments
path = None
else:
[path] = arguments
# Initialise an instance of the gui.Gui() class
app = App()
builtins._ = wx.GetTranslation
locale = wx.Locale()
locale.Init(wx.LANGUAGE_DEFAULT)
locale.AddCatalogLookupPathPrefix('locales')
locale.AddCatalog('base')
gui = Gui(
"Logic Simulator",
path,
names,
devices,
network,
monitors
)
gui.Show(True)
app.MainLoop()
if __name__ == "__main__":
main(sys.argv[1:])