From af21ff13ff963c2454e79cc2f80fc449eb4bdd76 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 13 Aug 2024 22:27:10 +0200 Subject: [PATCH] [WIP] --- platypush/config/__init__.py | 1 + platypush/entities/_engine/_procedure.py | 7 +++++ platypush/entities/procedures.py | 38 ++++++++++++++++++++++++ platypush/plugins/inspect/_serialize.py | 5 ++-- platypush/procedure/__init__.py | 3 ++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 platypush/entities/_engine/_procedure.py create mode 100644 platypush/entities/procedures.py diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index c6859e02bb..244c799af4 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -422,6 +422,7 @@ class Config: self.procedures[procedure_name] = { '_async': _async, + 'type': 'config', 'actions': component, 'args': args, } diff --git a/platypush/entities/_engine/_procedure.py b/platypush/entities/_engine/_procedure.py new file mode 100644 index 0000000000..74b566f236 --- /dev/null +++ b/platypush/entities/_engine/_procedure.py @@ -0,0 +1,7 @@ +class ProceduresManager: + """ + This class is responsible for managing the procedures as native entities. + """ + + def __init__(self): + self.procedures = {} diff --git a/platypush/entities/procedures.py b/platypush/entities/procedures.py new file mode 100644 index 0000000000..8cdebba4eb --- /dev/null +++ b/platypush/entities/procedures.py @@ -0,0 +1,38 @@ +import logging + +from sqlalchemy import ( + Column, + Enum, + ForeignKey, + Integer, + JSON, + String, +) + +from platypush.common.db import is_defined + +from . import Entity + +logger = logging.getLogger(__name__) + + +if not is_defined('procedure'): + + class Procedure(Entity): + """ + Models a procedure entity. + """ + + __tablename__ = 'procedure' + + id = Column( + Integer, ForeignKey('entity.id', ondelete='CASCADE'), primary_key=True + ) + name = Column(String, unique=True, nullable=False) + args = Column(JSON, nullable=False, default=[]) + type = Column(Enum('python', 'config', name='procedure_type'), nullable=False) + + __table_args__ = {'keep_existing': True} + __mapper_args__ = { + 'polymorphic_identity': __tablename__, + } diff --git a/platypush/plugins/inspect/_serialize.py b/platypush/plugins/inspect/_serialize.py index a9b75258b8..aa9a770316 100644 --- a/platypush/plugins/inspect/_serialize.py +++ b/platypush/plugins/inspect/_serialize.py @@ -10,9 +10,10 @@ class ProcedureEncoder(json.JSONEncoder): def default(self, o): if callable(o): return { - 'type': 'native_function', + 'type': 'python', 'module': o.__module__, - 'source': inspect.getsourcefile(o), + 'source': getattr(o, "_source", inspect.getsourcefile(o)), + 'line': getattr(o, "_line", inspect.getsourcelines(o)[1]), 'args': [ name for name, arg in inspect.signature(o).parameters.items() diff --git a/platypush/procedure/__init__.py b/platypush/procedure/__init__.py index 478b3a64b6..56cdc8f130 100644 --- a/platypush/procedure/__init__.py +++ b/platypush/procedure/__init__.py @@ -565,9 +565,12 @@ def procedure(name_or_func: Optional[str] = None, *upper_args, **upper_kwargs): """ Public decorator to mark a function as a procedure. """ + import inspect f.procedure = True f.procedure_name = name + f._source = inspect.getsourcefile(f) # pylint: disable=protected-access + f._line = inspect.getsourcelines(f)[1] # pylint: disable=protected-access @wraps(f) def _execute_procedure(*args, **kwargs):