Merge fixes from branch 'xorn'
[geda-gaf.git] / xorn / src / backend / gnet_bom.py
blobfac3907ce880e8cfe64f9bc417338190ea61d95d
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 # Bill of Material backend written by Matt Ettus
22 # Bill Of Materials Generator
23 # You must have a file called attribs in the pwd
24 # The file should be a text list of attributes you want listed,
25 # One per line. No comments are allowed in the file.
26 # Questions? Contact matt@ettus.com
27 # This software is released under the terms of the GNU GPL
29 import sys
30 from util_getopt import *
32 def run(f, netlist, args):
33 options = backend_getopt(args, {
34 'attrib_file': Option(False, REQUIRED_ARGUMENT, None),
35 'attribs': Option(False, REQUIRED_ARGUMENT, None)
38 # Parse attrib file or argument.
39 # Store list of read attributes in attriblist.
40 try:
41 attriblist = options['attribs'].split(',')
42 except KeyError:
43 try:
44 g = open(options.get('attrib_file', 'attribs'))
45 try:
46 data = g.read()
47 finally:
48 g.close()
49 except IOError as e:
50 sys.stderr.write("""\
51 ERROR: Can't read attribute file
54 You must do one of the following:
55 - Create an 'attribs' file
56 - Specify an attribute file using -Oattrib_file=<filename>
57 - Specify which attributes to include using
58 -Oattribs=attrib1,attrib2,... (no spaces)
59 """ % str(e))
60 netlist.failed = True
61 return
63 attriblist = []
64 start = 0
65 while start < len(data):
66 ends = [data.find(delim, start) for delim in ' \n\t'] + [len(data)]
67 end = min(end for end in ends if end != -1)
68 if end != start:
69 attriblist.append(data[start:end])
70 start = end + 1
72 if not attriblist:
73 return
75 f.write('refdes\t%s\t\n' % '\t'.join(attriblist))
77 for package in reversed(netlist.packages):
78 if package.get_attribute('nobom', None) == '1':
79 continue
80 f.write('%s\t%s\t\n' % (
81 package.refdes, '\t'.join(package.get_attribute(attrib, 'unknown')
82 for attrib in attriblist)))