create tables from optable.csv
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 7 Apr 2023 19:24:37 +0000 (20:24 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 7 Apr 2023 19:24:37 +0000 (20:24 +0100)
openpower/sv/rfc/ls012_optable.py [new file with mode: 0644]

diff --git a/openpower/sv/rfc/ls012_optable.py b/openpower/sv/rfc/ls012_optable.py
new file mode 100644 (file)
index 0000000..7c2d331
--- /dev/null
@@ -0,0 +1,102 @@
+#!/usr/bin/env python3
+# generates markdown tables from CSV files, after doing lovely sorting
+# by different columns, blah blah...
+from copy import deepcopy
+
+def write_mdwn_row(f, row):
+    row = "|".join(row)
+    row = "|%s|\n" % row
+    f.write(row)
+
+def underlines(header):
+    row = []
+    for col in header:
+        row.append("-" * len(col))
+    return row
+
+def print_table(title, header, areas, sortby):
+    fname = title.lower().replace(" ", "_")
+    with open("ls012/%s.mdwn" % fname, "w") as f:
+        # write out the page header
+        f.write("\\newpage{}\n")
+        f.write("\n")
+        f.write("# %s\n" % title)
+        f.write("\n")
+        # sort everything if required
+        if sortby is not None:
+            areas = sortby(areas)
+        linecount = None
+        # start writing out areas
+        for title, rows in areas.items():
+            # start new page (if not first newpage)
+            if linecount == 0:
+                f.write("\\newpage{}\n")
+                f.write("\n")
+            if linecount is None: # skipped first newpage
+                linecount = 0
+
+            f.write("## %s\n" % title)
+            f.write("\n")
+
+            # work out maximum length of items, and adjust header
+            hdr = deepcopy(header)
+            cols = {}
+            for hd in hdr:
+                cols[hd] = len(hd)
+            for row in rows:
+                for hd, value in row.items():
+                    cols[hd] = max(cols[hd], len(value))
+            # adjust header (add spaces)
+            for i, hd in enumerate(hdr):
+                n_spaces = cols[hd] - len(hd)
+                hdr[i] = hdr[i] + " " * n_spaces
+            # write out header
+            write_mdwn_row(f, hdr)
+            write_mdwn_row(f, underlines(hdr))
+            for row in rows:
+                # adjust row (add same spaces as header width)
+                r = []
+                for (value, col_len) in zip(row.values(), cols.values()):
+                    value = value.replace("_", "\_") # latex, duh
+                    n_spaces = col_len - len(value)
+                    r.append(value + " " * n_spaces)
+                # write row
+                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
+
+if __name__ == '__main__':
+    with open("ls012/optable.csv") as f:
+        l = map(str.strip, f.readlines())
+    areas = {}
+    header = None
+    for line in l:
+        if line.startswith("#"):
+            area = line[1:].strip()
+            areas[area]= []
+            continue
+        # split line by commas, whitespace-strip it
+        line = list(map(str.strip, line.split(',')))
+        # identify header
+        if header is None:
+            header = line
+            continue
+        # create a dictionary by tuple of header+line
+        linedict = dict(zip(header, line))
+        print (area)
+        print (linedict)
+        print ()
+        # store line in area
+        areas[area].append(linedict)
+
+    # exccellent - now have a dictionary of list of dictionaries:
+    # area - list-of-instructions - dictionary-by-heading
+    print_table("Areas", header, areas, None)
+