add means to write out csvs.mdwn which contains table pointers to all CSV files
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 7 May 2021 13:59:51 +0000 (14:59 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 7 May 2021 13:59:51 +0000 (14:59 +0100)
src/budget_sync/main.py

index 769b3dc68214aba6406fb14e467b24c7317038b7..3964134e7efe0cc30e209c83186b5910f2bdddea 100644 (file)
@@ -1,12 +1,30 @@
 from bugzilla import Bugzilla
 import logging
 import argparse
+import csv
 from pathlib import Path
 from budget_sync.util import all_bugs
 from budget_sync.config import Config, ConfigParseError
 from budget_sync.budget_graph import BudgetGraph, BudgetGraphBaseError
 from budget_sync.write_budget_markdown import write_budget_markdown
+from collections import OrderedDict
 
+# Write an array of dictionaries to the CSV file name
+def write_csv(name, items, headers):
+    with open(name, 'w') as csvfile:
+        writer = csv.DictWriter(csvfile, headers, lineterminator="\n")
+        writer.writeheader()
+        writer.writerows(items)
+
+mdwn_csv_template = """\
+# %s
+
+[[!table format=csv file="%s"]]
+"""
+
+mdwn_people_template = """\
+ * [%s](%s)
+"""
 
 def main():
     parser = argparse.ArgumentParser(
@@ -69,18 +87,25 @@ def main():
 
     # even quicker hack to create something vaguely resembling a CSV file
     milestone_csvs = {}
+    milestone_headings = {}
+    all_people = OrderedDict()
+    # pre-initialise the CSV lists (to avoid overwrite)
+    for milestone, payments in budget_graph.milestone_payments.items():
+        milestone_csvs[milestone] = [] # rows in the CSV file
+
     for milestone, payments in budget_graph.milestone_payments.items():
         # first get the list of people, then create some columns
         people = milestones_people[milestone]
         headings = []
         for person in people:
             name = str(person).replace(" ", "_")
+            all_people[person] = person
             # name, amount, requested (submitted), paid
             headings.append(name+"_amount")
             headings.append(name+"_req")
             headings.append(name+"_paid")
+        milestone_headings[milestone] = headings
         # now we go through the whole "payments" thing again...
-        milestone_csvs[milestone] = [] # rows in the CSV file
         row = {}
         for payment in payments:
             name = str(payment.payee.identifier).replace(" ", "_")
@@ -95,8 +120,24 @@ def main():
                 paid = ""
             row[name+"_req"] = requested
             row[name+"_paid"] = paid
-        print (row)
-        milestone_csvs[milestone].append(row)
+            print (row)
+            milestone_csvs[milestone].append(row)
+
+    if args.output_dir is not None:
+        with open("%s/csvs.mdwn" % args.output_dir, "w") as f:
+            # write out the people pages
+            # TODO, has to be done by the markdown page name
+            #f.write("# People\n\n")
+            #for name, person in all_people.items():
+            #    fname = "%s/%s" % (args.output_dir, name)
+            #    f.write(mdwn_people_template % (person, fname))
+            # and the CSV files
+            for milestone, rows in milestone_csvs.items():
+                ident = milestone.identifier
+                header = milestone_headings[milestone]
+                fname = "%s/%s.csv" % (args.output_dir, ident)
+                write_csv(fname, rows, header)
+                f.write(mdwn_csv_template % (ident, fname))
 
 if __name__ == "__main__":
     main()