#!/usr/bin/env python3 import shutil import csv import os import re import sys from pathlib import Path from itertools import groupby from collections import defaultdict, namedtuple field_skip = set( "18 Kb", ) field_mapping = { "36 Kb": ("RAM blocks", None), "Max (Kb)": ("RAM", None), "Total I/O Banks": ("IO banks", None), "Logic Cells": (None, "Xilix logic cells"), "CMTs": ("Clock management tiles", "Xilix Clock management tiles"), } extra_fields = { "RAM Block Size": "36 kB" } def index_by_header(rows): item_keys = rows[0] items = {k: {} for idx, k in enumerate(item_keys) if idx > 0} fields = set() for r_idx, row in enumerate(rows): for c_idx, cell in enumerate(row): if c_idx == 0: field = cell fields.add(field) else: key = item_keys[c_idx] item = items[key] item[field] = cell return fields, items def table_to_map(table, keys_in: str = None): if keys_in == "rows": data = {} fields = table[0] for row in table[1:]: key = row[0] data[key] = o = {} for idx, cell in enumerate(row): field = fields[idx] o[field] = cell return data else: raise Exception("unknown keys_in value: {}".format(keys_in)) def read_csv(f, remove_notes = True): table = [row for row in csv.reader(f) if not row[0].startswith("#") and len(row[0]) > 0] if remove_notes: r = r"\(.*\)" table[0] = [re.sub(r, "", name) for name in table[0]] table[0] = [name.strip() for name in table[0]] return table dbg = lambda x: '' dbg = print p = print with open("temperatures.csv") as f: rows = list(csv.reader(f)) Temperature = namedtuple("Temperature", rows[0]) temperatures = {row[0]: Temperature(*row) for row in rows[1:]} print("Temperatures") for t in temperatures.values(): print(t) with open("packages.csv") as f: rows = list(csv.reader(f)) Package = namedtuple("Package", [r.lower().replace(" ", "_") for r in rows[0]]) packages = [Package(*r) for r in rows[1:]] print("Packages") for p in packages: print(p) with open("package-devices.csv") as f: rows = read_csv(f) # for r in rows: # print(r) r = r"\(.*\)" package_idx = {int(idx): re.sub(r, "", part) for idx, part in enumerate(rows[0]) if len(part) and idx > 0} io_by_part = {} for row in rows[1:]: part = row[0] io_by_part[part] = info = {} for idx, package in package_idx.items(): transceivers = row[idx] if len(transceivers) == 0: continue pins = row[idx + 1] info[package] = (package, pins, transceivers) # print("package={}, pins={}, tx={}".format(package, pins, transceivers)) print("IO") for part, info in io_by_part.items(): print(part) for package, pins, tx in info.values(): print(" {}: pins: {}, transceivers: {}".format(package, pins, tx)) if False: sys.exit(1) with open("parts.csv") as f: table = read_csv(f) parts = table_to_map(table, keys_in="rows") out_dir = Path("mw") if out_dir.exists(): shutil.rmtree(out_dir) out_dir.mkdir() # print("parts") # for part, info in rows.items(): # print(info) for part in parts.values(): part_number = part["Part number"] for k, v in extra_fields.items(): part[k] = v print("--- PART: {} ---".format(part_number)) path = out_dir / "{}.mw".format(part_number) with open(path, "w") as out: p = lambda s = None: print(s if s is not None else "", file=out) def field(k, v): pass p("= {} =".format(part_number)) p() p("{|class='wikitable'") for field, value in part.items(): if field in field_skip: continue mapping = field_mapping.get(field) f = field if mapping is not None: field = mapping[0] if mapping[0] else field f = mapping[1] if mapping[1] else field p("!{}".format(field)) p("|[[{}::{}]]".format(f, value)) p("|-") p("|}") p() p("[[Category:Generated]]") p("[[Category:FPGA Chip]]") p("[[Category:Artix-7 generated data set]]")