forked from MARIOBASZ/bCNC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
226 lines (198 loc) · 6.61 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
#!/usr/bin/env python3
import os
import sys
import getopt
PRGPATH = os.path.abspath(os.path.dirname(__file__))
sys.path.append(PRGPATH)
sys.path.append(os.path.join(PRGPATH, "lib"))
sys.path.append(os.path.join(PRGPATH, "plugins"))
sys.path.append(os.path.join(PRGPATH, "controllers"))
# -----------------------------------------------------------------------------
def usage(rc):
import Utils
wrt = sys.stdout.write
wrt(
f"{Utils.__prg__} V{Utils.__version__} [{Utils.__date__}] "
+ f"{Utils.__platform_fingerprint__}\n"
)
wrt(f"{Utils.__author__} <{Utils.__email__}>\n\n")
wrt("Usage: [options] [filename...]\n\n")
wrt("Options:\n")
wrt("\t-b # | --baud #\t\tSet the baud rate\n")
wrt("\t-d\t\t\tEnable developer features\n")
wrt("\t-D\t\t\tDisable developer features\n")
wrt("\t-f | --fullscreen\tEnable fullscreen mode\n")
wrt("\t-g #\t\t\tSet the default geometry\n")
wrt("\t-h | -? | --help\tThis help page\n")
wrt("\t-i # | --ini #\t\tAlternative ini file for testing\n")
wrt("\t-l | --list\t\tList all recently opened files\n")
wrt("\t-p # | --pendant #\tOpen pendant to specified port\n")
wrt("\t-P\t\t\tDo not start pendant\n")
wrt("\t-r | --recent\t\tLoad the most recent file opened\n")
wrt("\t-R #\t\t\tLoad the recent file matching the argument\n")
wrt("\t-s # | --serial #\tOpen serial port specified\n")
wrt("\t-S\t\t\tDo not open serial port\n")
wrt("\t--run\t\t\tDirectly run the file once loaded\n")
wrt("\n")
sys.exit(rc)
# -----------------------------------------------------------------------------
def main():
import Helpers
import bmain
import tkExtra
import Utils
import Updates
from CNC import CNC
try:
import serial
except ImportError:
serial = None
print("testing mode, could not import serial")
# Parse arguments
try:
optlist, args = getopt.getopt(
sys.argv[1:],
"?b:dDfhi:g:rlpPSs:",
[
"help",
"ini=",
"fullscreen",
"recent",
"list",
"pendant=",
"serial=",
"baud=",
"run",
],
)
except getopt.GetoptError:
usage(1)
recent = None
run = False
fullscreen = False
for opt, val in optlist:
if opt in ("-h", "-?", "--help"):
usage(0)
elif opt in ("-i", "--ini"):
Utils.iniUser = val
Utils.loadConfiguration()
elif opt == "-d":
CNC.developer = True
elif opt == "-D":
CNC.developer = False
elif opt in ("-r", "-R", "--recent", "-l", "--list"):
if opt in ("-r", "--recent"):
r = 0
elif opt in ("--list", "-l"):
r = -1
else:
try:
r = int(val) - 1
except Exception:
# Scan in names
for r in range(Utils._maxRecent):
filename = Utils.getRecent(r)
if filename is None:
break
fn, ext = os.path.splitext(os.path.basename(filename))
if fn == val:
break
else:
r = 0
if r < 0:
# display list of recent files
maxlen = 10
for i in range(Utils._maxRecent):
try:
filename = Utils.getRecent(i)
except Exception:
continue
maxlen = max(maxlen, len(os.path.basename(filename)))
sys.stdout.write("Recent files:\n")
for i in range(Utils._maxRecent):
filename = Utils.getRecent(i)
if filename is None:
break
d = os.path.dirname(filename)
fn = os.path.basename(filename)
sys.stdout.write(f" {i + 1:2d}: {fn:<{maxlen}} {d}\n")
try:
sys.stdout.write("Select one: ")
r = int(sys.stdin.readline()) - 1
except Exception:
pass
try:
recent = Utils.getRecent(r)
except Exception:
pass
elif opt in ("-f", "--fullscreen"):
fullscreen = True
elif opt == "-p":
pass # startPendant()
elif opt == "-P":
pass # stopPendant()
elif opt == "--pendant":
pass # startPendant on port
elif opt == "--run":
run = True
application = bmain.Application(className=f" {Utils.__prg__} ")
palette = {"background": application.cget("background")}
color_count = 0
custom_color_count = 0
for color_name in (
"background",
"foreground",
"activeBackground",
"activeForeground",
"disabledForeground",
"highlightBackground",
"highlightColor",
"selectBackground",
"selectForeground",
):
color2 = Utils.getStr("Color", "global." + color_name.lower(), None)
color_count += 1
if (color2 is not None) and (color2.strip() != ""):
palette[color_name] = color2.strip()
custom_color_count += 1
if color_count == 0:
tkExtra.GLOBAL_CONTROL_BACKGROUND = color2
elif color_count == 1:
tkExtra.GLOBAL_FONT_COLOR = color2
if custom_color_count > 0:
print("Changing palette")
application.tk_setPalette(**palette)
if fullscreen:
application.attributes("-fullscreen", True)
# Parse remaining arguments except files
if recent:
args.append(recent)
for fn in args:
application.load(fn)
if serial is None:
application.showSerialError()
if Updates.need2Check():
application.checkUpdates()
if run:
application.run()
try:
application.mainloop()
except KeyboardInterrupt:
application.quit()
application.close()
Utils.saveConfiguration()
if __name__ == "__main__":
sys.stdout.write("=" * 80 + "\n")
sys.stdout.write(
"WARNING: bCNC was recently ported to only support \n"
+ "python3.8 and newer.\n"
)
sys.stdout.write(
"Most things seem to work reasonably well.\n"
)
sys.stdout.write(
"Please report any issues to: "
+ "https://github.com/vlachoudis/bCNC/issues\n"
)
sys.stdout.write("=" * 80 + "\n")
main()