2 # generates markdown tables from CSV files, after doing lovely sorting
3 # by different columns, blah blah...
4 from copy
import deepcopy
7 def write_mdwn_row(f
, row
):
12 def underlines(header
):
15 row
.append("-" * len(col
))
19 def is_svp64_page(page
):
20 return page
in ['sv/setvl', 'sv/svstep', 'sv/remap']
22 def sort_by_page(p1
, p2
):
25 if not (is_svp64_page(p1
) ^
is_svp64_page(p2
)):
35 priorities
= ['high', 'med', 'low', 'TBD']
37 def sort_by_priority(p1
, p2
):
38 p1
= priorities
.index(p1
['priority'])
39 p2
= priorities
.index(p2
['priority'])
44 def sort_by_cost(p1
, p2
):
47 if not p1
.isdigit(): p1
= 0
48 if not p2
.isdigit(): p2
= 0
55 def sort_by_cost_priority_page(p1
, p2
):
56 v
= sort_by_cost(p1
, p2
)
58 v
= sort_by_priority(p1
, p2
)
60 v
= sort_by_page(p1
, p2
)
64 def by_cost_then_priority_then_page(areas
):
65 # first blat all columns together (drop area-dict)
67 for row
in areas
.values():
70 res
= sorted(res
, key
=functools
.cmp_to_key(sort_by_cost_priority_page
))
71 # now split out into a dict again this time by cost-priority
77 costs
[cost
].append(row
)
81 # For prettier printing, replace short column heading
82 # names with full, consistent names.
83 # Expected input is a list of column strings
84 def column_header_replacement(header
):
85 replacement_col
= {'cost': 'XO Cost'}
86 new_header
= deepcopy(header
)
87 for index
, shortname
in enumerate(replacement_col
.keys()):
88 # update with replacement if any otherwise leave alone
89 new_header
[index
] = replacement_col
.get(shortname
, shortname
)
93 def print_table(title
, header
, areas
, sortby
):
94 fname
= title
.lower().replace(" ", "_")
95 with
open("ls012/%s.mdwn" % fname
, "w") as f
:
96 # write out the page header
97 f
.write("\\newpage{}\n")
99 f
.write("# %s\n" % title
)
101 # sort everything if required
102 if sortby
is not None:
103 areas
= sortby(areas
)
105 # start writing out areas
106 for title
, rows
in areas
.items():
107 # start new page (if not first newpage)
108 if linecount
is not None:
109 # allow 60 rows per page
110 linecount
+= len(rows
)
114 f
.write("\\newpage{}\n")
116 if linecount
is None: # skipped first newpage
119 f
.write("## %s\n" % title
)
122 # work out maximum length of items, and adjust header
123 hdr
= deepcopy(header
)
128 for hd
, value
in row
.items():
129 cols
[hd
] = max(cols
[hd
], len(value
))
130 # adjust header (add spaces)
131 for i
, hd
in enumerate(hdr
):
132 n_spaces
= cols
[hd
] - len(hd
)
133 hdr
[i
] = hdr
[i
] + " " * n_spaces
135 write_mdwn_row(f
, hdr
)
136 write_mdwn_row(f
, underlines(hdr
))
138 # adjust row (add same spaces as header width)
140 for key
in row
.keys():
141 col_len
, value
= cols
[key
], row
[key
]
143 prefix
= 'https://libre-soc.org/openpower/'
144 v
= value
.replace("_", "\_") # latex, duh
145 url
= '[%s](%s%s)' % (value
, prefix
, v
)
147 elif key
== 'rfc' and value
.startswith('ls'):
148 prefix
= 'https://libre-soc.org/openpower/sv/rfc/'
149 url
= '[%s](%s%s)' % (value
, prefix
, value
)
152 value
= value
.replace("_", "\_") # latex, duh
153 n_spaces
= col_len
- len(value
)
154 r
.append(value
+ " " * n_spaces
)
159 # approx 8 lines per header
162 if __name__
== '__main__':
163 with
open("ls012/optable.csv") as f
:
164 l
= map(str.strip
, f
.readlines())
168 if line
.startswith("#"):
169 area
= line
[1:].strip()
172 # split line by commas, whitespace-strip it
173 line
= list(map(str.strip
, line
.split(',')))
178 # create a dictionary by tuple of header+line
179 linedict
= dict(zip(header
, line
))
184 areas
[area
].append(linedict
)
186 # exccellent - now have a dictionary of list of dictionaries:
187 # area - list-of-instructions - dictionary-by-heading
188 print_table("Areas", header
, areas
, None)
190 # now sort by cost and then by page
191 print_table("XO cost", header
, areas
, by_cost_then_priority_then_page
)