Skip to content

Commit

Permalink
Adding plugin support (#2679)
Browse files Browse the repository at this point in the history
* Adding plugin support

* Adding an empty __init__.py
  • Loading branch information
TheSavior committed Aug 7, 2016
1 parent 44a6602 commit 85f7607
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

from pokemongo_bot import PokemonGoBot, TreeConfigBuilder
from pokemongo_bot.health_record import BotEvent
from pokemongo_bot.plugin_loader import PluginLoader

if sys.version_info >= (2, 7, 9):
ssl._create_default_https_context = ssl._create_unverified_context
Expand Down Expand Up @@ -384,6 +385,7 @@ def init_config():
config.release = load.get('release', {})
config.action_wait_max = load.get('action_wait_max', 4)
config.action_wait_min = load.get('action_wait_min', 1)
config.plugins = load.get('plugins', [])
config.raw_tasks = load.get('tasks', [])

config.vips = load.get('vips', {})
Expand Down Expand Up @@ -439,6 +441,10 @@ def task_configuration_error(flag_name):
parser.error("--catch_randomize_spin_factor is out of range! (should be 0 <= catch_randomize_spin_factor <= 1)")
return None

plugin_loader = PluginLoader()
for plugin in config.plugins:
plugin_loader.load_path(plugin)

# create web dir if not exists
try:
os.makedirs(web_dir)
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pgoapi.utilities import f2i, get_cell_ids

import cell_workers
from plugin_loader import PluginLoader
from api_wrapper import ApiWrapper
from cell_workers.utils import distance
from event_manager import EventManager
Expand Down
18 changes: 18 additions & 0 deletions pokemongo_bot/plugin_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import sys
import importlib

class PluginLoader(object):
folder_cache = []

def load_path(self, path):
parent_dir = os.path.dirname(path)
if parent_dir not in self.folder_cache:
self.folder_cache.append(parent_dir)
sys.path.append(parent_dir)

def get_class(self, namespace_class):
[namespace, class_name] = namespace_class.split('.')
my_module = importlib.import_module(namespace)
return getattr(my_module, class_name)

20 changes: 20 additions & 0 deletions pokemongo_bot/test/plugin_loader_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import imp
import sys
import pkgutil
import importlib
import unittest
import os
from datetime import timedelta, datetime
from mock import patch, MagicMock
from pokemongo_bot.plugin_loader import PluginLoader
from pokemongo_bot.test.resources.plugin_fixture import FakeTask

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_path(package_path)
loaded_class = self.plugin_loader.get_class('plugin_fixture.FakeTask')
self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
Empty file.
1 change: 1 addition & 0 deletions pokemongo_bot/test/resources/plugin_fixture/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from fake_task import FakeTask
5 changes: 5 additions & 0 deletions pokemongo_bot/test/resources/plugin_fixture/fake_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pokemongo_bot.cell_workers import BaseTask

class FakeTask(BaseTask):
def work(self):
return 'FakeTask'
11 changes: 10 additions & 1 deletion pokemongo_bot/tree_config_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cell_workers
from pokemongo_bot.plugin_loader import PluginLoader

class ConfigException(Exception):
pass
Expand All @@ -7,6 +8,7 @@ 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:
Expand All @@ -16,6 +18,9 @@ def _get_worker_by_name(self, name):

return worker

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

def build(self):
workers = []

Expand All @@ -28,7 +33,11 @@ def build(self):

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

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

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

Expand Down
18 changes: 17 additions & 1 deletion tests/tree_config_builder_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest
import json
from pokemongo_bot import PokemonGoBot, ConfigException, TreeConfigBuilder
import os
from pokemongo_bot import PokemonGoBot, ConfigException, TreeConfigBuilder, PluginLoader
from pokemongo_bot.cell_workers import HandleSoftBan, CatchLuredPokemon
from pokemongo_bot.test.resources.plugin_fixture import FakeTask

def convert_from_json(str):
return json.loads(str)
Expand Down Expand Up @@ -83,3 +85,17 @@ def test_task_with_config(self):
builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()
self.assertTrue(tree[0].config.get('longer_eggs_first', False))

def test_load_plugin_task(self):
package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture')
plugin_loader = PluginLoader()
plugin_loader.load_path(package_path)

obj = convert_from_json("""[{
"type": "plugin_fixture.FakeTask"
}]""")

builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()
result = tree[0].work()
self.assertEqual(result, 'FakeTask')

0 comments on commit 85f7607

Please sign in to comment.