class BooleanParameter(Parameter):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def default_value(self):
        raise NotImplementedError class CommandBuilder(object):
    def __init__(self, command_factory, inferior_repository, platform_factory,
                 inferior_factory, thread_factory, config, terminal):
        self.commands = {}
        for Cmd in _command_list:
            cmd = command_factory.create(Cmd, inferior_repository,
                                         platform_factory, inferior_factory,
                                         thread_factory, config, terminal)
            self.commands[Cmd.name()] = cmd class Configuration(object):
    def __init__(self):
        self._root = ConfigurationNode()

    def _get_parent(self, path):
        node = self._root
        for tag in path.split(' ')[:-1]:
            node = node.get(tag)
        return node

    def register_parameter(self, parameter):
        parent = self._get_parent(parameter.name())
        parent.append(parameter)

    def parameter(self, path):
        node = self._root
        for tag in path.split(' '):
            if tag not in node:
                raise KeyError('Parameter %s not found!' % path)
            node = node.get(tag)
        return node

    def __repr__(self):
        return repr(self._root) class DataCommand(Command):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def execute(self, terminal, thread, argument):
        raise NotImplementedError class EnumParameter(Parameter):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def sequence(self):
        raise NotImplementedError class Parameter(object):
    __metaclass__ = abc.ABCMeta

    def init(self):
        pass

    @staticmethod
    def name():
        raise NotImplementedError

    @abc.abstractmethod
    def default_value(self):
        raise NotImplementedError class ParameterBuilder(object):
    def __init__(self, factory, config):
        for Param in _parameter_list:
            param = factory.create(Param)
            param.init()
            config.register_parameter(param) class PrefixCommand(Command):
    def execute(self, terminal):
        terminal.write('%(face-error)sAttempting to invoke an '
                       'incomplete command!\n') def register_command(cls):
    _command_list.append(cls)
    return cls def register_parameter(cls):
    _parameter_list.append(cls)
    return cls class CpuFactory(object):
    __metaclass__ = abc.ABCMeta

    def create_cpu(self, architecture):
        assert architecture in _cpu_map
        return _cpu_map.get(architecture,
                            None)(self)

    @abc.abstractmethod
    def create_register(self, cpu, register):
        raise NotImplementedError class InferiorRepository(object):
    def __init__(self):
        self._inferiors = {}

    def add_inferior(self, inferior):
        self._inferiors[inferior.id()] = inferior

    def has_inferior(self, inferior_id):
        return inferior_id in self._inferiors

    def get_inferior(self, inferior_id):
        assert self.has_inferior(inferior_id), ('inferior %r not found' %
                                                inferior_id)
        return self._inferiors[inferior_id] class TestCommandFactory(CommandFactory, object):
    def create(self, command_type, inferior_repository, platform_factory,
               inferior_factory, thread_factory, config, terminal):
        if issubclass(command_type, DataCommand):
            class TestDataCommand(command_type):
                __doc__ = command_type.__doc__

                def __init__(self):
                    command_type.__init__(self)

                def invoke(self, argument, _):
                    inferior = TestInferior()
                    thread = TestThread(inferior.id(), 0)
                    command_type.execute(self, terminal, thread, argument)

            return TestDataCommand()

        if issubclass(command_type, Command):
            class TestCommand(command_type):
                __doc__ = command_type.__doc__

                def __init__(self):
                    command_type.__init__(self)

                def invoke(self, argument, _):
                    command_type.execute(self, terminal, argument)

            return TestCommand()

        else:
            raise TypeError('Command type %s unknown!' %
                            str(command_type)) class TestInferiorFactory(InferiorFactory):
    def __init__(self):
        super(TestInferiorFactory, self).__init__(TestCpuFactory())

    def create_inferior(self, cpu, inferior_id):
        return TestInferior(cpu, inferior_id)

    def create_thread(self, inferior, thread_id):
        thread = TestThread(inferior.id(), thread_id)
        inferior.add_thread(thread)
        return thread class TestParameterFactory(ParameterFactory, object):
    def create(self, parameter_type):
        if issubclass(parameter_type, EnumParameter):
            class TestParameterEnum(parameter_type):
                def __init__(self):
                    parameter_type.__init__(self)
                    self.value = parameter_type.default_value(self)

            return TestParameterEnum()

        elif issubclass(parameter_type, BooleanParameter):
            class TestParameterBoolean(parameter_type):
                def __init__(self):
                    parameter_type.__init__(self)
                    self.value = parameter_type.default_value(self)

            return TestParameterBoolean()

        elif issubclass(parameter_type, Parameter):
            class TestParameter(parameter_type):
                def __init__(self):
                    parameter_type.__init__(self)

            return TestParameter()

        else:
            raise TypeError('Parameter type %s unknown!' %
                            str(type(parameter_type))) class TestPlatformFactory(PlatformFactory, object):
    def __init__(self):
        self._registers = None
        self.reset()

    def reset(self):
        self._registers = {}

    def create_context(self, config, inferior, thread):
        class TestContext(Context):
            def __init__(self):
                sp = inferior.cpu().stack_pointer()
                super(TestContext, self).__init__(sp)

                for group, register_dict in inferior.cpu().registers():
                    register_dict = dict((x.name(), create_static_register(x))
                                         for x in register_dict.itervalues())
                    self._registers[group] = register_dict

        return TestContext() class TestThreadFactory(ThreadFactory):
    def create_thread(self, inferior, thread_id):
        thread = TestThread(inferior, thread_id)
        inferior.add_thread(thread)
        return thread
from flowui.terminal import AnsiTerminal
from flowui.terminals import SysTerminal
from flowui.themes import Solarized
from unittest import TestCase
from voidwalker.framework.interface import BooleanParameter
from voidwalker.framework.interface import CommandBuilder
from voidwalker.framework.interface import Configuration
from voidwalker.framework.interface import DataCommand
from voidwalker.framework.interface import EnumParameter
from voidwalker.framework.interface import Parameter
from voidwalker.framework.interface import ParameterBuilder
from voidwalker.framework.interface import PrefixCommand
from voidwalker.framework.interface import register_command
from voidwalker.framework.interface import register_parameter
from voidwalker.framework.platform import CpuFactory
from voidwalker.framework.target import InferiorRepository
from voidwalker.backends.test import TestCommandFactory
from voidwalker.backends.test import TestInferiorFactory
from voidwalker.backends.test import TestParameterFactory
from voidwalker.backends.test import TestPlatformFactory
from voidwalker.backends.test import TestThreadFactory
# (void)walker unit tests
# Copyright (C) 2012 David Holm <dholmster@gmail.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.





@register_command
class TestCommand(PrefixCommand):
    @staticmethod
    def name():
        return 'test'


@register_command
class TestDataCommand(DataCommand):
    @staticmethod
    def name():
        return '%s %s' % (TestCommand.name(), 'data')

    def execute(self, terminal, thread, argument):
        pass


class CommandTest(TestCase):
    def setUp(self):
        self._terminal = AnsiTerminal(SysTerminal(), Solarized())

    def test_command(self):
        inferior_factory = TestInferiorFactory()

        thread_factory = TestThreadFactory()