# Path: lib/python/asterisk/pluggable_registry.py
# PLUGGABLE_EVENT_REGISTRY = PluggableRegistry()
# 
# PLUGGABLE_ACTION_REGISTRY = PluggableRegistry()
# 
# def var_replace(text, values):
#     """ perform variable replacement on text
# 
#     This allows a parameters to be written to include variables from the
#     arbitrarily structured object provided by an ARI or AMI event like so:
#     from ARI to ARI: Uri: 'playbacks/{playback.id}/control'
#     from AMI to AMI: Channel: '{channel}'
#     from AMI to ARI: Uri: 'channels/{uniqueid}/play'
#     from ARI to AMI: Channel: '{channel.name}'
# 
#     :param text: text with optional {var} entries
#     :param values: nested dict of values to get replacement values from
#     """
#     if not isinstance(text, str):
#         return text
# 
#     for match in re.findall(r'(?<!\\){[^}]*(?<!\\)}', text):
#         value = values
#         for var in match[1:-1].split('.'):
#             if not var in value:
#                 LOGGER.error('Unable to replace variables in %s from %s',
#                              text, values)
#                 return None
#             value = value[var]
#         text = text.replace(match, value)
# 
#     return text


import sys
import os
import time
import shutil
import logging
import fileinput
import polyfill
from . import test_suite_utils
from .config import ConfigFile
from twisted.internet import reactor, protocol, defer, utils, error
from twisted.python.failure import Failure
    from twisted.python.filepath import FilePath
    from twisted.internet.endpoints import UNIXClientEndpoint
    from twisted.conch.ssh.keys import Key
    from twisted.conch.client.knownhosts import KnownHostsFile
    from twisted.conch.endpoints import SSHCommandClientEndpoint
    from twisted.internet.error import ProcessTerminated
from .pluggable_registry import PLUGGABLE_EVENT_REGISTRY,\
    PLUGGABLE_ACTION_REGISTRY, var_replace

class CLIPluggableActionModule(object):
    """Pluggable CLI action module.

    Options:
        id - The Asterisk id to execute the command on. This is optional and,
             if not specified defaults to 0 (ast1).
        cmd - The CLI command to execute
    """

    def __init__(self, test_object, config):
        """Setup the CLI event observer"""

        self.test_object = test_object

        if not isinstance(config, list):
            config = [config]

        self.cmds = config

    def run(self, triggered_by, source, extra):
        """Callback called when this action is triggered."""

        for cmd in self.cmds:
            text = cmd.get('cmd')
            if text:
                instance = cmd.get('id', 0)
                self.test_object.ast[instance].cli_exec(text)



# Path: lib/python/asterisk/pluggable_registry.py
# PLUGGABLE_EVENT_REGISTRY = PluggableRegistry()
# 
# PLUGGABLE_ACTION_REGISTRY = PluggableRegistry()
# 
# def var_replace(text, values):
#     """ perform variable replacement on text
# 
#     This allows a parameters to be written to include variables from the
#     arbitrarily structured object provided by an ARI or AMI event like so:
#     from ARI to ARI: Uri: 'playbacks/{playback.id}/control'
#     from AMI to AMI: Channel: '{channel}'
#     from AMI to ARI: Uri: 'channels/{uniqueid}/play'
#     from ARI to AMI: Channel: '{channel.name}'
# 
#     :param text: text with optional {var} entries
#     :param values: nested dict of values to get replacement values from
#     """
#     if not isinstance(text, str):
#         return text
# 
#     for match in re.findall(r'(?<!\\){[^}]*(?<!\\)}', text):
#         value = values
#         for var in match[1:-1].split('.'):
#             if not var in value:
#                 LOGGER.error('Unable to replace variables in %s from %s',
#                              text, values)
#                 return None
#             value = value[var]
#         text = text.replace(match, value)
# 
#     return text


# Path: lib/python/asterisk/asterisk.py
import sys
import os
import time
import shutil
import logging
import fileinput
import polyfill
from . import test_suite_utils
from .config import ConfigFile
from twisted.internet import reactor, protocol, defer, utils, error
from twisted.python.failure import Failure
    from twisted.python.filepath import FilePath
    from twisted.internet.endpoints import UNIXClientEndpoint
    from twisted.conch.ssh.keys import Key
    from twisted.conch.client.knownhosts import KnownHostsFile
    from twisted.conch.endpoints import SSHCommandClientEndpoint
    from twisted.internet.error import ProcessTerminated
from .pluggable_registry import PLUGGABLE_EVENT_REGISTRY,\
    PLUGGABLE_ACTION_REGISTRY, var_replace


class CLIPluggableActionModule(object):
    """Pluggable CLI action module.

    Options:
        id - The Asterisk id to execute the command on. This is optional and,
             if not specified defaults to 0 (ast1).
        cmd - The CLI command to execute
    """

    def __init__(self, test_object, config):
        """Setup the CLI event observer"""

        self.test_object = test_object

        if not isinstance(config, list):
            config = [config]

        self.cmds = config

    def run(self, triggered_by, source, extra):
        """Callback called when this action is triggered."""

        for cmd in self.cmds:
            text = cmd.get('cmd')
            if text:
                instance = cmd.get('id', 0)
                self.test_object.ast[instance].cli_exec(text)



PLUGGABLE_ACTION_REGISTRY.register("cli-cmds", CLIPluggableActionModule)