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

WIP - Plugin architecture #1799

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions changedetectionio/processors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,26 @@ def run(self, uuid, skip_when_checksum_same=True, preferred_proxy=None):


def available_processors():
import importlib
import pkgutil

from . import restock_diff, text_json_diff
x=[('text_json_diff', text_json_diff.name), ('restock_diff', restock_diff.name)]
# @todo Make this smarter with introspection of sorts.
return x

processors = [('text_json_diff', text_json_diff.name), ('restock_diff', restock_diff.name)]

discovered_plugins = {
name: importlib.import_module(name)
for finder, name, ispkg
in pkgutil.iter_modules()
if name.startswith('changedetectionio-plugin-')
}

try:
for name, plugin in discovered_plugins.items():
if hasattr(plugin, 'processors'):
for machine_name, desc in plugin.processors.items():
processors.append((machine_name, desc))
except Exception as e:
print (f"Problem fetching one or more plugins")

return processors
14 changes: 14 additions & 0 deletions changedetectionio/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def __init__(self, datastore_path="/datastore", include_default_watches=True, ve

self.needs_write = True

self.scan_plugins()
# Finally start the thread that will manage periodic data saves to JSON
save_data_thread = threading.Thread(target=self.save_datastore).start()

Expand Down Expand Up @@ -612,6 +613,19 @@ def get_all_tags_for_watch(self, uuid):
def tag_exists_by_name(self, tag_name):
return any(v.get('title', '').lower() == tag_name.lower() for k, v in self.__data['settings']['application']['tags'].items())

def scan_plugins(self):
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also uses local ~/.cdio-plugins/ perhaps

import importlib
import pkgutil

discovered_plugins = {
name: importlib.import_module(name)
for finder, name, ispkg
in pkgutil.iter_modules()
if name.startswith('changedetectionio-plugin-')
}

return discovered_plugins

# Run all updates
# IMPORTANT - Each update could be run even when they have a new install and the schema is correct
# So therefor - each `update_n` should be very careful about checking if it needs to actually run
Expand Down
21 changes: 19 additions & 2 deletions changedetectionio/update_worker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import importlib
import os
import threading
import pkgutil
import queue
import threading
import time

from changedetectionio import content_fetcher
Expand Down Expand Up @@ -229,13 +231,28 @@ def run(self):
now = time.time()

try:
processor = self.datastore.data['watching'][uuid].get('processor','text_json_diff')
processor = self.datastore.data['watching'][uuid].get('processor', 'text_json_diff')

# @todo some way to switch by name
if processor == 'restock_diff':
update_handler = restock_diff.perform_site_check(datastore=self.datastore)
else:
# Used as a default and also by some tests
discovered_plugins = {
name: importlib.import_module(name)
for finder, name, ispkg
in pkgutil.iter_modules()
if name.startswith('changedetectionio-plugin-')
}

for module_name, plugin in discovered_plugins.items():
if hasattr(plugin, 'processors'):
for machine_name, desc in plugin.processors:
if machine_name == processor:
module = importlib.import_module(f"{module_name}.processors.{plugin}")
update_handler = module.perform_site_check(datastore=self.datastore)
#processors.append((machine_name, desc))

update_handler = text_json_diff.perform_site_check(datastore=self.datastore)

changed_detected, update_obj, contents = update_handler.run(uuid, skip_when_checksum_same=queued_item_data.item.get('skip_when_checksum_same'))
Expand Down