import logging

from idiotic import block
from idiotic import resource
from idiotic import config
import subprocess
import asyncio

import types


class Speech(block.Block):
    def __init__(self, name, text=None, parameters=None, defaults=None, command=None):
        self.name = name

        self.speech_command = command or config.config.get("modules", {}).get("espeak", {}).get("command", "espeak")

        self._text = text
        self.parameters = parameters or []

        self.data = {}
        self.defaults = defaults or {}

        self._param_dict = {n: self.defaults.get(n, None) for n in self.parameters}

        for name in self.parameters:
            async def setparam(self, val):
                await self._setparam(name, val)

            setattr(self, name, types.MethodType(setparam, self))

        self.inputs = {}
        self.resources = []

    async def _setparam(self, name, value):
        self._param_dict[name] = value
        await self.speak()

    async def text(self, text):
        self._text = text
        await self.speak()

    def _speak(self):
        try:
            logging.debug("Saying \"{}\"".format(self._text.format(**self._param_dict)))
            subprocess.run("espeak --stdout | paplay", input=self._text.format(**self._param_dict).encode('UTF-8'), shell=True)
        except subprocess.CalledProcessError as e:
            logging.error("While trying espeak...")

    async def speak(self, *_):
        await asyncio.get_event_loop().run_in_executor(None, self._speak)

@@ -628,16 +628,100 @@
     
-for name
+self.inputs = %7B%7D%0A        self.resources = %5B%5D%0A%0A    def __getattr__(self, key):%0A        if key
  in 
@@ -725,26 +725,27 @@
 in self.
+_
 param
-eters
+_dict
 :%0A      
@@ -760,31 +760,24 @@
 ync def 
-setparam(self, 
+__input(
 val):%0A  
@@ -815,20 +815,18 @@
 ram(
-name
+key
 , val)%0A
-%0A
     
@@ -837,62 +837,22 @@
     
-setattr(self, name, types.MethodType(setparam, self))%0A
+return __input
 %0A   
@@ -860,52 +860,73 @@
     
-s
 el
-f.inputs = %7B%7D%0A        self.resources = %5B%5D
+se:%0A            raise ValueError(%22Parameter name not declared%22)
 %0A%0A  

Fix espeak module parameters