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

Modular plugins #131

Merged
merged 2 commits into from
Jul 25, 2022
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
9 changes: 1 addition & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ def get_long_description() -> str:
python_requires=">=3.7.0",
zip_safe=False,
install_requires=[
"PyDictionary==1.5.2",
"requests==2.21.0",
"wikipedia==1.4.0",
"google==2.0.2",
"beautifulsoup4==4.7.1",
"deuces==0.2",
"httplib2==0.18.0",
"flightradar24==0.2"

],
classifiers=[
"Development Status :: 4 - Beta",
Expand Down
78 changes: 45 additions & 33 deletions src/honeybot/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import sys
import time
import os
import pathlib
import pkg_resources
import setuptools
import subprocess
# from pathlib import Path

try:
Expand Down Expand Up @@ -149,53 +153,61 @@ def print_running_infos(self):
print(key, self.info[key])
print('-'*3)

def load_plugins(self):
"""
Load plugins that are specified in the plugins list.

Args:
plugins_to_load (list of str): List of plugins to load.

Examples:
TODO
"""

logger.info("Loading plugins...")
def load_plugins_from_folder(self, category_folder, from_conf=None, from_dir=None):

to_load = []
plugs = os.path.join(self.settings_path, "PLUGINS.conf")
with open(plugs) as f:
to_load = f.read().split("\n")
to_load = list(filter(lambda x: x != "", to_load))
if from_dir is not None:
to_load = [f for f in os.listdir(from_dir) if self.is_valid_plug_name(f)]
elif from_conf is not None:
to_load = []
plugs = from_conf
with open(plugs) as f:
to_load = f.read().split("\n")
to_load = list(filter(lambda x: x != "", to_load))

print('Loading', category_folder)
for folder in to_load:
print("loading plugin:", folder)
try:
sys.path.append(self.root_path)
module = importlib.import_module(
"plugins.downloaded.{}.main".format(folder))
"plugins.{}.{}.main".format(category_folder, folder))
obj = module
self.plugins.append(obj)
except ModuleNotFoundError as e:
logger.warning(f"{folder}: module import error, skipped' {e}")

print()
print('loading core plugins')
for folder in os.listdir(os.path.join(self.info['plugins_path'], 'core')):
if self.is_valid_plug_name(folder):
print("loading plugin:", folder)
try:
module = importlib.import_module(
"plugins.core.{}.main".format(folder))
obj = module
self.plugins.append(obj)
except ModuleNotFoundError as e:
logger.warning(f"{folder}: module import error, skipped' {e}")
else:
if not folder.startswith('__'):
logger.warning(f"{folder}: name not valid")
try:
req_path = os.path.join(self.info['cwd'], 'plugins', category_folder, folder, 'requirements.txt')
if os.path.exists(req_path):
with pathlib.Path(req_path).open() as requirements_txt:
install_requires = [
str(requirement)
for requirement
in pkg_resources.parse_requirements(requirements_txt)
]
print('installing', install_requires)
subprocess.check_call([sys.executable, '-m', 'pip', 'install', *install_requires])
except Exception as e:
logger.debug(e)

def load_plugins(self):
"""
Load plugins that are specified in the plugins list.

Args:
plugins_to_load (list of str): List of plugins to load.

Examples:
TODO
"""

logger.info("Loading plugins...")


conf_path = os.path.join(self.settings_path, "PLUGINS.conf")
dir_path = os.path.join(self.info['plugins_path'], 'core')
self.load_plugins_from_folder('downloaded', from_conf=conf_path)
self.load_plugins_from_folder('core', from_dir=dir_path)

logger.info("Loaded plugins")
print('---')
Expand Down
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/bitcoin/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# parts not necessary for blackjack but required for poker commented out

# import deuces
# import deuces "deuces==0.2",
# import best5
# import board
# import card
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/book_of_day/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/comic/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/converter/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/corona/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/dadjoke/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyDictionary==1.5.2
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/flight/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flightradar24==0.2
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/google/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google==2.0.2
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/horoscope/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/monero/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/news/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/onthisday/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/pynews/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/pypi/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/repostats/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/story/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/tpbquote/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
beautifulsoup4
2 changes: 2 additions & 0 deletions src/honeybot/plugins/downloaded/translate/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
httplib2
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/weather/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
1 change: 1 addition & 0 deletions src/honeybot/plugins/downloaded/wikipedia/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wikipedia