cosapp.tools.module_parser.parseModule

Functions

extract_modules(module)

Extract all modules from module.

find_ports_and_systems(module[, includes, ...])

Extract recursively all systems and ports from module

format_kwargs(**kwargs)

get_data_from_class(dtype[, package_name, ports])

Get informations from a system or a port

get_data_from_module(module[, ctor_config, ...])

Extract metadata of all systems and ports found in module.

parse_module(module[, ctor_config, ...])

Creates a json file containing the metadata of all systems and ports found in module.

cosapp.tools.module_parser.parseModule.extract_modules(module: ModuleType)[source]

Extract all modules from module.

Returns:

List[module]

cosapp.tools.module_parser.parseModule.find_ports_and_systems(module: ModuleType, includes: str | List[str] = '*', excludes: str | List[str] = None) Tuple[Set[Type[System]], Set[Type[Port]]][source]

Extract recursively all systems and ports from module

Parameters:

  • module [ModuleType]:

    Python module to be parsed.

  • includes [str or List[str]] (optional):

    System and port names matching these patterns will be included.

  • excludes [str or List[str]] (optional):

    System and port names matching these patterns will be excluded.

Returns:

systemSet, portSet: set[type[System]], set[type[Port]]

cosapp.tools.module_parser.parseModule.format_kwargs(**kwargs)[source]
cosapp.tools.module_parser.parseModule.get_data_from_class(dtype: Type[System] | Type[Port], package_name: str | None = None, ports: Set[Type[Port]] | None = None, *args, **kwargs)[source]

Get informations from a system or a port

Parameters:

  • dtype [type[System | Port]]:

    System or Port class to be analyzed.

  • packageName [str] (optional):

    Custom package name.

  • ports [set[Port]] (optional):

    Used to check that all ports were found (in case a port is used in a children system not in the package for example).

  • *args, **kwargs:

    Additional arguments forwarded to class constructor, if required.

Returns:

dict[str, Any]

cosapp.tools.module_parser.parseModule.get_data_from_module(module: ModuleType, ctor_config: Dict[str, Dict[str, Any]] | Dict[str, List[Dict[str, Any]]] = {}, package_name: str | None = None, includes: str | List[str] = '*', excludes: str | List[str] = None) Dict[str, Any][source]

Extract metadata of all systems and ports found in module.

Parameters:

  • module [ModuleType]:

    Python module to be parsed.

  • ctor_config [dict[str, Any] | dict[str, list[dict[str, any]]]] (optional):

    Dictionary or list of dictionaries containing kwargs required for system/port construction (if any), referenced by class names (keys). If the dictionary contains key ‘__alias__’, the class will be renamed into the associated value.

  • packageName [str] (optional):

    Custom package name.

  • includes [str or List[str]] (optional):

    System and port names matching these patterns will be included.

  • excludes [str or List[str]] (optional):

    System and port names matching these patterns will be excluded.

Returns:

dict[str, Any]

cosapp.tools.module_parser.parseModule.parse_module(module: ModuleType, ctor_config: Dict[str, Dict[str, Any]] | Dict[str, List[Dict[str, Any]]] | None = None, package_name: str | None = None, includes: str | List[str] | None = None, excludes: str | List[str] | None = None, path: str | Path | None = None) None[source]

Creates a json file containing the metadata of all systems and ports found in module.

Parameters:

  • module [ModuleType]:

    Python module to be parsed.

  • ctor_config [dict[str, list[dict[str, any]] | dict[str, Any]]] (optional):

    Dictionary or list of dictionaries containing kwargs required for system/port construction (if any), referenced by class names (keys). If the dictionary contains key ‘__alias__’, the class will be renamed into the associated value.

  • package_name [str] (optional):

    Custom package name.

  • includes [str or List[str]] (optional):

    System and port names matching these patterns will be included.

  • excludes [str or List[str]] (optional):

    System and port names matching these patterns will be excluded (ports used by included systems will always be included).

  • path [str | pathlib.Path] (optional):

    Optional path of output file <packageName or module.__name__>.json (current directory by default).

Use pre-defined settings

Pre-defined values of optional arguments ctor_config, package_name, includes and excludes may be specified at module level by implementing hook function _parse_module_config, returning preset values in a dictionary of the kind {option: value}. Typically, _parse_module_config may be defined in the __init__.py file of the module.

Examples:

>>> parse_module(module1)
>>>
>>> parse_module(
>>>     module2,
>>>     ctor_config = {
>>>         'SystemA': dict(n=2, x=0.5),
>>>         'SystemB': [
>>>             dict(foo=0),
>>>             dict(foo=None, __alias__='SystemB [default]'),
>>>         ],
>>>     },
>>> )