示例#1
0
class PluginLoaderTest(unittest.TestCase):
    def setUp(self):
        self.plugin_loader = PluginLoader()

    def test_load_namespace_class(self):
        package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture')
        self.plugin_loader.load_plugin(package_path)
        loaded_class = self.plugin_loader.get_class('plugin_fixture.FakeTask')
        self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
        self.plugin_loader.remove_path(package_path)

    def test_load_zip(self):
        package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip')
        self.plugin_loader.load_plugin(package_path)
        loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask')
        self.assertEqual(loaded_class({}, {}).work(), 'FakeTaskZip')
        self.plugin_loader.remove_path(package_path)

    def copy_plugin(self):
        package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture')
        dest_path = os.path.join(PLUGIN_PATH, 'org_repo', 'plugin_fixture_tests')
        shutil.copytree(package_path, os.path.join(dest_path))
        with open(os.path.join(os.path.dirname(dest_path), '.sha'), 'w') as file:
            file.write('testsha')
        return dest_path

    def test_load_github_already_downloaded(self):
        dest_path = self.copy_plugin()
        self.plugin_loader.load_plugin('org/repo#testsha')
        loaded_class = self.plugin_loader.get_class('plugin_fixture_tests.FakeTask')
        self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
        self.plugin_loader.remove_path(dest_path)
        shutil.rmtree(os.path.dirname(dest_path))

    def copy_zip(self):
        zip_name = 'test-pgo-plugin-2d54eddde33061be9b329efae0cfb9bd58842655.zip'
        fixture_zip = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', zip_name)
        zip_dest = os.path.join(PLUGIN_PATH, 'org_test-pgo-plugin_2d54eddde33061be9b329efae0cfb9bd58842655.zip')
        shutil.copyfile(fixture_zip, zip_dest)

    @mock.patch.object(GithubPlugin, 'download', copy_zip)
    def test_load_github_not_downloaded(self):
        self.plugin_loader.load_plugin('org/test-pgo-plugin#2d54eddde33061be9b329efae0cfb9bd58842655')
        loaded_class = self.plugin_loader.get_class('test-pgo-plugin.PrintText')
        self.assertEqual(loaded_class({}, {}).work(), 'PrintText')
        dest_path = os.path.join(PLUGIN_PATH, 'org_test-pgo-plugin')
        self.plugin_loader.remove_path(os.path.join(dest_path, 'test-pgo-plugin'))
        shutil.rmtree(dest_path)
class PluginLoaderTest(unittest.TestCase):
    def setUp(self):
        self.plugin_loader = PluginLoader()

    def test_load_namespace_class(self):
        package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture')
        self.plugin_loader.load_plugin(package_path)
        loaded_class = self.plugin_loader.get_class('plugin_fixture.FakeTask')
        self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
        self.plugin_loader.remove_path(package_path)

    def test_load_zip(self):
        package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip')
        self.plugin_loader.load_plugin(package_path)
        loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask')
        self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
        self.plugin_loader.remove_path(package_path)
class TreeConfigBuilder(object):
    def __init__(self, bot, tasks_raw):
        self.bot = bot
        self.tasks_raw = tasks_raw
        self.plugin_loader = PluginLoader()

    def _get_worker_by_name(self, name):
        try:
            worker = getattr(cell_workers, name)
        except AttributeError:
            raise ConfigException('No worker named {} defined'.format(name))

        return worker

    def _is_plugin_task(self, name):
        return '.' in name

    def build(self):
        workers = []

        for task in self.tasks_raw:
            task_type = task.get('type', None)
            if task_type is None:
                raise ConfigException('No type found for given task {}'.format(task))
            elif task_type == 'EvolveAll':
                raise ConfigException('The EvolveAll task has been renamed to EvolvePokemon')

            task_config = task.get('config', {})

            if self._is_plugin_task(task_type):
                worker = self.plugin_loader.get_class(task_type)
            else:
                worker = self._get_worker_by_name(task_type)

            error_string = ''
            if BaseTask.TASK_API_VERSION < worker.SUPPORTED_TASK_API_VERSION:
                error_string = 'Do you need to update the bot?'

            elif BaseTask.TASK_API_VERSION > worker.SUPPORTED_TASK_API_VERSION:
                error_string = 'Is there a new version of this task?'

            if error_string != '':
                raise MismatchTaskApiVersion(
                    'Task {} only works with task api version {}, you are currently running version {}. {}'
                    .format(
                        task_type,
                        worker.SUPPORTED_TASK_API_VERSION,
                        BaseTask.TASK_API_VERSION,
                        error_string
                    )
                )

            instance = worker(self.bot, task_config)
            if instance.enabled:
                workers.append(instance)

        return workers
class TreeConfigBuilder(object):
    def __init__(self, bot, tasks_raw):
        self.bot = bot
        self.tasks_raw = tasks_raw
        self.plugin_loader = PluginLoader()

    def _get_worker_by_name(self, name):
        try:
            worker = getattr(cell_workers, name)
        except AttributeError:
            raise ConfigException('No worker named {} defined'.format(name))

        return worker

    def _is_plugin_task(self, name):
        return '.' in name

    def build(self):
        workers = []

        for task in self.tasks_raw:
            task_type = task.get('type', None)
            if task_type is None:
                raise ConfigException(
                    'No type found for given task {}'.format(task))
            elif task_type == 'EvolveAll':
                raise ConfigException(
                    'The EvolveAll task has been renamed to EvolvePokemon')

            task_config = task.get('config', {})

            if self._is_plugin_task(task_type):
                worker = self.plugin_loader.get_class(task_type)
            else:
                worker = self._get_worker_by_name(task_type)

            error_string = ''
            if BaseTask.TASK_API_VERSION < worker.SUPPORTED_TASK_API_VERSION:
                error_string = 'Do you need to update the bot?'

            elif BaseTask.TASK_API_VERSION > worker.SUPPORTED_TASK_API_VERSION:
                error_string = 'Is there a new version of this task?'

            if error_string != '':
                raise MismatchTaskApiVersion(
                    'Task {} only works with task api version {}, you are currently running version {}. {}'
                    .format(task_type, worker.SUPPORTED_TASK_API_VERSION,
                            BaseTask.TASK_API_VERSION, error_string))

            instance = worker(self.bot, task_config)
            workers.append(instance)

        return workers
class TreeConfigBuilder(object):
    def __init__(self, bot, tasks_raw):
        self.bot = bot
        self.tasks_raw = tasks_raw
        self.plugin_loader = PluginLoader()

    def _get_worker_by_name(self, name):
        try:
            worker = getattr(cell_workers, name)
        except AttributeError:
            raise ConfigException('No worker named {} defined'.format(name))

        return worker

    def _is_plugin_task(self, name):
        return '.' in name

    def build(self):
        workers = []
        deprecated_pokemon_task = False

        for task in self.tasks_raw:
            task_type = task.get('type', None)
            if task_type is None:
                raise ConfigException('No type found for given task {}'.format(task))
            elif task_type == 'EvolveAll':
                raise ConfigException('The EvolveAll task has been renamed to EvolvePokemon')

            task_config = task.get('config', {})

            if task_type in ['CatchVisiblePokemon', 'CatchLuredPokemon']:
                if deprecated_pokemon_task:
                    continue
                else:
                    deprecated_pokemon_task = True
                    task_type = 'CatchPokemon'
                    task_config = {}
                    self.bot.logger.warning('The CatchVisiblePokemon & CatchLuredPokemon tasks have been replaced with '
                                            'CatchPokemon.  CatchPokemon has been enabled with default settings.')

            if task_type == 'SleepSchedule':
                self.bot.logger.warning('The SleepSchedule task was moved out of the task section. '
                                        'See config.json.*example for more information.')
                continue

            if self._is_plugin_task(task_type):
                worker = self.plugin_loader.get_class(task_type)
            else:
                worker = self._get_worker_by_name(task_type)

            error_string = ''
            if BaseTask.TASK_API_VERSION < worker.SUPPORTED_TASK_API_VERSION:
                error_string = 'Do you need to update the bot?'

            elif BaseTask.TASK_API_VERSION > worker.SUPPORTED_TASK_API_VERSION:
                error_string = 'Is there a new version of this task?'

            if error_string != '':
                raise MismatchTaskApiVersion(
                    'Task {} only works with task api version {}, you are currently running version {}. {}'
                    .format(
                        task_type,
                        worker.SUPPORTED_TASK_API_VERSION,
                        BaseTask.TASK_API_VERSION,
                        error_string
                    )
                )

            instance = worker(self.bot, task_config)
            if instance.enabled:
                workers.append(instance)

        return workers