-
Notifications
You must be signed in to change notification settings - Fork 187
/
get_factories.py
43 lines (33 loc) · 1.3 KB
/
get_factories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
from inspect import getmembers, signature
from gdsfactory.config import logger
from gdsfactory.typings import Component, ComponentFactory
def get_cells(modules, verbose: bool = False) -> dict[str, ComponentFactory]:
"""Returns PCells (component functions) from a module or list of modules.
Args:
modules: module or iterable of modules.
verbose: prints in case any errors occur.
"""
modules = modules if isinstance(modules, Iterable) else [modules]
cells = {}
for module in modules:
for t in getmembers(module):
if callable(t[1]) and t[0] != "partial":
try:
r = signature(
t[1] if not isinstance(t[1], partial) else t[1].func
).return_annotation
if r == Component or (
isinstance(r, str) and r.endswith("Component")
):
cells[t[0]] = t[1]
except ValueError as e:
if verbose:
logger.warn(f"error in {t[0]}: {e}")
return cells
if __name__ == "__main__":
import ubcpdk
f = get_cells(ubcpdk.components)
print(f.keys())