refactor: add more types

This commit is contained in:
Uri Shaked
2023-09-19 20:42:47 +03:00
parent 3b469b9488
commit ff0eea8794
4 changed files with 37 additions and 24 deletions
+9
View File
@@ -0,0 +1,9 @@
from typing import TypedDict
class Config(TypedDict):
"""TypedDict for Tiny Tapeout's config.yaml file."""
id: str
name: str
project_dir: str
start_date: str
+14 -11
View File
@@ -3,6 +3,8 @@ import datetime
import yaml import yaml
import json import json
import argparse, logging, sys, os, collections import argparse, logging, sys, os, collections
from typing import Dict, List, TypedDict
from config import Config
from project import Project from project import Project
from documentation import Docs from documentation import Docs
from shuttle import ShuttleConfig from shuttle import ShuttleConfig
@@ -16,7 +18,7 @@ signal(SIGPIPE, SIG_DFL)
class Projects(): class Projects():
def __init__(self, config, args): def __init__(self, config: Config, args):
self.args = args self.args = args
self.config = config self.config = config
self.project_dir = config['project_dir'] self.project_dir = config['project_dir']
@@ -24,7 +26,7 @@ class Projects():
if not os.path.exists(self.project_dir): if not os.path.exists(self.project_dir):
os.makedirs(self.project_dir) os.makedirs(self.project_dir)
self.projects = [] self.projects: List[Project] = []
project_list = [entry for entry in os.listdir(self.project_dir) if os.path.isdir(os.path.join(self.project_dir, entry))] project_list = [entry for entry in os.listdir(self.project_dir) if os.path.isdir(os.path.join(self.project_dir, entry))]
if args.test: if args.test:
project_list = ['tt_um_chip_rom', 'tt_um_factory_test'] project_list = ['tt_um_chip_rom', 'tt_um_factory_test']
@@ -78,18 +80,18 @@ class Projects():
exit(1) exit(1)
def build_metrics(self): def build_metrics(self):
total_seconds = 0 total_seconds = 0.0
total_wire_length = 0 total_wire_length = 0
total_wires_count = 0 total_wires_count = 0
total_physical_cells = 0 total_physical_cells = 0
max_cells = 0 max_cells = 0
min_cells = 1000 min_cells = 1000
max_cell_project = None max_cell_project = None
max_util = 0 max_util = 0.0
min_util = 100 min_util = 100.0
max_util_project = None max_util_project = None
languages = {} languages: Dict[str, int] = {}
tags = [] tags: List[str] = []
for project in self.projects: for project in self.projects:
try: try:
@@ -104,7 +106,8 @@ class Projects():
script_dir = os.path.dirname(os.path.realpath(__file__)) script_dir = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(script_dir, 'categories.json')) as fh: with open(os.path.join(script_dir, 'categories.json')) as fh:
categories = json.load(fh) categories = json.load(fh)
by_category = {} CategoryInfo = TypedDict("CategoryInfo", {'count': int, 'examples': List[str]})
by_category: Dict[str, CategoryInfo] = {}
total = 0 total = 0
for cell_name in cell_count: for cell_name in cell_count:
cat_index = categories['map'][cell_name] cat_index = categories['map'][cell_name]
@@ -166,8 +169,8 @@ class Projects():
tags = [x.strip().lower() for x in tags] tags = [x.strip().lower() for x in tags]
tags = list(filter(lambda a: a != "", tags)) tags = list(filter(lambda a: a != "", tags))
def count_items(lst): def count_items(lst: List[str]):
freq = {} freq: Dict[str, int] = {}
for item in lst: for item in lst:
if item not in freq: if item not in freq:
freq[item] = 1 freq[item] = 1
@@ -232,7 +235,7 @@ if __name__ == '__main__':
docs = Docs(config, projects.projects, args=args) docs = Docs(config, projects.projects, args=args)
shuttle = ShuttleConfig(config, projects.projects, modules_yaml_name) shuttle = ShuttleConfig(config, projects.projects, modules_yaml_name)
rom = ROMFile(config, projects.projects) rom = ROMFile(config)
if args.list: if args.list:
shuttle.list() shuttle.list()
+6 -5
View File
@@ -4,14 +4,15 @@ import subprocess
import shutil import shutil
import json import json
import git import git
import frontmatter import frontmatter # type: ignore
from config import Config
from git_utils import get_first_remote from git_utils import get_first_remote
from markdown_utils import latex_centered_image, rewrite_image_paths from markdown_utils import latex_centered_image, rewrite_image_paths
class Docs(): class Docs():
def __init__(self, config, projects, args): def __init__(self, config: Config, projects, args):
self.config = config self.config = config
self.projects = projects self.projects = projects
self.args = args self.args = args
@@ -110,15 +111,15 @@ class Docs():
.format(**yaml_data) .format(**yaml_data)
) )
fh.write(doc) fh.write(doc)
fh.write("\n\clearpage\n") fh.write("\n\\clearpage\n")
except IndexError: except IndexError:
logging.warning("missing pins in info.yaml, skipping") logging.warning("missing pins in info.yaml, skipping")
# ending # ending
fh.write(doc_pinout) fh.write(doc_pinout)
fh.write("\n\clearpage\n") fh.write("\n\\clearpage\n")
fh.write(doc_info) fh.write(doc_info)
fh.write("\n\clearpage\n") fh.write("\n\\clearpage\n")
fh.write(doc_credits) fh.write(doc_credits)
logging.info(f'wrote markdown to {self.args.dump_markdown}') logging.info(f'wrote markdown to {self.args.dump_markdown}')
+8 -8
View File
@@ -1,6 +1,7 @@
import git from git.repo import Repo
import os import os
from urllib.parse import urlparse from urllib.parse import urlparse
from config import Config
MAX_ROM_TEXT_SIZE = 92 MAX_ROM_TEXT_SIZE = 92
@@ -25,20 +26,19 @@ segment_font = {
"t": 0b01111000, "t": 0b01111000,
} }
def segment_char(c): def segment_char(c: str):
return segment_font[c] return segment_font[c]
class ROMFile: class ROMFile:
def __init__(self, config, projects): def __init__(self, config: Config):
self.projects = projects
self.config = config self.config = config
def get_git_remote(self): def get_git_remote(self):
repo_url = list(git.Repo(".").remotes[0].urls)[0] repo_url = list(Repo(".").remotes[0].urls)[0]
return urlparse(repo_url).path[1:] return urlparse(repo_url).path[1:]
def get_git_commit_hash(self): def get_git_commit_hash(self):
return git.Repo(".").commit().hexsha return Repo(".").commit().hexsha
def write_rom(self): def write_rom(self):
rom = bytearray(256) rom = bytearray(256)
@@ -63,7 +63,7 @@ class ROMFile:
continue continue
fh.write(f"// {line}\n") fh.write(f"// {line}\n")
fh.write("\n") fh.write("\n")
for line in range(0, len(rom), 16): for line_offset in range(0, len(rom), 16):
for byte in range(0, 16): for byte in range(0, 16):
fh.write("{:02x} ".format(rom[line + byte])) fh.write("{:02x} ".format(rom[line_offset + byte]))
fh.write("\n") fh.write("\n")