From 2acff3196d87ccc1c7e9fa19ab7ed18a20f7c1cc Mon Sep 17 00:00:00 2001 From: Karl-Martin Minkner Date: Sun, 7 Jun 2015 10:02:15 +0200 Subject: [PATCH] Better plugins directory structure. Sorted by author. Every plugin can now be loaded with the instance config in config.ini. Core plugins "core_" are loaded automatically, no matter in that folder they are! Core plugins loaded first. --- plugins/{ => core}/core_TS3channel.py | 0 plugins/{ => core}/core_TS3chat.py | 0 plugins/{ => core}/core_TS3clients.py | 0 plugins/{ => core}/core_TS3db.py | 0 plugins/{ => core}/core_TS3events.py | 0 .../{instance1/deactivated => demo}/demo_1.py | 0 .../deactivated/test.py => demo/demo_test.py} | 0 .../test2.py => demo/demo_test2.py} | 0 .../deactivated => jonni}/jonni_afkmover.py | 0 plugins/{ => jonni}/jonni_userTracker.py | 0 .../kdr_dynamic_channel.py | 0 ts3base.py | 84 +++++++++++++------ 12 files changed, 57 insertions(+), 27 deletions(-) rename plugins/{ => core}/core_TS3channel.py (100%) rename plugins/{ => core}/core_TS3chat.py (100%) rename plugins/{ => core}/core_TS3clients.py (100%) rename plugins/{ => core}/core_TS3db.py (100%) rename plugins/{ => core}/core_TS3events.py (100%) rename plugins/{instance1/deactivated => demo}/demo_1.py (100%) rename plugins/{instance1/deactivated/test.py => demo/demo_test.py} (100%) rename plugins/{instance1/deactivated/test2.py => demo/demo_test2.py} (100%) rename plugins/{instance1/deactivated => jonni}/jonni_afkmover.py (100%) rename plugins/{ => jonni}/jonni_userTracker.py (100%) rename plugins/{instance1/deactivated => kandru}/kdr_dynamic_channel.py (100%) diff --git a/plugins/core_TS3channel.py b/plugins/core/core_TS3channel.py similarity index 100% rename from plugins/core_TS3channel.py rename to plugins/core/core_TS3channel.py diff --git a/plugins/core_TS3chat.py b/plugins/core/core_TS3chat.py similarity index 100% rename from plugins/core_TS3chat.py rename to plugins/core/core_TS3chat.py diff --git a/plugins/core_TS3clients.py b/plugins/core/core_TS3clients.py similarity index 100% rename from plugins/core_TS3clients.py rename to plugins/core/core_TS3clients.py diff --git a/plugins/core_TS3db.py b/plugins/core/core_TS3db.py similarity index 100% rename from plugins/core_TS3db.py rename to plugins/core/core_TS3db.py diff --git a/plugins/core_TS3events.py b/plugins/core/core_TS3events.py similarity index 100% rename from plugins/core_TS3events.py rename to plugins/core/core_TS3events.py diff --git a/plugins/instance1/deactivated/demo_1.py b/plugins/demo/demo_1.py similarity index 100% rename from plugins/instance1/deactivated/demo_1.py rename to plugins/demo/demo_1.py diff --git a/plugins/instance1/deactivated/test.py b/plugins/demo/demo_test.py similarity index 100% rename from plugins/instance1/deactivated/test.py rename to plugins/demo/demo_test.py diff --git a/plugins/instance1/deactivated/test2.py b/plugins/demo/demo_test2.py similarity index 100% rename from plugins/instance1/deactivated/test2.py rename to plugins/demo/demo_test2.py diff --git a/plugins/instance1/deactivated/jonni_afkmover.py b/plugins/jonni/jonni_afkmover.py similarity index 100% rename from plugins/instance1/deactivated/jonni_afkmover.py rename to plugins/jonni/jonni_afkmover.py diff --git a/plugins/jonni_userTracker.py b/plugins/jonni/jonni_userTracker.py similarity index 100% rename from plugins/jonni_userTracker.py rename to plugins/jonni/jonni_userTracker.py diff --git a/plugins/instance1/deactivated/kdr_dynamic_channel.py b/plugins/kandru/kdr_dynamic_channel.py similarity index 100% rename from plugins/instance1/deactivated/kdr_dynamic_channel.py rename to plugins/kandru/kdr_dynamic_channel.py diff --git a/ts3base.py b/ts3base.py index cd974bf..a1aa7b6 100644 --- a/ts3base.py +++ b/ts3base.py @@ -10,14 +10,6 @@ from threading import Thread from threading import Lock -""" Changelog -------------- -2. kandru -- changed identifier to self.identifier -- changed package to self.package -1. kandru - initial commit -""" - # For easier usage calculate the path relative to here. here = os.path.abspath(os.path.dirname(__file__)) get_path = partial(os.path.join, here) @@ -26,45 +18,44 @@ class ts3base(threading.Thread): def __init__(self, config): + """ + Creates a new instance of ts3base and initializes all neccessary parameters + """ # init threading threading.Thread.__init__(self) - # set config for whole class self.config = config - # debug message self.debprint('instance initialized') - # init callbacks self.callbacks = defaultdict(dict) # list of all classes (instances, objects, however) self.classes = {} - # identifier + package name for pluginbase self.identifier = config['id'] self.package = 'ts3eventscripts' + self.identifier - # init pluginbase self.pluginbase = PluginBase(package=self.package) - # init pluginsource - self.pluginsource = self.pluginbase.make_plugin_source( - # two plugin directories: global plugins in plugins/, instance only plugins in directory named with the instance name - searchpath=[get_path('./plugins/' + self.config['id']), get_path('./plugins')], identifier=self.identifier) - # lock for command socket send & receive method self.sendlock = Lock() - # init ts3 connection self.ts3_init() + # load user plugins + self.plugin_load() + for plugin in self.pluginsource.list_plugins(): + self.debprint(plugin) # init all plugins self.plugin_init() def ts3_init(self): + """ + Initialization of the ts3eventscript sockets for teamspeak3 + """ # init ts3 query socket (command socket) self.command_socket = ts3socket( self.config['ip'], - self.config['port'], - self.config['sid'], + int(self.config['port']), + int(self.config['sid']), self.config['user'], self.config['pass']) # debug message @@ -73,8 +64,8 @@ def ts3_init(self): # init event socket for event thread self.event_socket = ts3socket( self.config['ip'], - self.config['port'], - self.config['sid'], + int(self.config['port']), + int(self.config['sid']), self.config['user'], self.config['pass']) # send register commands @@ -87,12 +78,34 @@ def ts3_init(self): self.event_thread.start() self.debprint('initialized event socket in thread') + def plugin_load(self): + """ + Load plugins specified from a user + """ + paths = [] + for path in os.listdir(get_path('./plugins/')): + paths.append('./plugins/' + path) + self.pluginsource = self.pluginbase.make_plugin_source( + # core plugins are loaded for any instance. Other plugins are handled later on + searchpath=paths, identifier=self.identifier) + def plugin_init(self): - # debug message + """ + Init plugins for this instance + """ self.debprint('loading plugins') + plugins = self.config['plugins'].split(',') + # load core plugins first for plugin_name in self.pluginsource.list_plugins(): - plugin = self.pluginsource.load_plugin(plugin_name) - plugin.setup(self) # for advanced usage, add a socket as passed variable + author = plugin_name.split('_', 1)[0] + if author == 'core': + plugin = self.pluginsource.load_plugin(plugin_name) + plugin.setup(self) # pass ts3base instance to plugin + # load user plugins next + for plugin_name in self.pluginsource.list_plugins(): + if plugin_name in plugins: + plugin = self.pluginsource.load_plugin(plugin_name) + plugin.setup(self) # pass ts3base instance to plugin def event_process(self): """ @@ -104,9 +117,15 @@ def event_process(self): self.execute_callback('ts3.receivedevent', event) def get_event_socket(self): + """ + Get the event socket + """ return self.event_socket def get_command_socket(self): + """ + Get the command socket + """ return self.command_socket def send_receive(self, cmd): @@ -119,6 +138,9 @@ def send_receive(self, cmd): return self.command_socket.recv_all() def register_callback(self, plugin, key, function): + """ + Register a new callback + """ self.callbacks[key][plugin + '_' + function.__name__] = function # debug message self.debprint( @@ -130,6 +152,9 @@ def register_callback(self, plugin, key, function): key) def execute_callback(self, key, values): + """ + Execute all plugin callbacks from a specific type in a separate thread + """ if key in self.callbacks: for index, func in self.callbacks[key].items(): t = Thread(target=func, args=(values,)) @@ -166,10 +191,15 @@ def register_class(self, key, plugin): plugin.__name__) def debprint(self, msg): + """ + Prints a debug message to the console + """ print(self.config['id'] + ' - ' + msg) def run(self): - + """ + Main class to keep ts3base.py running forever + """ try: # set nickname to instance id to identify itself (set some id's) ts3tools.set_nickname(self, self.config['id'])