Skip to content

Commit

Permalink
Support loading plugins from .zip files (#2766)
Browse files Browse the repository at this point in the history
  • Loading branch information
elicwhite authored Aug 7, 2016
1 parent 6960f35 commit eeecbc6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pokemongo_bot/plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@
class PluginLoader(object):
folder_cache = []

def _get_correct_path(self, path):
extension = os.path.splitext(path)[1]

if extension == '.zip':
correct_path = path
else:
correct_path = os.path.dirname(path)

return correct_path

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)
correct_path = self._get_correct_path(path)

if correct_path not in self.folder_cache:
self.folder_cache.append(correct_path)
sys.path.append(correct_path)

def remove_path(self, path):
correct_path = self._get_correct_path(path)
sys.path.remove(correct_path)

def get_class(self, namespace_class):
[namespace, class_name] = namespace_class.split('.')
Expand Down
8 changes: 8 additions & 0 deletions pokemongo_bot/test/plugin_loader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ def test_load_namespace_class(self):
self.plugin_loader.load_path(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_path(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)
Binary file not shown.

0 comments on commit eeecbc6

Please sign in to comment.