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: module)[source]

Extract all modules from module.

List[module]

cosapp.tools.module_parser.parseModule.find_ports_and_systems(module: module, includes: Union[str, List[str]] = '*', excludes: Optional[Union[str, List[str]]] = None)Tuple[Set[Type[cosapp.systems.system.System]], Set[Type[cosapp.ports.port.Port]]][source]

Extract recursively all systems and ports from module

  • 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.

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: Union[Type[cosapp.systems.system.System], Type[cosapp.ports.port.Port]], package_name: Optional[str] = None, ports: Optional[Set[Type[cosapp.ports.port.Port]]] = None, *args, **kwargs)[source]

Get informations from a system or a port

  • 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.

dict[str, Any]

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

Extract metadata of all systems and ports found in module.

  • 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.

dict[str, Any]

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

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

  • 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).

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.

>>> 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]'),
>>>         ],
>>>     },
>>> )