1
0
mirror of https://github.com/nophead/Mendel90.git synced 2025-01-16 20:38:15 +01:00

add support for python 3.2 and 3.3, retain backward compatibility

This commit is contained in:
Brad Pitcher 2014-02-06 19:43:19 -08:00
parent 718e8ebd4d
commit 8eff5da9ca
9 changed files with 61 additions and 48 deletions

View File

@ -1,13 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
#from http://kaioa.com/node/42 #from http://kaioa.com/node/42
from __future__ import print_function
import os, subprocess, sys import os, subprocess, sys
def run(*args): def run(*args):
print "inkscape", print("inkscape", end=" ")
for arg in args: for arg in args:
print arg, print(arg, end=" ")
print print()
run = subprocess.Popen(["inkscape"] + list(args) + [" -z"], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE) run = subprocess.Popen(["inkscape"] + list(args) + [" -z"], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
out,err=[e.splitlines() for e in run.communicate()] out,err=[e.splitlines() for e in run.communicate()]
return run.returncode, out, err return run.returncode, out, err
@ -15,8 +18,8 @@ def run(*args):
if __name__=='__main__': if __name__=='__main__':
r = run(sys.argv[1:]) r = run(sys.argv[1:])
if not r[0]==0: if not r[0]==0:
print 'return code:',r[0] print('return code:', r[0])
for l in r[1]: for l in r[1]:
print l print(l)
for l in r[2]: for l in r[2]:
print l print(l)

35
bom.py
View File

@ -1,10 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import os import os
import sys import sys
import shutil import shutil
import openscad import openscad
class BOM: class BOM:
def __init__(self): def __init__(self):
self.count = 1 self.count = 1
@ -34,7 +37,7 @@ class BOM:
return ass.replace("assembly", "assemblies") return ass.replace("assembly", "assemblies")
def print_bom(self, breakdown, file = None): def print_bom(self, breakdown, file = None):
print >> file, "Vitamins:" print("Vitamins:", file=file)
if breakdown: if breakdown:
longest = 0 longest = 0
for ass in self.assemblies: for ass in self.assemblies:
@ -45,10 +48,10 @@ class BOM:
name = ass.replace("_assembly","").replace("_"," ").capitalize() name = ass.replace("_assembly","").replace("_"," ").capitalize()
index = i - (longest - len(name)) index = i - (longest - len(name))
if index < 0: if index < 0:
print >> file, " ", print(" ", end=" ", file=file)
else: else:
print >> file, " %s" % name[index], print(" %s" % name[index], end=" ", file=file)
print >> file print(file=file)
for part in sorted(self.vitamins): for part in sorted(self.vitamins):
if ': ' in part: if ': ' in part:
@ -62,10 +65,10 @@ class BOM:
file.write("%2d|" % bom.vitamins[part]) file.write("%2d|" % bom.vitamins[part])
else: else:
file.write(" |") file.write(" |")
print >> file, "%3d" % self.vitamins[part], description print("%3d" % self.vitamins[part], description, file=file)
print >> file print(file=file)
print >> file, "Printed:" print("Printed:", file=file)
for part in sorted(self.printed): for part in sorted(self.printed):
if breakdown: if breakdown:
for ass in sorted(self.assemblies): for ass in sorted(self.assemblies):
@ -74,13 +77,13 @@ class BOM:
file.write("%2d|" % bom.printed[part]) file.write("%2d|" % bom.printed[part])
else: else:
file.write(" |") file.write(" |")
print >> file, "%3d" % self.printed[part], part print("%3d" % self.printed[part], part, file=file)
print >> file print(file=file)
if self.assemblies: if self.assemblies:
print >> file, "Sub-assemblies:" print("Sub-assemblies:", file=file)
for ass in sorted(self.assemblies): for ass in sorted(self.assemblies):
print >> file, "%3d %s" % (self.assemblies[ass].count, self.assemblies[ass].make_name(ass)) print("%3d %s" % (self.assemblies[ass].count, self.assemblies[ass].make_name(ass)), file=file)
def boms(machine): def boms(machine):
bom_dir = machine + "/bom" bom_dir = machine + "/bom"
@ -93,7 +96,7 @@ def boms(machine):
f.close() f.close()
openscad.run("-D","$bom=2","-o", "dummy.csg", "scad/bom.scad") openscad.run("-D","$bom=2","-o", "dummy.csg", "scad/bom.scad")
print "Generating bom ...", print("Generating bom ...", end=" ")
main = BOM() main = BOM()
stack = [] stack = []
@ -110,7 +113,7 @@ def boms(machine):
else: else:
if s[0] == '/': if s[0] == '/':
if s[1:] != stack[-1]: if s[1:] != stack[-1]:
raise Exception, "Mismatched assembly " + s[1:] + str(stack) raise Exception("Mismatched assembly " + s[1:] + str(stack))
stack.pop() stack.pop()
else: else:
main.add_part(s) main.add_part(s)
@ -122,15 +125,15 @@ def boms(machine):
for ass in sorted(main.assemblies): for ass in sorted(main.assemblies):
f = open(bom_dir + "/" + ass + ".txt", "wt"); f = open(bom_dir + "/" + ass + ".txt", "wt");
bom = main.assemblies[ass] bom = main.assemblies[ass]
print >> f, bom.make_name(ass) + ":" print(bom.make_name(ass) + ":", file=f)
bom.print_bom(False, f) bom.print_bom(False, f)
f.close() f.close()
print " done" print(" done")
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
boms(sys.argv[1]) boms(sys.argv[1])
else: else:
print "usage: bom [mendel|sturdy|your_machine]" print("usage: bom [mendel|sturdy|your_machine]")
sys.exit(1) sys.exit(1)

View File

@ -5,8 +5,12 @@
# It then sorts the triangles to start with the one with the lowest vertices first (comparing first vertex, second, then third) # It then sorts the triangles to start with the one with the lowest vertices first (comparing first vertex, second, then third)
# This has no effect on the model but makes the STL consistent. I.e. it makes a canonical form. # This has no effect on the model but makes the STL consistent. I.e. it makes a canonical form.
# #
from __future__ import print_function
import sys import sys
class Vertex: class Vertex:
def __init__(self, x, y, z): def __init__(self, x, y, z):
self.x, self.y, self.z = x, y, z self.x, self.y, self.z = x, y, z
@ -55,20 +59,20 @@ class STL:
self.facets.sort(key = Facet.key) self.facets.sort(key = Facet.key)
else: else:
print "Not an OpenSCAD ascii STL file" print("Not an OpenSCAD ascii STL file")
sys.exit(1) sys.exit(1)
def write(self, fname): def write(self, fname):
f = open(fname,"wt") f = open(fname,"wt")
print >> f,'solid OpenSCAD_Model' print('solid OpenSCAD_Model', file=f)
for facet in self.facets: for facet in self.facets:
print >> f, ' facet normal %s %s %s' % (facet.normal.dx, facet.normal.dy, facet.normal.dz) print(' facet normal %s %s %s' % (facet.normal.dx, facet.normal.dy, facet.normal.dz), file=f)
print >> f, ' outer loop' print(' outer loop', file=f)
for vertex in facet.vertices: for vertex in facet.vertices:
print >> f, ' vertex %s %s %s' % (vertex.x, vertex.y, vertex.z) print(' vertex %s %s %s' % (vertex.x, vertex.y, vertex.z), file=f)
print >> f, ' endloop' print(' endloop', file=f)
print >> f, ' endfacet' print(' endfacet', file=f)
print >> f, 'endsolid OpenSCAD_Model' print('endsolid OpenSCAD_Model', file=f)
f.close() f.close()
def canonicalise(fname): def canonicalise(fname):
@ -79,5 +83,5 @@ if __name__ == '__main__':
if len(sys.argv) == 2: if len(sys.argv) == 2:
canonicalise(sys.argv[1]) canonicalise(sys.argv[1])
else: else:
print "usage: c14n_stl file" print("usage: c14n_stl file")
sys.exit(1) sys.exit(1)

16
dxf.py
View File

@ -7,9 +7,9 @@ def parse_dxf(fn):
f = open(fn) f = open(fn)
# skip to entities section # skip to entities section
s = f.next() s = next(f)
while s.strip() != 'ENTITIES': while s.strip() != 'ENTITIES':
s = f.next() s = next(f)
in_line = False in_line = False
in_circle = False in_circle = False
@ -27,8 +27,8 @@ def parse_dxf(fn):
keys = dict.fromkeys(['8','10','20','30','11','21','31'], 0.0) keys = dict.fromkeys(['8','10','20','30','11','21','31'], 0.0)
while line != '0': while line != '0':
if line in keys: if line in keys:
keys[line] = float(f.next().strip()) keys[line] = float(next(f).strip())
line = f.next().strip() line = next(f).strip()
pt_list.append( ((keys['10'], keys['20']), (keys['11'], keys['21'])) ) pt_list.append( ((keys['10'], keys['20']), (keys['11'], keys['21'])) )
in_line = False in_line = False
@ -36,8 +36,8 @@ def parse_dxf(fn):
keys = dict.fromkeys(['8','10','20','30','40'], 0.0) keys = dict.fromkeys(['8','10','20','30','40'], 0.0)
while line != '0': while line != '0':
if line in keys: if line in keys:
keys[line] = float(f.next().strip()) keys[line] = float(next(f).strip())
line = f.next().strip() line = next(f).strip()
cir_list.append([[keys['10'], keys['20'], keys['30']], keys['40']]) cir_list.append([[keys['10'], keys['20'], keys['30']], keys['40']])
in_circle = False in_circle = False
@ -102,7 +102,7 @@ def dxf_to_svg(fn):
xmin = ymin = 99999999 xmin = ymin = 99999999
for loop in loops: for loop in loops:
if len(loop) < 4 or loop[0] != loop[-1]: if len(loop) < 4 or loop[0] != loop[-1]:
raise Exception, "loop not closed " + str(loop) raise Exception("loop not closed " + str(loop))
for point in loop: for point in loop:
if point[0] > xmax: xmax = point[0] if point[0] > xmax: xmax = point[0]
if point[0] < xmin: xmin = point[0] if point[0] < xmin: xmin = point[0]
@ -111,7 +111,7 @@ def dxf_to_svg(fn):
def p(x, y): return (x - xmin, ymax - y) def p(x, y): return (x - xmin, ymax - y)
print xmin, ymin, xmax, ymax print(xmin, ymin, xmax, ymax)
scene = Scene(fn[:-4], ceil(ymax - ymin + 10), ceil(xmax - xmin + 10)) scene = Scene(fn[:-4], ceil(ymax - ymin + 10), ceil(xmax - xmin + 10))
for loop in loops: for loop in loops:
circle = is_circle(loop) circle = is_circle(loop)

View File

@ -16,5 +16,5 @@ if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
make_machine(sys.argv[1]) make_machine(sys.argv[1])
else: else:
print "usage: make_machine [mendel|sturdy|your_machine]" print("usage: make_machine [mendel|sturdy|your_machine]")
sys.exit(1) sys.exit(1)

View File

@ -1,10 +1,13 @@
from __future__ import print_function
import subprocess import subprocess
def run(*args): def run(*args):
print "openscad", print("openscad", end=" ")
for arg in args: for arg in args:
print arg, print(arg, end=" ")
print print()
log = open("openscad.log", "w") log = open("openscad.log", "w")
subprocess.call(["openscad"] + list(args), stdout = log, stderr = log) subprocess.call(["openscad"] + list(args), stdout = log, stderr = log)
log.close() log.close()

View File

@ -50,11 +50,11 @@ def plates(machine):
if os.path.isfile(path): if os.path.isfile(path):
shutil.copy(path, target_dir + "/" + file) shutil.copy(path, target_dir + "/" + file)
else: else:
print "can't find %s to copy" % path print("can't find %s to copy" % path)
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
plates(sys.argv[1]) plates(sys.argv[1])
else: else:
print "usage: plates [mendel|sturdy|your_machine]" print("usage: plates [mendel|sturdy|your_machine]")
sys.exit(1) sys.exit(1)

View File

@ -70,5 +70,5 @@ if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
sheets(sys.argv[1]) sheets(sys.argv[1])
else: else:
print "usage: sheets [mendel|sturdy|your_machine]" print("usage: sheets [mendel|sturdy|your_machine]")
sys.exit(1) sys.exit(1)

View File

@ -87,12 +87,12 @@ def stls(machine, parts = None):
# List the ones we didn't find # List the ones we didn't find
# #
for module in targets: for module in targets:
print "Could not find", module print("Could not find", module)
return used return used
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
stls(sys.argv[1], sys.argv[2:]) stls(sys.argv[1], sys.argv[2:])
else: else:
print "usage: stls [mendel|sturdy|your_machine] [part.stl ...]" print("usage: stls [mendel|sturdy|your_machine] [part.stl ...]")
sys.exit(1) sys.exit(1)