1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2024-11-24 08:12:13 +02:00

add decorator for creating commands

This commit is contained in:
MarkParker5 2021-10-18 00:26:45 +03:00
parent deb828324f
commit a5c488f1a5
3 changed files with 15 additions and 7 deletions

2
.gitignore vendored
View File

@ -12,3 +12,5 @@ __pycache__
/config.py
/tts-gc-key.json
/logs.txt
audio/

View File

@ -225,13 +225,21 @@ class Command(ABC):
'params': {'string':string,},
}
@staticmethod
@staticmethod #TODO: pass Response instead of answer and voice; create ThreadData struct
def background(answer = '', voice = ''): # make background cmd
def decorator(cmd): #wrapper of wrapper (decorator of decorator)
def decorator(func): #wrapper of wrapper (decorator of decorator)
def wrapper(text):
finish_event = Event()
thread = RThread(target=cmd, args=(text, finish_event))
thread = RThread(target=func, args=(text, finish_event))
thread.start()
return Response(voice = voice, text = answer, thread = {'thread': thread, 'finish_event': finish_event} )
return wrapper
return decorator
@classmethod
def new(cls, *args, **kwargs):
def creator(func):
cmd: Command = cls(*args, **kwargs)
cmd.setStart(func)
return func
return creator

View File

@ -1,10 +1,8 @@
from .SmallTalk import *
from ..Command import Response
################################################################################
@SmallTalk.new('Hello', patterns = ['* привет* *',])
def method(params):
voice = text = 'Привет'
return Response(text = text, voice = voice)
patterns = ['* привет* *',]
hello = SmallTalk('Hello', {}, patterns)
hello.setStart(method)