From 636f4650f810897b1542f6bd537735ba8794c2bb Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Fri, 23 Feb 2024 21:56:05 +0100 Subject: Use command line options instead of hardcoded values --- forms/colour_chart.py | 75 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/forms/colour_chart.py b/forms/colour_chart.py index bbdf021..7ec7ddd 100755 --- a/forms/colour_chart.py +++ b/forms/colour_chart.py @@ -9,10 +9,6 @@ import strictyaml class Chart: - COLUMNS=4 - ROWS=16 - INDEX_COLS=6 - def __init__(self): self.parser = self.get_parser() self.args = self.parser.parse_args() @@ -24,10 +20,40 @@ class Chart: "chart_data", type=argparse.FileType("r") ) + parser.add_argument( + "--output", + type=argparse.FileType("w"), + default=None, + help="Destination file", + ) + parser.add_argument( + "--columns", "-c", + default=4, + type=int, + help="Number of columns per page", + ) + parser.add_argument( + "--rows", "-r", + default=16, + type=int, + help="Number of rows per page", + ) + parser.add_argument( + "--index-columns", "-i", + default=6, + type=int, + help="Number of columns per index page", + ) + parser.add_argument( + "--background-fill", "-b", + action=argparse.BooleanOptionalAction, + default=True, + help="Fill the background with the target colour", + ) return parser def get_arranged_cells(self): - per_page = self.COLUMNS * self.ROWS + per_page = self.args.columns * self.args.rows padded = self.data["colours"] + [{}] * ( per_page - len(self.data["colours"]) % per_page ) @@ -37,8 +63,8 @@ class Chart: ] cells = [ [ - [p[i + self.ROWS * j] for j in range(self.COLUMNS)] - for i in range(len(p) // self.COLUMNS) + [p[i + self.args.rows * j] for j in range(self.args.columns)] + for i in range(len(p) // self.args.columns) ] for p in pages ] @@ -46,13 +72,13 @@ class Chart: def get_index(self): index = [ - (c["label"], i // self.ROWS) + (c["label"], i // self.args.rows) for i, c in enumerate(self.data["colours"]) ] index.sort(key=operator.itemgetter(0)) return index - def write_pdf(self, fname): + def write_pdf(self): pdf = fpdf.FPDF(format="A5") pdf.add_font( family="FreeSans", @@ -70,27 +96,33 @@ class Chart: for p_num, page in enumerate(self.get_arranged_cells()): pdf.add_page() - pdf.set_font("FreeSerif", size=12) + pdf.set_font("FreeSerif", size=10) + cell_width = (148 - 20) / (self.args.columns * 2) pdf.cell(64, 8, self.data["title"], align="C") pdf.ln() - pdf.set_font("FreeSans", "B", 12) - for j in range(self.COLUMNS): - pdf.cell(16, 8, str(j + p_num * self.COLUMNS),) - pdf.cell(16, 8, "") + pdf.set_font("FreeSans", "B", 10) + for j in range(self.args.columns): + pdf.cell(cell_width, 8, str(j + p_num * self.args.columns),) + pdf.cell(cell_width, 8, "") pdf.ln() for row in page: for cell in row: if cell: pdf.set_fill_color(255,255,255) - if cell.get("bg"): + if self.args.background_fill and cell.get("bg"): pdf.set_fill_color(*map(int, cell["bg"].split())) - pdf.cell(16, 8, "", border=1, fill=True) - pdf.cell(16, 8, cell["label"], ) + pdf.cell(cell_width, 8, "", border=1, fill=True) + pdf.cell(cell_width, 8, cell["label"], ) pdf.ln(10) + pdf.add_page() + pdf.set_font("FreeSerif", size=10) + cell_width = (148 - 20) / (self.args.columns * 2) + pdf.cell(64, 8, self.data["title"], align="C") + pdf.ln() pdf.set_font("FreeSerif", size=10) with pdf.text_columns( - ncols=self.INDEX_COLS, + ncols=self.args.index_columns, balance=True, gutter=4, text_align="RIGHT", @@ -103,8 +135,13 @@ class Chart: cols.write(str(c[1])) cols.ln() + fname = ( + getattr(self.args.output, "name", None) or + self.args.chart_data.name.replace(".yaml", ".pdf") + ) + print(fname) pdf.output(fname) if __name__ == "__main__": - Chart().write_pdf("asd.pdf") + Chart().write_pdf() -- cgit v1.2.3