This commit is contained in:
Fabio Manganiello 2024-08-13 22:27:10 +02:00
parent d155094729
commit af21ff13ff
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
5 changed files with 52 additions and 2 deletions

View file

@ -422,6 +422,7 @@ class Config:
self.procedures[procedure_name] = {
'_async': _async,
'type': 'config',
'actions': component,
'args': args,
}

View file

@ -0,0 +1,7 @@
class ProceduresManager:
"""
This class is responsible for managing the procedures as native entities.
"""
def __init__(self):
self.procedures = {}

View file

@ -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__,
}

View file

@ -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()

View file

@ -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):