podhmo / metashape

0 stars 1 forks source link

customizable string expression for typeinfo #100

Open podhmo opened 3 years ago

podhmo commented 3 years ago

something like this.

from __future__ import annotations
import typing as t
from handofcats import as_command
from magicalimport import import_module
from metashape.runtime import get_walker
from metashape import typeinfo   # typeinfo.to_string is needed function

def _to_str_typeinfo(info: typeinfo.TypeInfo) -> str:
    name = info.type_.__name__
    if not info.user_defined_type:
        return name
    return f"[{name}](#{name})"  # markdown with link

@as_command
def run(name: str):
    m = import_module(name)
    w = get_walker(m, aggressive=True, recursive=False)

    for cls in w.walk():
        print(f"## {cls.__name__}")
        print("")
        print("| name | type | description |")
        print("| :--- | :--- | :--- |")
        for name, info, metadata in w.get_type_walker(cls).walk():
            print(
                f"| {name} | {typeinfo.to_string(info, to_str=_to_str_typeinfo)} | {metadata.get('description') or ''}|"
            )