Skip to content
Snippets Groups Projects
Commit 92b34948 authored by Cyril Moineau's avatar Cyril Moineau
Browse files

Add basic export registry system.

parent 9885ceaf
No related branches found
No related tags found
3 merge requests!279v0.4.0,!253v0.4.0,!163Export refactor
from typing import Dict, Tuple, Set
import aidge_core
from aidge_core.export_utils import ExportNode
from enum import Enum
# Language
LANGUAGE = Enum('LANGUAGE', ['Cpp/C'])
# Define new type registry_key
registry_key = Tuple[str, aidge_core.dtype, aidge_core.dformat]
# TODO: very naive implementation !
# error handling should be added !
class ExportLib(): # Should be abstract ?
"""Aidge export lib, define a registry
"""
# Lib name usefull ?
_name:str = None
# Registry of exportNode
_export_node_registry:Dict[registry_key, ExportNode] = {}
# The language type usefull ?
_language: LANGUAGE = None
def __init__(self) -> None:
raise RuntimeError("ExportLib should not be instanciated")
@classmethod
def exportable(cls, node:aidge_core.Node)->bool:
"""
:param node: aidge_node to try to export
:type node: aidge_core.Node
:return: True if this node is exportable with this lib, else false
:rtype: bool
"""
# TODO: should return usable error that can be catch to know if only some keys have not been respected !
key: registry_key = (node.type(), node.dtype(), node.dformat())
return key in cls._export_node_registry
@classmethod
def supported_operators(cls)->Set[str]:
"""
:return: list of supported operator by this ExportLib
:rtype: List[str]
"""
operators = set()
for key in cls._export_node_registry.keys():
operators.add(key[0])
return operators
@classmethod
def get_export_node(cls, node:aidge_core.Node)->ExportNode:
"""
:param node: Node to transform
:type node: aidge_core.Node
:return: Corresponding export node.
:rtype: ExportNode
"""
if not cls.exportable(node):
raise ValueError("Node is not exportable ...")
key: registry_key = (node.type(), node.dtype(), node.dformat())
return key in cls._export_node_registry
@classmethod
def add_export_node(cls, key:registry_key, eNode:ExportNode)->None:
cls._export_node_registry[key] = eNode
def operator_register(lib: ExportLib, key:registry_key, *args):
"""Helper decorator to register an :py:class:`ExportNode` to an :py:class:`ExportLib`
"""
def decorator(operator):
def wrapper(*args, **kwargs):
return operator(*args, **kwargs)
lib.add_export_node(key, operator)
return wrapper
return decorator
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment