Installation of requirements.txt before loading module
This commit is contained in:
parent
f6fd324912
commit
3f007cc83f
@ -20,7 +20,7 @@ DATA_LOCATION = os.path.join(os.path.dirname(__file__), '../data/bot.db')
|
||||
private_message = "Hey {}! Ik heb je even een privébericht gestuurd 🙂"
|
||||
|
||||
def eprint(*args, **kwargs):
|
||||
'''Print error messages to stderr'''
|
||||
"""Print error messages to stderr"""
|
||||
print(*args, file=sys.stderr, **kwargs)
|
||||
|
||||
class MatrixBotAPI:
|
||||
@ -85,6 +85,10 @@ class MatrixBotAPI:
|
||||
self.add_handler(self.help_handler)
|
||||
|
||||
def add_plugins(self):
|
||||
"""Acquire list of plugins from configuration, load them,
|
||||
initialize them, and add them to a list of object
|
||||
"""
|
||||
|
||||
# Store empty list of plugins
|
||||
self.plugin_objects = []
|
||||
|
||||
@ -93,72 +97,91 @@ class MatrixBotAPI:
|
||||
modules = []
|
||||
|
||||
#try:
|
||||
for plugin in self.config['plugins']:
|
||||
# Loop through the available plugins, install their requirements,
|
||||
# load them as module, run their setup, append them to a list,
|
||||
# and add their handler variables
|
||||
for i, plugin in enumerate(self.config['plugins']):
|
||||
# Install requirements
|
||||
self.install_requirements(plugin)
|
||||
|
||||
# Dynamically load the module
|
||||
modules.append(
|
||||
importlib.import_module(
|
||||
"plugins.{0}.{0}".format(plugin), package = None))
|
||||
|
||||
# Run the module's setup function and save that it got installed
|
||||
self.setup_plugin(modules[i])
|
||||
|
||||
# Create new instance of plugin and append to plugin_objects array
|
||||
self.plugin_objects.append(modules[i].Plugin(self))
|
||||
|
||||
# Add handler of newly created instance to bot
|
||||
self.add_handler(self.plugin_objects[i].handler)
|
||||
#except:
|
||||
# eprint("Importing one or more of the plugins did not go well!")
|
||||
# sys.exit()
|
||||
|
||||
# Loop through the available modules and add their handler variables
|
||||
for i, module in enumerate(modules):
|
||||
# Check if module is already installed
|
||||
self.setup_plugin(module)
|
||||
|
||||
# Create new instance of plugin and append to plugin_objects array
|
||||
self.plugin_objects.append(module.Plugin(self))
|
||||
|
||||
# Add handler of newly created instance to bot
|
||||
self.add_handler(self.plugin_objects[i].handler)
|
||||
|
||||
def setup_plugin(self, module):
|
||||
"""Check in an SQLite database if the plugin is already installed.
|
||||
If this is not the case, install all required packages and run the
|
||||
plugin's setup method.
|
||||
def check_installation_plugin(self, module_name):
|
||||
"""Function returns 0 if a plugin with that name is not
|
||||
currently installed
|
||||
"""
|
||||
module_name = module.__name__.split('.')[-1]
|
||||
|
||||
# Open SQLite database
|
||||
sqlite_db = sqlite3.connect(DATA_LOCATION)
|
||||
|
||||
sqlite_cursor = sqlite_db.cursor()
|
||||
|
||||
# Define query to SELECT event from SQLite DB
|
||||
select_sql = f"""SELECT module_name
|
||||
FROM plugins
|
||||
WHERE module_name = "{module_name}" """
|
||||
|
||||
# Open SQLite database and run query
|
||||
sqlite_db = sqlite3.connect(DATA_LOCATION)
|
||||
sqlite_cursor = sqlite_db.cursor()
|
||||
sqlite_cursor.execute(select_sql)
|
||||
|
||||
if sqlite_cursor.fetchall():
|
||||
number_fetched = sqlite_cursor.fetchall()
|
||||
|
||||
sqlite_db.close()
|
||||
|
||||
return number_fetched
|
||||
|
||||
def install_requirements(self, module_name):
|
||||
"""Install requirements for a given plugin."""
|
||||
if self.check_installation_plugin(module_name):
|
||||
# Do nothing, this was already installed
|
||||
print(f"Check: {module_name} already installed.")
|
||||
else:
|
||||
# This appears to be new, insert it, process requirements.txt
|
||||
# and run the plugin's setup file.
|
||||
print(f"Running installation of {module_name}.")
|
||||
|
||||
# Install requirements
|
||||
pipmain(
|
||||
['install',
|
||||
'-r',
|
||||
f'plugins/{module_name}/requirements.txt'])
|
||||
|
||||
|
||||
def setup_plugin(self, module):
|
||||
"""Run the setup for a given plugin and subsequently save that
|
||||
this plugin was installed.
|
||||
"""
|
||||
|
||||
module_name = module.__name__.split('.')[-1]
|
||||
|
||||
if not self.check_installation_plugin(module_name):
|
||||
# This appears to be new, insert it, process requirements.txt
|
||||
# and run the plugin's setup file.
|
||||
print(f"Running installation of {module_name}.")
|
||||
|
||||
# Run module's install method
|
||||
try:
|
||||
#try:
|
||||
module.setup()
|
||||
except:
|
||||
print(f"{module_name} did not specify setup(). Skipping...")
|
||||
#except:
|
||||
# print(f"{module_name} did not specify setup(). Skipping...")
|
||||
|
||||
# Save in database that we installed this plugin
|
||||
datetime_added = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Open SQLite database
|
||||
insert_sql = f"""INSERT INTO plugins(module_name, datetime_added)
|
||||
VALUES('{module_name}', '{datetime_added}')"""
|
||||
|
||||
sqlite_db = sqlite3.connect(DATA_LOCATION)
|
||||
sqlite_cursor = sqlite_db.cursor()
|
||||
sqlite_cursor.execute(insert_sql)
|
||||
sqlite_db.commit()
|
||||
sqlite_db.close()
|
||||
@ -209,6 +232,12 @@ class MatrixBotAPI:
|
||||
pass
|
||||
|
||||
def send_message_private_public(self, room, event, message):
|
||||
"""This method takes a room, event, and message and makes sure
|
||||
that the message is sent in a private room and not in a public
|
||||
room. If no private room exists, it will create a private room
|
||||
with the sender of the event.
|
||||
"""
|
||||
|
||||
orig_room = room
|
||||
found_room = False
|
||||
|
||||
|
@ -1,11 +0,0 @@
|
||||
<strong>@room er is zojuist een nieuw evenement aangemaakt! Op {} om {} zal "{}" plaatsvinden.</strong>
|
||||
|
||||
<br /><br />
|
||||
|
||||
Ben je aangemeld op de leden database in je favoriete browser? Klik dan hier:
|
||||
<ul>
|
||||
<li> <a href='{}'>om het evenement te bekijken</a> 👀
|
||||
<li> <a href='{}'>om je op aanwezig te zetten</a> ✅
|
||||
<li> <a href='{}'>om je op misschien te zetten</a> 🤷
|
||||
<li> <a href='{}'>om je op afwezig te zetten</a> ⛔
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user