# generates markdown tables from CSV files, after doing lovely sorting
# by different columns, blah blah...
from copy import deepcopy
+import functools
def write_mdwn_row(f, row):
row = "|".join(row)
row.append("-" * len(col))
return row
+# sorting functions
+def is_svp64_page(page):
+ return page in ['sv/setvl', 'sv/svstep', 'sv/remap']
+
+def sort_by_page(p1, p2):
+ p1 = p1['page']
+ p2 = p2['page']
+ if not (is_svp64_page(p1) ^ is_svp64_page(p2)):
+ if p1 < p2: return -1
+ if p1 > p2: return 1
+ return 0
+ if is_svp64_page(p1):
+ return -1
+ if is_svp64_page(p2):
+ return 1
+ return 0
+
+priorities = ['high', 'med', 'low', 'TBD']
+
+def sort_by_priority(p1, p2):
+ p1 = priorities.index(p1['priority'])
+ p2 = priorities.index(p2['priority'])
+ if p1 < p2: return -1
+ if p1 > p2: return 1
+ return 0
+
+def sort_by_cost(p1, p2):
+ p1 = p1['cost']
+ p2 = p2['cost']
+ if not p1.isdigit(): p1 = 0
+ if not p2.isdigit(): p2 = 0
+ p1 = int(p1)
+ p2 = int(p2)
+ if p1 < p2: return -1
+ if p1 > p2: return 1
+ return 0
+
+def sort_by_cost_priority_page(p1, p2):
+ v = sort_by_cost(p1, p2)
+ if v == 0:
+ v = sort_by_priority(p1, p2)
+ if v == 0:
+ v = sort_by_page(p1, p2)
+ return v
+
+
+def by_cost_then_priority_then_page(areas):
+ # first blat all columns together (drop area-dict)
+ res = []
+ for row in areas.values():
+ res += row
+ # now sort them
+ res = sorted(res, key=functools.cmp_to_key(sort_by_cost_priority_page))
+ # now split out into a dict again this time by cost-priority
+ costs = {}
+ for row in res:
+ cost = row['cost']
+ if cost not in costs:
+ costs[cost] = []
+ costs[cost].append(row)
+ return costs
+
+
def print_table(title, header, areas, sortby):
fname = title.lower().replace(" ", "_")
with open("ls012/%s.mdwn" % fname, "w") as f:
# start writing out areas
for title, rows in areas.items():
# start new page (if not first newpage)
+ if linecount is not None:
+ # allow 60 rows per page
+ linecount += len(rows)
+ if linecount >= 60:
+ linecount = 0
if linecount == 0:
f.write("\\newpage{}\n")
f.write("\n")
write_mdwn_row(f, r)
f.write("\n\n")
- # approx 6 lines per header
- linecount += 6
-
- # allow 60 rows per page
- linecount += len(rows)
- if linecount >= 60:
- linecount = 0
+ # approx 8 lines per header
+ linecount += 9
if __name__ == '__main__':
with open("ls012/optable.csv") as f:
# area - list-of-instructions - dictionary-by-heading
print_table("Areas", header, areas, None)
+ # now sort by cost and then by page
+ print_table("XO cost", header, areas, by_cost_then_priority_then_page)
+