Skip to content

Commit 5e1a613

Browse files
committed
Make PortMap more normal
1 parent fe4e9e9 commit 5e1a613

File tree

2 files changed

+15
-43
lines changed

2 files changed

+15
-43
lines changed

chipflow_lib/pin_lock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44

55
from pathlib import Path
6+
from pprint import pformat
67

78
from chipflow_lib import _parse_config, _ensure_chipflow_root
89
from chipflow_lib.platforms import top_components, LockFile, PACKAGE_DEFINITIONS
@@ -23,7 +24,7 @@ def lock_pins() -> None:
2324

2425
if lockfile.exists():
2526
oldlock = LockFile.model_validate_json(lockfile.read_text())
26-
27+
print(f"Old Lock =\n{pformat(oldlock)}")
2728
print(f"Locking pins: {'using pins.lock' if lockfile.exists() else ''}")
2829

2930
# Get package definition from dict instead of Pydantic model

chipflow_lib/platforms/utils.py

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def __str__(self):
285285

286286
class Port(pydantic.BaseModel):
287287
type: str
288-
pins: List[str] | None # None implies must be allocated at end
288+
pins: List[Pin] | None # None implies must be allocated at end
289289
port_name: str
290290
direction: Optional[io.Direction] = None
291291
options: Optional[dict] = None
@@ -430,57 +430,28 @@ def _allocate_pins(name: str, member: Dict[str, Any], pins: List[str], port_name
430430
Interface = Dict[str, Port]
431431
Component = Dict[str, Interface]
432432

433-
class PortMap(MutableMapping[str, Component]):
434-
def __init__(self, data = {}):
435-
self.map: Dict[str, Component] = data
436-
return super().__init__()
437-
"Represents a mapping of `Port`s to Package pins, grouped by `Component` and `Interface`"
438-
def __getitem__(self, key: str):
439-
"Gets an `Component` from the map by name"
440-
return self.map[key]
441-
442-
def __setitem__(self, key: str, value: Component):
443-
"Adds or modifies a `Component` in the map by name"
444-
self.map[key] = value
445-
446-
def __delitem__(self, key: str):
447-
"Deletes a `Component` in the map by name"
448-
del self.map[key]
449-
450-
def __iter__(self):
451-
"Iterates `Component`s in the map by name"
452-
return iter(self.map)
453-
454-
def __len__(self):
455-
return len(self.map)
433+
class PortMap(pydantic.BaseModel):
434+
ports: Dict[str, Component] = {}
456435

457436
def _add_port(self, component: str, interface: str, port_name: str, port: Port):
458437
"Internally used by a `PackageDef`"
459-
if component not in self:
460-
self[component] = {}
461-
if interface not in self[component]:
462-
self[component][interface] = {}
463-
self[component][interface][port_name] = port
438+
if component not in self.ports:
439+
self.ports[component] = {}
440+
if interface not in self.ports[component]:
441+
self.ports[component][interface] = {}
442+
self.ports[component][interface][port_name] = port
464443

465444
def _add_ports(self, component: str, interface: str, ports: Dict[str, Port]):
466445
"Internally used by a `PackageDef`"
467-
if component not in self:
468-
self[component] = {}
469-
self[component][interface] = ports
446+
if component not in self.ports:
447+
self.ports[component] = {}
448+
self.ports[component][interface] = ports
470449

471450
def get_ports(self, component: str, interface: str) -> Dict[str, Port]:
472451
"List the ports allocated in this PortMap for the given `Component` and `Interface`"
473-
if component not in self:
452+
if component not in self.ports:
474453
raise KeyError(f"'{component}' not found in {self}")
475-
return self[component][interface]
476-
477-
@classmethod
478-
def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHandler):
479-
return core_schema.dict_schema(
480-
keys_schema=core_schema.str_schema(),
481-
values_schema=core_schema.any_schema()
482-
)
483-
454+
return self.ports[component][interface]
484455

485456

486457
class LockFile(pydantic.BaseModel):

0 commit comments

Comments
 (0)