printrun-src/printcore.py

Sat, 04 Jun 2016 09:22:51 +0200

author
mbayer
date
Sat, 04 Jun 2016 09:22:51 +0200
changeset 20
03b34402d405
parent 15
0bbb006204fc
child 45
c82943fb205f
permissions
-rw-r--r--

Code cleanup

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import getopt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import getopt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 from printrun.printcore import printcore
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 from printrun.utils import setup_logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 from printrun import gcoder
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 if __name__ == '__main__':
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 setup_logging(sys.stderr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 baud = 115200
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 loud = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 statusreport = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 from printrun.printcore import __version__ as printcore_version
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 usage = "Usage:\n"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 " printcore [OPTIONS] PORT FILE\n\n"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 "Options:\n"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 " -b, --baud=BAUD_RATE"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 "\t\tSet baud rate value. Default value is 115200\n"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 " -s, --statusreport\t\tPrint progress as percentage\n"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 " -v, --verbose\t\t\tPrint additional progress information\n"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 " -V, --version\t\t\tPrint program's version number and exit\n"+\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 " -h, --help\t\t\tPrint this help message and exit\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 opts, args = getopt.getopt(sys.argv[1:], "b:svVh",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 ["baud=", "statusreport", "verbose", "version", "help"])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 except getopt.GetoptError, err:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 print str(err)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 print usage
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 sys.exit(2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 for o, a in opts:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 if o in ('-h', '--help'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 print usage
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 sys.exit(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 elif o in ('-V','--version'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 print "printrun "+printcore_version
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 sys.exit(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 elif o in ('-b','--baud'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 baud = int(a)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 except ValueError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 print "ValueError:"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 print "\tInvalid BAUD_RATE value '%s'" % a
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 print "\tBAUD_RATE must be an integer\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 # FIXME: This should output a more apropiate error message when
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 # not a good baud rate is passed as an argument
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 # i.e: when baud <= 1000 or > 225000
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 print usage
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 sys.exit(2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 elif o in ('-v', '--verbose'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 loud = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 elif o in ('-s', '--statusreport'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 statusreport = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 if len(args) <= 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 print "Error: Port or gcode file were not specified.\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 print usage
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 sys.exit(2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 elif len(args) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 port = args[-2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 filename = args[-1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 print "Printing: %s on %s with baudrate %d" % (filename, port, baud)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 p = printcore(port, baud)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 p.loud = loud
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 time.sleep(2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 gcode = [i.strip() for i in open(filename)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 gcode = gcoder.LightGCode(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 p.startprint(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 if statusreport:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 p.loud = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 sys.stdout.write("Progress: 00.0%\r")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 while p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 time.sleep(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 if statusreport:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 progress = 100 * float(p.queueindex) / len(p.mainqueue)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 sys.stdout.write("Progress: %02.1f%%\r" % progress)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 p.disconnect()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 sys.exit(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 p.disconnect()

mercurial