Merge fixes from branch 'xorn'
[geda-gaf.git] / xorn / src / backend / partslist_common.py
blob59622d1b71112c4d90ff67d487623015af6ab9fc
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 # Copyright (C) 2001-2010 MIYAMOTO Takanori
22 def get_parts_table(netlist):
23 return [(package.refdes,
24 package.get_attribute('device', 'unknown'),
25 package.get_attribute('value', 'unknown'),
26 package.get_attribute('footprint', 'unknown')) # sdb change
27 for package in reversed(netlist.packages)
28 if package.get_attribute('device', 'unknown') != 'include']
30 def write_one_row(f, ls, separator, end_char):
31 if ls:
32 f.write(separator.join(ls) + end_char)
34 def marge_sort_sub(ls1, ls2, key_column):
35 if not ls1 or not ls2:
36 return ls1 + ls2
38 if ls1[0][key_column].lower() <= ls2[0][key_column].lower():
39 return [ls1[0]] + marge_sort_sub(ls1[1:], ls2, key_column)
40 else:
41 return [ls2[0]] + marge_sort_sub(ls1, ls2[1:], key_column)
43 def marge_sort(ls, key_column):
44 if len(ls) <= 1:
45 return ls
47 return marge_sort_sub(
48 marge_sort(ls[:len(ls) - len(ls) / 2], key_column),
49 marge_sort(ls[len(ls) - len(ls) / 2:], key_column), key_column)
51 def marge_sort_with_multikey(ls, key_columns):
52 if len(ls) <= 1 or not key_columns:
53 return ls
55 key_column = key_columns[0]
56 sorted_ls = marge_sort(ls, key_column)
58 first_value = sorted_ls[0][key_column]
59 match_length = 0
60 for i, x in enumerate(reversed(sorted_ls)):
61 if x[key_column] == first_value:
62 match_length = len(sorted_ls) - i
63 break
65 return marge_sort_with_multikey(list(reversed(sorted_ls[:match_length])),
66 key_columns[1:]) + \
67 marge_sort_with_multikey(sorted_ls[match_length:], key_columns)