forked from vlachoudis/bCNC
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utils.py
735 lines (640 loc) · 21.1 KB
/
Utils.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
# -*- coding: ascii -*-
# $Id$
#
# Author: [email protected]
# Date: 16-Apr-2015
__author__ = "Vasilis Vlachoudis"
__email__ = "[email protected]"
import os
import glob
import traceback
from log import say
try:
from Tkinter import *
import tkFont
import tkMessageBox
import ConfigParser
except ImportError:
from tkinter import *
import tkinter.font as tkFont
import tkinter.messagebox as tkMessageBox
import configparser as ConfigParser
import gettext
try:
import __builtin__
except:
import builtins as __builtin__
#__builtin__.unicode = str # dirty hack for python3
__prg__ = "bCNC"
prgpath = os.path.abspath(os.path.dirname(sys.argv[0]))
iniSystem = os.path.join(prgpath,"%s.ini"%(__prg__))
iniUser = os.path.expanduser("~/.%s" % (__prg__))
hisFile = os.path.expanduser("~/.%s.history" % (__prg__))
# dirty way of substituting the "_" on the builtin namespace
#__builtin__.__dict__["_"] = gettext.translation('bCNC', 'locale', fallback=True).ugettext
__builtin__._ = gettext.translation('bCNC', os.path.join(prgpath,'locale'), fallback=True).gettext
__builtin__.N_ = lambda message: message
import Ribbon
import tkExtra
__www__ = "https://github.com/vlachoudis/bCNC"
__contribute__ = \
"@effer Filippo Rivato\n" \
"@carlosgs Carlos Garcia Saura"
__credits__ = \
"@1bigpig\n" \
"@chamnit Sonny Jeon\n" \
"@onekk Carlo\n" \
"@willadams William Adams"
__translations__ = \
"French - @ThierryM\n" \
"German - @feistus\n" \
"Italian - @onekk\n" \
"Japanese - @stm32f1\n" \
"traditional chinese - @Engineer2Designer\n" \
"Portuguese - @moacirbmn \n" \
"Russian - @minithc\n" \
"Spanish - @carlosgs\n"
LANGUAGES = {
"" : "<system>",
"de" : "Deutsch",
"en" : "English",
"es" : u"Espa\u00f1ol",
"fr" : u"Fran\u00e7ais",
"it" : "Italiano",
"ja" : "Japanese",
"zh_tw" : "traditional chinese",
"pt_BR" : "Brazilian - Portuguese",
"ru" : "Russian",
}
icons = {}
config = ConfigParser.ConfigParser()
language = ""
_errorReport = True
errors = []
_maxRecent = 10
_FONT_SECTION = "Font"
GRBL0 = 0
GRBL1 = 1
SMOOTHIE = 10
CONTROLLER = { "Grbl-V0" : GRBL0,
"Grbl" : GRBL1,
"Smoothie" : SMOOTHIE}
#------------------------------------------------------------------------------
def loadIcons():
global icons
icons = {}
for img in glob.glob("%s%sicons%s*.gif"%(prgpath,os.sep,os.sep)):
name,ext = os.path.splitext(os.path.basename(img))
try:
icons[name] = PhotoImage(file=img)
if getBool("CNC", "doublesizeicon"):
icons[name] = icons[name].zoom(2,2)
except TclError:
pass
#------------------------------------------------------------------------------
def delIcons():
global icons
if len(icons) > 0:
for i in icons.values():
del i
icons = {} # needed otherwise it complains on deleting the icons
#------------------------------------------------------------------------------
# Load configuration
#------------------------------------------------------------------------------
def loadConfiguration(systemOnly=False):
global config, _errorReport, language
if systemOnly:
config.read(iniSystem)
else:
config.read([iniSystem, iniUser])
_errorReport = getInt("Connection","errorreport",1)
language = getStr(__prg__, "language")
if language:
# replace language
__builtin__._ = gettext.translation('bCNC', os.path.join(prgpath,'locale'),
fallback=True, languages=[language]).gettext
#------------------------------------------------------------------------------
# Save configuration file
#------------------------------------------------------------------------------
def saveConfiguration():
global config
cleanConfiguration()
f = open(iniUser,"w")
config.write(f)
f.close()
delIcons()
#----------------------------------------------------------------------
# Remove items that are the same as in the default ini
#----------------------------------------------------------------------
def cleanConfiguration():
global config
newconfig = config # Remember config
config = ConfigParser.ConfigParser()
loadConfiguration(True)
# Compare items
for section in config.sections():
for item, value in config.items(section):
try:
new = newconfig.get(section, item)
if value==new:
newconfig.remove_option(section, item)
except ConfigParser.NoOptionError:
pass
config = newconfig
#------------------------------------------------------------------------------
# add section if it doesn't exist
#------------------------------------------------------------------------------
def addSection(section):
global config
if not config.has_section(section):
config.add_section(section)
#------------------------------------------------------------------------------
def getStr(section, name, default=""):
global config
try:
return config.get(section, name)
except:
return default
#------------------------------------------------------------------------------
def getUtf(section, name, default=""):
global config
try:
return config.get(section, name).decode("utf8")
except:
return default
#------------------------------------------------------------------------------
def getInt(section, name, default=0):
global config
try: return int(config.get(section, name))
except: return default
#------------------------------------------------------------------------------
def getFloat(section, name, default=0.0):
global config
try: return float(config.get(section, name))
except: return default
#------------------------------------------------------------------------------
def getBool(section, name, default=False):
global config
try: return bool(int(config.get(section, name)))
except: return default
#-------------------------------------------------------------------------------
# Return a font from a string
#-------------------------------------------------------------------------------
def makeFont(name, value=None):
try:
font = tkFont.Font(name=name, exists=True)
except TclError:
font = tkFont.Font(name=name)
font.delete_font = False
except AttributeError:
return None
if value is None: return font
if isinstance(value, tuple):
font.configure(family=value[0])
try: font.configure(size=value[1])
except: pass
try: font.configure(weight=value[2])
except: pass
try: font.configure(slant=value[3])
except: pass
return font
#-------------------------------------------------------------------------------
# Create a font string
#-------------------------------------------------------------------------------
def fontString(font):
name = str(font[0])
size = str(font[1])
if name.find(' ')>=0:
s = '"%s" %s'%(name,size)
else:
s = '%s %s'%(name,size)
try:
if font[2] == tkFont.BOLD: s += " bold"
except: pass
try:
if font[3] == tkFont.ITALIC: s += " italic"
except: pass
return s
#-------------------------------------------------------------------------------
# Get font from configuration
#-------------------------------------------------------------------------------
def getFont(name, default=None):
try:
value = config.get(_FONT_SECTION, name)
except:
value = None
if not value:
font = makeFont(name, default)
setFont(name, font)
return font
if isinstance(value, str):
value = tuple(value.split(','))
if isinstance(value, tuple):
font = makeFont(name, value)
if font is not None: return font
return value
#-------------------------------------------------------------------------------
# Set font in configuration
#-------------------------------------------------------------------------------
def setFont(name, font):
if font is None: return
if isinstance(font,str):
config.set(_FONT_SECTION, name, font)
elif isinstance(font,tuple):
config.set(_FONT_SECTION, name, ",".join(map(str,font)))
else:
config.set(_FONT_SECTION, name, "%s,%s,%s" % \
(font.cget("family"),font.cget("size"),font.cget("weight")))
#------------------------------------------------------------------------------
def setBool(section, name, value):
global config
config.set(section, name, str(int(value)))
#------------------------------------------------------------------------------
def setStr(section, name, value):
global config
config.set(section, name, str(value))
#------------------------------------------------------------------------------
def setUtf(section, name, value):
global config
try:
s = str(value.encode("utf8"))
except:
s = str(value)
config.set(section, name, s)
setInt = setStr
setFloat = setStr
#-------------------------------------------------------------------------------
def controllerName(idx):
for n,i in CONTROLLER.items():
if i==idx:
return n
return "unknown"
#-------------------------------------------------------------------------------
# Add Recent
#-------------------------------------------------------------------------------
def addRecent(filename):
try:
sfn = str(os.path.abspath(filename))
except UnicodeEncodeError:
sfn = filename.encode("utf8")
last = _maxRecent-1
for i in range(_maxRecent):
rfn = getRecent(i)
if rfn is None:
last = i-1
break
if rfn == sfn:
if i==0: return
last = i-1
break
# Shift everything by one
for i in range(last, -1, -1):
config.set("File", "recent.%d"%(i+1), getRecent(i))
config.set("File", "recent.0", sfn)
#-------------------------------------------------------------------------------
def getRecent(recent):
try:
return config.get("File","recent.%d"%(recent))
except ConfigParser.NoOptionError:
return None
#------------------------------------------------------------------------------
# Return all comports when serial.tools.list_ports is not available!
#------------------------------------------------------------------------------
def comports():
locations=[ '/dev/ttyACM',
'/dev/ttyUSB',
'/dev/ttyS',
'com']
comports = []
for prefix in locations:
for i in range(32):
device = "%s%d"%(prefix,i)
try:
os.stat(device)
comports.append((device,None,None))
except OSError:
pass
return comports
#===============================================================================
def addException():
global errors
#self.widget._report_exception()
try:
typ, val, tb = sys.exc_info()
traceback.print_exception(typ, val, tb)
if errors: errors.append("")
exception = traceback.format_exception(typ, val, tb)
errors.extend(exception)
if len(errors) > 100:
# If too many errors are found send the error report
ReportDialog(self.widget)
except:
say(str(sys.exc_info()))
#===============================================================================
class CallWrapper:
"""Replaces the Tkinter.CallWrapper with extra functionality"""
def __init__(self, func, subst, widget):
"""Store FUNC, SUBST and WIDGET as members."""
self.func = func
self.subst = subst
self.widget = widget
# ----------------------------------------------------------------------
def __call__(self, *args):
"""Apply first function SUBST to arguments, than FUNC."""
try:
if self.subst:
args = self.subst(*args)
return self.func(*args)
# One possible fix is to make an external file for the wrapper
# and import depending the version
#except SystemExit, msg: # python2.4 syntax
#except SystemExit as msg: # python3 syntax
# raise SystemExit(msg)
except SystemExit: # both
raise SystemExit(sys.exc_info()[1])
except KeyboardInterrupt:
pass
except:
addException()
#===============================================================================
# Error message reporting dialog
#===============================================================================
class ReportDialog(Toplevel):
_shown = False # avoid re-entry when multiple errors are displayed
def __init__(self, master):
if ReportDialog._shown: return
ReportDialog._shown = True
Toplevel.__init__(self, master)
if master is not None: self.transient(master)
self.title(_("Error Reporting"))
# Label Frame
frame = LabelFrame(self, text=_("Report"))
frame.pack(side=TOP, expand=YES, fill=BOTH)
l = Label(frame, text=_("The following report is about to be send "\
"to the author of %s")%(__name__), justify=LEFT, anchor=W)
l.pack(side=TOP)
self.text = Text(frame, background="White")
self.text.pack(side=LEFT, expand=YES, fill=BOTH)
sb = Scrollbar(frame, orient=VERTICAL, command=self.text.yview)
sb.pack(side=RIGHT, fill=Y)
self.text.config(yscrollcommand=sb.set)
# email frame
frame = Frame(self)
frame.pack(side=TOP, fill=X)
l = Label(frame, text=_("Your email"))
l.pack(side=LEFT)
self.email = Entry(frame, background="White")
self.email.pack(side=LEFT, expand=YES, fill=X)
# Automatic error reporting
self.err = BooleanVar()
self.err.set(_errorReport)
b = Checkbutton(frame, text=_("Automatic error reporting"),
variable=self.err, anchor=E, justify=RIGHT)
b.pack(side=RIGHT)
# Buttons
frame = Frame(self)
frame.pack(side=BOTTOM, fill=X)
b = Button(frame, text=_("Close"),
compound=LEFT,
command=self.cancel)
b.pack(side=RIGHT)
b = Button(frame, text=_("Send report"),
compound=LEFT,
command=self.send)
b.pack(side=RIGHT)
from bCNC import __version__, __date__
# Fill report
txt = [ "Program : %s"%(__prg__),
"Version : %s"%(__version__),
"Last Change : %s"%(__date__),
"Platform : %s"%(sys.platform),
"Python : %s"%(sys.version),
"TkVersion : %s"%(TkVersion),
"TclVersion : %s"%(TclVersion),
"\nTraceback:" ]
for e in errors:
if e!="" and e[-1] == "\n":
txt.append(e[:-1])
else:
txt.append(e)
self.text.insert('0.0', "\n".join(txt))
# Guess email
user = os.getenv("USER")
host = os.getenv("HOSTNAME")
if user and host:
email = "%s@%s"%(user,host)
else:
email = ""
self.email.insert(0,email)
self.protocol("WM_DELETE_WINDOW", self.close)
self.bind('<Escape>', self.close)
# Wait action
self.wait_visibility()
self.grab_set()
self.focus_set()
self.wait_window()
# ----------------------------------------------------------------------
def close(self, event=None):
ReportDialog._shown = False
self.destroy()
# ----------------------------------------------------------------------
def send(self):
import httplib, urllib
global errors
email = self.email.get()
desc = self.text.get('1.0', END).strip()
# Send information
self.config(cursor="watch")
self.text.config(cursor="watch")
self.update_idletasks()
params = urllib.urlencode({"email":email, "desc":desc})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection("www.fluka.org:80")
try:
conn.request("POST", "/flair/send_email_bcnc.php", params, headers)
response = conn.getresponse()
except:
tkMessageBox.showwarning(_("Error sending report"),
_("There was a problem connecting to the web site"),
parent=self)
else:
if response.status == 200:
tkMessageBox.showinfo(_("Report successfully send"),
_("Report was successfully uploaded to web site"),
parent=self)
del errors[:]
else:
tkMessageBox.showwarning(_("Error sending report"),
_("There was an error sending the report\nCode=%d %s")%\
(response.status, response.reason),
parent=self)
conn.close()
self.config(cursor="")
self.cancel()
# ----------------------------------------------------------------------
def cancel(self):
global _errorReport, errors
_errorReport = self.err.get()
config.set("Connection", "errorreport", str(bool(self.err.get())))
del errors[:]
self.close()
# ----------------------------------------------------------------------
@staticmethod
def sendErrorReport():
ReportDialog(None)
#===============================================================================
# User Button
#===============================================================================
class UserButton(Ribbon.LabelButton):
TOOLTIP = "User configurable button.\n<RightClick> to configure"
def __init__(self, master, cnc, button, *args, **kwargs):
if button == 0:
Button.__init__(self, master, *args, **kwargs)
else:
Ribbon.LabelButton.__init__(self, master, *args, **kwargs)
self["width"] = 60
self.cnc = cnc
self.button = button
self.get()
#self.bind("<Control-Button-1>", self.edit)
self.bind("<Button-3>", self.edit)
self["command"] = self.execute
# ----------------------------------------------------------------------
# get information from configuration
# ----------------------------------------------------------------------
def get(self):
if self.button == 0: return
name = self.name()
self["text"] = name
#if icon == "":
# icon = icons.get("empty","")
self["image"] = icons.get(self.icon(),icons["material"])
self["compound"] = LEFT
tooltip = self.tooltip()
if not tooltip: tooltip = UserButton.TOOLTIP
tkExtra.Balloon.set(self, tooltip)
# ----------------------------------------------------------------------
def name(self):
try:
return config.get("Buttons","name.%d"%(self.button))
except:
return str(self.button)
# ----------------------------------------------------------------------
def icon(self):
try:
return config.get("Buttons","icon.%d"%(self.button))
except:
return None
# ----------------------------------------------------------------------
def tooltip(self):
try:
return config.get("Buttons","tooltip.%d"%(self.button))
except:
return ""
# ----------------------------------------------------------------------
def command(self):
try:
return config.get("Buttons","command.%d"%(self.button))
except:
return ""
# ----------------------------------------------------------------------
# Edit button
# ----------------------------------------------------------------------
def edit(self, event=None):
UserButtonDialog(self, self)
self.get()
# ----------------------------------------------------------------------
# Execute command
# ----------------------------------------------------------------------
def execute(self):
cmd = self.command()
if not cmd:
self.edit()
return
for line in cmd.splitlines():
self.cnc.pendant.put(line)
#===============================================================================
# User Configurable Buttons
#===============================================================================
class UserButtonDialog(Toplevel):
NONE = "<none>"
def __init__(self, master, button):
Toplevel.__init__(self, master)
self.title(_("User configurable button"))
self.transient(master)
self.button = button
# Name
row,col = 0,0
Label(self, text=_("Name:")).grid(row=row, column=col, sticky=E)
col += 1
self.name = Entry(self, background="White")
self.name.grid(row=row, column=col, columnspan=2, sticky=EW)
tkExtra.Balloon.set(self.name, _("Name to appear on button"))
# Icon
row,col = row+1,0
Label(self, text=_("Icon:")).grid(row=row, column=col, sticky=E)
col += 1
self.icon = Label(self, relief=RAISED)
self.icon.grid(row=row, column=col, sticky=EW)
col += 1
self.iconCombo = tkExtra.Combobox(self, True,
width=5,
command=self.iconChange)
lst = list(sorted(icons.keys()))
lst.insert(0,UserButtonDialog.NONE)
self.iconCombo.fill(lst)
self.iconCombo.grid(row=row, column=col, sticky=EW)
tkExtra.Balloon.set(self.iconCombo, _("Icon to appear on button"))
# Tooltip
row,col = row+1,0
Label(self, text=_("Tool Tip:")).grid(row=row, column=col, sticky=E)
col += 1
self.tooltip = Entry(self, background="White")
self.tooltip.grid(row=row, column=col, columnspan=2, sticky=EW)
tkExtra.Balloon.set(self.tooltip, _("Tooltip for button"))
# Tooltip
row,col = row+1,0
Label(self, text=_("Command:")).grid(row=row, column=col, sticky=N+E)
col += 1
self.command = Text(self, background="White", width=40, height=10)
self.command.grid(row=row, column=col, columnspan=2, sticky=EW)
self.grid_columnconfigure(2,weight=1)
self.grid_rowconfigure(row,weight=1)
# Actions
row += 1
f = Frame(self)
f.grid(row=row, column=0, columnspan=3, sticky=EW)
Button(f, text=_("Cancel"), command=self.cancel).pack(side=RIGHT)
Button(f, text=_("Ok"), command=self.ok).pack(side=RIGHT)
# Set variables
self.name.insert(0,self.button.name())
self.tooltip.insert(0,self.button.tooltip())
icon = self.button.icon()
if icon is None:
self.iconCombo.set(UserButtonDialog.NONE)
else:
self.iconCombo.set(icon)
self.icon["image"] = icons.get(icon,"")
self.command.insert("1.0", self.button.command())
# Wait action
self.wait_visibility()
self.grab_set()
self.focus_set()
self.wait_window()
# ----------------------------------------------------------------------
def ok(self, event=None):
n = self.button.button
config.set("Buttons", "name.%d"%(n), self.name.get().strip())
icon = self.iconCombo.get()
if icon == UserButtonDialog.NONE: icon = ""
config.set("Buttons", "icon.%d"%(n), icon)
config.set("Buttons", "tooltip.%d"%(n), self.tooltip.get().strip())
config.set("Buttons", "command.%d"%(n), self.command.get("1.0",END).strip())
self.destroy()
# ----------------------------------------------------------------------
def cancel(self):
self.destroy()
# ----------------------------------------------------------------------
def iconChange(self):
self.icon["image"] = icons.get(self.iconCombo.get(),"")