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 json
import argparse, logging, sys, os, collections
from typing import Dict, List, TypedDict
from config import Config
from project import Project
from documentation import Docs
from shuttle import ShuttleConfig
@@ -16,7 +18,7 @@ signal(SIGPIPE, SIG_DFL)
class Projects():
def __init__(self, config, args):
def __init__(self, config: Config, args):
self.args = args
self.config = config
self.project_dir = config['project_dir']
@@ -24,7 +26,7 @@ class Projects():
if not os.path.exists(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))]
if args.test:
project_list = ['tt_um_chip_rom', 'tt_um_factory_test']
@@ -78,18 +80,18 @@ class Projects():
exit(1)
def build_metrics(self):
total_seconds = 0
total_seconds = 0.0
total_wire_length = 0
total_wires_count = 0
total_physical_cells = 0
max_cells = 0
min_cells = 1000
max_cell_project = None
max_util = 0
min_util = 100
max_util = 0.0
min_util = 100.0
max_util_project = None
languages = {}
tags = []
languages: Dict[str, int] = {}
tags: List[str] = []
for project in self.projects:
try:
@@ -104,7 +106,8 @@ class Projects():
script_dir = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(script_dir, 'categories.json')) as fh:
categories = json.load(fh)
by_category = {}
CategoryInfo = TypedDict("CategoryInfo", {'count': int, 'examples': List[str]})
by_category: Dict[str, CategoryInfo] = {}
total = 0
for cell_name in cell_count:
cat_index = categories['map'][cell_name]
@@ -166,8 +169,8 @@ class Projects():
tags = [x.strip().lower() for x in tags]
tags = list(filter(lambda a: a != "", tags))
def count_items(lst):
freq = {}
def count_items(lst: List[str]):
freq: Dict[str, int] = {}
for item in lst:
if item not in freq:
freq[item] = 1
@@ -232,7 +235,7 @@ if __name__ == '__main__':
docs = Docs(config, projects.projects, args=args)
shuttle = ShuttleConfig(config, projects.projects, modules_yaml_name)
rom = ROMFile(config, projects.projects)
rom = ROMFile(config)
if args.list:
shuttle.list()
+6 -5
View File
@@ -4,14 +4,15 @@ import subprocess
import shutil
import json
import git
import frontmatter
import frontmatter # type: ignore
from config import Config
from git_utils import get_first_remote
from markdown_utils import latex_centered_image, rewrite_image_paths
class Docs():
def __init__(self, config, projects, args):
def __init__(self, config: Config, projects, args):
self.config = config
self.projects = projects
self.args = args
@@ -110,15 +111,15 @@ class Docs():
.format(**yaml_data)
)
fh.write(doc)
fh.write("\n\clearpage\n")
fh.write("\n\\clearpage\n")
except IndexError:
logging.warning("missing pins in info.yaml, skipping")
# ending
fh.write(doc_pinout)
fh.write("\n\clearpage\n")
fh.write("\n\\clearpage\n")
fh.write(doc_info)
fh.write("\n\clearpage\n")
fh.write("\n\\clearpage\n")
fh.write(doc_credits)
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
from urllib.parse import urlparse
from config import Config
MAX_ROM_TEXT_SIZE = 92
@@ -25,20 +26,19 @@ segment_font = {
"t": 0b01111000,
}
def segment_char(c):
def segment_char(c: str):
return segment_font[c]
class ROMFile:
def __init__(self, config, projects):
self.projects = projects
def __init__(self, config: Config):
self.config = config
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:]
def get_git_commit_hash(self):
return git.Repo(".").commit().hexsha
return Repo(".").commit().hexsha
def write_rom(self):
rom = bytearray(256)
@@ -63,7 +63,7 @@ class ROMFile:
continue
fh.write(f"// {line}\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):
fh.write("{:02x} ".format(rom[line + byte]))
fh.write("{:02x} ".format(rom[line_offset + byte]))
fh.write("\n")