Skip to content

Commit

Permalink
Merge pull request #187 from gdsfactory/add_get_cells
Browse files Browse the repository at this point in the history
get_cells
  • Loading branch information
sebastian-goeldi authored Oct 4, 2023
2 parents 7a4b0ba + 3925bf6 commit 9499421
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions changelog.d/+963ab5a0.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added back kf.kcell.get_cells in order to automatically scrape a module for KCell factories [PR](https://github.com/gdsfactory/kfactory/pull/187)
43 changes: 22 additions & 21 deletions src/kfactory/kcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from hashlib import sha3_512
from pathlib import Path
from tempfile import gettempdir
from types import ModuleType
from typing import Any, Literal, TypeAlias, TypeVar, overload

import cachetools.func
Expand Down Expand Up @@ -334,27 +335,27 @@ def port_check(p1: Port, p2: Port, checks: PortCheck = PortCheck.all_opposite) -
assert p1.port_type == p2.port_type, f"Port type mismatch for {p1=} {p2=}"


# def get_cells(
# modules: Iterable[ModuleType], verbose: bool = False
# ) -> dict[str, KCellFactory]:
# """Returns KCells (KCell functions) from a module or list of modules.

# Args:
# modules: module or iterable of modules.
# verbose: prints in case any errors occur.
# """
# cells = {}
# for module in modules:
# for t in inspect.getmembers(module):
# if callable(t[1]) and t[0] != "partial":
# try:
# r = inspect.signature(t[1]).return_annotation
# if r == KCell or (isinstance(r, str) and r.endswith("KCell")):
# cells[t[0]] = KCellFactory(name=t[0], factory=t[1])
# except ValueError:
# if verbose:
# print(f"error in {t[0]}")
# return cells
def get_cells(
modules: Iterable[ModuleType], verbose: bool = False
) -> dict[str, Callable[..., KCell]]:
"""Returns KCells (KCell functions) from a module or list of modules.
Args:
modules: module or iterable of modules.
verbose: prints in case any errors occur.
"""
cells = {}
for module in modules:
for t in inspect.getmembers(module):
if callable(t[1]) and t[0] != "partial":
try:
r = inspect.signature(t[1]).return_annotation
if r == KCell or (isinstance(r, str) and r.endswith("KCell")):
cells[t[0]] = t[1]
except ValueError:
if verbose:
print(f"error in {t[0]}")
return cells


class LayerEnclosureModel(BaseModel):
Expand Down

0 comments on commit 9499421

Please sign in to comment.