Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing an error if tasks don't work for the given api #2732

Merged
merged 1 commit into from
Aug 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from pokemongo_bot.websocket_remote_control import WebsocketRemoteControl
from worker_result import WorkerResult
from tree_config_builder import ConfigException, TreeConfigBuilder
from tree_config_builder import ConfigException, MismatchTaskApiVersion, TreeConfigBuilder


class PokemonGoBot(object):
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class BaseTask(object):
TASK_API_VERSION = 1

def __init__(self, bot, config):
self.bot = bot
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/catch_lured_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@


class CatchLuredPokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def work(self):
lured_pokemon = self.get_lured_pokemon()
if lured_pokemon:
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/catch_visible_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@


class CatchVisiblePokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def work(self):
if 'catchable_pokemons' in self.bot.cell and len(self.bot.cell['catchable_pokemons']) > 0:
# Sort all by distance from current pos- eventually this should
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/collect_level_up_reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


class CollectLevelUpReward(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

current_level = 0
previous_level = 0

Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/evolve_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class EvolvePokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.api = self.bot.api
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/follow_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pokemongo_bot.base_task import BaseTask

class FollowCluster(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.is_at_destination = False
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/follow_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@


class FollowPath(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.ptr = 0
self._process_config()
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/follow_spiral.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pokemongo_bot.base_task import BaseTask

class FollowSpiral(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.steplimit = self.config.get("diameter", 4)
self.step_size = self.config.get("step_size", 70)
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/handle_soft_ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@


class HandleSoftBan(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def work(self):
if not self.should_run():
return
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/incubate_eggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@


class IncubateEggs(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

last_km_walked = 0

def initialize(self):
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/move_to_fort.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


class MoveToFort(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.lure_distance = 0
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/move_to_map_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


class MoveToMapPokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.last_map_update = 0
self.pokemon_data = self.bot.pokemon_list
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/nickname_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pokemongo_bot.base_task import BaseTask

class NicknamePokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.template = self.config.get('nickname_template','').lower().strip()
if self.template == "{name}":
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/recycle_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pokemongo_bot.tree_config_builder import ConfigException

class RecycleItems(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.item_filter = self.config.get('item_filter', {})
self._validate_item_filter()
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/sleep_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SleepSchedule(BaseTask):
duration_random_offset: (HH:MM) random offset of duration of sleep
for this example the possible duration is 5:00-6:00
"""
SUPPORTED_TASK_API_VERSION = 1

LOG_INTERVAL_SECONDS = 600
SCHEDULING_MARGIN = timedelta(minutes=10) # Skip if next sleep is RESCHEDULING_MARGIN from now
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/spin_fort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


class SpinFort(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def should_run(self):
if not self.bot.has_space_for_loot():
self.emit_event(
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/cell_workers/transfer_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


class TransferPokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def work(self):
pokemon_groups = self._release_pokemon_get_groups()
for pokemon_id in pokemon_groups:
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/update_title_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class UpdateTitleStats(BaseTask):
stats : An array of stats to display and their display order (implicitly),
see available stats above.
"""
SUPPORTED_TASK_API_VERSION = 1

DEFAULT_MIN_INTERVAL = 10
DEFAULT_DISPLAYED_STATS = []
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/test/resources/plugin_fixture/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from fake_task import FakeTask
from unsupported_api_task import UnsupportedApiTask
2 changes: 2 additions & 0 deletions pokemongo_bot/test/resources/plugin_fixture/fake_task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pokemongo_bot.base_task import BaseTask

class FakeTask(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def work(self):
return 'FakeTask'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pokemongo_bot.base_task import BaseTask

class UnsupportedApiTask(BaseTask):
SUPPORTED_TASK_API_VERSION = 2

def work():
return 2
22 changes: 22 additions & 0 deletions pokemongo_bot/tree_config_builder.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import cell_workers
from pokemongo_bot.plugin_loader import PluginLoader
from pokemongo_bot.base_task import BaseTask

class ConfigException(Exception):
pass

class MismatchTaskApiVersion(Exception):
pass

class TreeConfigBuilder(object):
def __init__(self, bot, tasks_raw):
self.bot = bot
Expand Down Expand Up @@ -38,6 +42,24 @@ def build(self):
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)

Expand Down
41 changes: 39 additions & 2 deletions tests/tree_config_builder_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest
import json
import os
from pokemongo_bot import PokemonGoBot, ConfigException, TreeConfigBuilder, PluginLoader
from pokemongo_bot import PokemonGoBot, ConfigException, MismatchTaskApiVersion, TreeConfigBuilder, PluginLoader, BaseTask
from pokemongo_bot.cell_workers import HandleSoftBan, CatchLuredPokemon
from pokemongo_bot.test.resources.plugin_fixture import FakeTask
from pokemongo_bot.test.resources.plugin_fixture import FakeTask, UnsupportedApiTask

def convert_from_json(str):
return json.loads(str)
Expand Down Expand Up @@ -99,3 +99,40 @@ def test_load_plugin_task(self):
tree = builder.build()
result = tree[0].work()
self.assertEqual(result, 'FakeTask')

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

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

return TreeConfigBuilder(self.bot, obj)

def test_task_version_too_high(self):
builder = self.setupUnsupportedBuilder()

previous_version = BaseTask.TASK_API_VERSION
BaseTask.TASK_API_VERSION = 1

self.assertRaisesRegexp(
MismatchTaskApiVersion,
"Task plugin_fixture.UnsupportedApiTask only works with task api version 2, you are currently running version 1. Do you need to update the bot?",
builder.build)

BaseTask.TASK_API_VERSION = previous_version

def test_task_version_too_low(self):
builder = self.setupUnsupportedBuilder()

previous_version = BaseTask.TASK_API_VERSION
BaseTask.TASK_API_VERSION = 3

self.assertRaisesRegexp(
MismatchTaskApiVersion,
"Task plugin_fixture.UnsupportedApiTask only works with task api version 2, you are currently running version 3. Is there a new version of this task?",
builder.build)

BaseTask.TASK_API_VERSION = previous_version