Merge fixes from branch 'xorn'
[geda-gaf.git] / xorn / src / backend / gnet_allegro.py
blob66c4cecc0cda53add3b47d7b6c75f7d9f39d7ec1
1 # gaf.netlist - gEDA Netlist Extraction and Generation
2 # Copyright (C) 1998-2010 Ales Hvezda
3 # Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
4 # Copyright (C) 2013-2020 Roland Lutz
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # Allegro netlist format
22 import errno, os, sys
24 def open_and_check(device):
25 try:
26 os.mkdir('devfiles')
27 except OSError as e:
28 mkdir_error = e
29 else:
30 mkdir_error = None
32 try:
33 f = open('devfiles/%s.txt' % device.lower(), 'w')
34 except IOError as e:
35 if e.errno == errno.ENOENT and mkdir_error is not None:
36 sys.stderr.write("%s\n" % mkdir_error)
37 else:
38 sys.stderr.write("%s\n" % e)
39 sys.stderr.write("The device files are expected to be "
40 "in the 'devfiles' directory.\n")
41 sys.exit(3)
43 return f
45 def output_netlist(f, package):
46 f.write('(Device File generated by gEDA Allegro Netlister)\n')
47 f.write('PACKAGE %s\n' % package.get_attribute('footprint', 'unknown'))
48 f.write('CLASS %s\n' % package.get_attribute('class', 'unknown'))
49 f.write('PINCOUNT %s\n' % package.get_attribute('pins', 'unknown'))
50 altfoot = package.get_attribute('alt_foot', None)
51 if altfoot is not None:
52 f.write('PACKAGEPROP ALT_SYMBOLS\n')
53 f.write("'(%s)'\n" % altfoot)
54 f.write('END\n')
56 def get_component_text(package, default):
57 try:
58 return package.get_attribute('value')
59 except KeyError:
60 try:
61 return package.get_attribute('label')
62 except KeyError:
63 return package.get_attribute('device', default)
65 def run(f, netlist):
66 f.write('(Allegro netlister by M. Ettus)\n')
68 f.write('$PACKAGES\n')
69 for package in reversed(netlist.packages):
70 footprint = package.get_attribute('footprint', None)
71 if footprint is not None:
72 f.write(footprint)
73 f.write('! %s! %s; %s\n' % (package.get_attribute('device', 'unknown'),
74 get_component_text(package, 'unknown'),
75 package.refdes))
77 f.write('$NETS\n')
78 for net in reversed(netlist.nets):
79 f.write(net.name)
80 f.write(';')
81 f.write(',\n'.join(' %s.%s' % (pin.package.refdes, pin.number)
82 for pin in reversed(net.connections)) + '\n')
84 f.write('$END\n')
86 # write device files
87 done = []
88 for package in reversed(netlist.packages):
89 device = package.get_attribute('device', 'unknown')
90 if device in done:
91 continue
93 if f is sys.stdout:
94 output_netlist(f, package)
95 else:
96 g = open_and_check(device)
97 try:
98 output_netlist(g, package)
99 finally:
100 g.close()