commit a68ae68f94b344b3890758f7036f07d6f26f2827
parent 592abbec820082e1c40def12751a9db2b5be15a6
Author: Juan F. Meleiro <juan@juanmeleiro.mat.br>
Date: Thu, 6 Oct 2022 20:57:54 -0300
format: Add first draft of history formatting script
Diffstat:
| A | format.py | | | 64 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 64 insertions(+), 0 deletions(-)
diff --git a/format.py b/format.py
@@ -0,0 +1,64 @@
+'''Format history.tsv into emailable format'''
+import csv
+import pprint
+import string
+
+class NotesFormatter(string.Formatter):
+ def __init__(self, notes=[]):
+ string.Formatter.__init__(self)
+ self.notes = notes
+ self.abbrevs = []
+
+ def get_value(self, key, args, kwds):
+ if key == "notes":
+ n = kwds["notes"]
+ if n == "":
+ return n
+ cur = len(self.notes)
+ self.notes.append(n)
+ return "[{}]".format(cur)
+ elif key == "name":
+ # This is bad and should be replaced by overwriting get_field
+ n = kwds["name"]
+ MAX = 20
+ if len(n) > MAX:
+ cur = len(self.abbrevs)
+ self.abbrevs.append(n)
+ return n[0:(MAX-3-len(str(cur)))] + "…[{}]".format(cur)
+ else:
+ return n
+ else:
+ return string.Formatter.get_value(self, key, args, kwds)
+
+columns = [
+ "rune",
+ "name",
+ "contact",
+ "registration",
+ "deregistration",
+ "notes"
+]
+history = []
+
+with open("history.tsv", "r") as f:
+ csv = csv.reader(f, delimiter="\t")
+ next(csv)
+ for line in csv:
+ entry = dict()
+ for i, n in enumerate(columns):
+ if i < len(line):
+ entry[n] = line[i]
+ else:
+ entry[n] = ""
+ history.append(entry)
+
+notes = []
+formatted = []
+entryformat = "{rune: <2} {name: <20} {registration: >10} {deregistration: >10} {notes}"
+formatter = NotesFormatter()
+
+print(entryformat.format(rune="", name="Name", registration="From", deregistration="Until", notes="Notes"))
+for h in history:
+ print(formatter.vformat(entryformat, [], h).strip())
+for i, n in enumerate(formatter.notes):
+ print("[{}]: {}".format(i, n))