From: zonidjan Date: Sun, 3 Sep 2017 00:55:36 +0000 (-0500) Subject: added softdeps to modinfo - bumped APIVERSION X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/a62d0d18bbb5926d22df18db5eba53eb21a10817?hp=591964e837ca06bef75e99a022dbb09582488f51 added softdeps to modinfo - bumped APIVERSION APIVERSION is now 2. modules written for 2 are compatible with 1; however modules written for 1 need to be updated (by adding softdeps to modinfo). softdeps are loaded before the current module is started, when possible. however, if they are disabled (by setting them to 0 in config "[autoloads]"), or non-existent, they will be ignored. Additionally, if they are unloaded at runtime any modules with softdeps on them will continue to be loaded and are expected to not raise any errors. --- diff --git a/ctlmod.py b/ctlmod.py index be1095f..dbd2a36 100644 --- a/ctlmod.py +++ b/ctlmod.py @@ -19,17 +19,24 @@ def load(parent, modname, dependent=False): print "%09.3f [MOD] [?] Loading %s..." % (time.time() % 100000, modname), modstatus = _load(parent, modname, dependent) if not modstatus: - print str(modstatus) + if dependent: + print "failed: %s)" % (modstatus), + else: + print "failed: %s." % (modstatus) elif modstatus == True: if dependent: print "OK)", else: print "OK." else: - print modstatus + if dependent: + print "OK: %s)" % (modstatus), + else: + print "OK: %s." % (modstatus) return modstatus def _load(parent, modname, dependent=False): + successstatus = [] if not isloaded(modname): try: mod = __import__('modules.'+modname, globals(), locals(), ['*'], -1) @@ -49,20 +56,44 @@ def _load(parent, modname, dependent=False): dependents[modname] = [] for dep in mod.modinfo['depends']: - if dep not in modules: - depret = load(parent, dep, dependent=True) - if depret is not None and not depret: - return depret #TODO FIXME + if bool(int(parent.cfg.get('autoloads', dep, default=1))): + if dep not in modules: + depret = load(parent, dep, dependent=True) + if depret is not None and not depret: + return depret + else: + return modlib.error("dependent %s disabled" % (dep)) dependents[dep].append(modname) + for dep in mod.modinfo['softdeps']: + if bool(int(parent.cfg.get('autoloads', dep, default=1))): + if dep not in modules: + depret = load(parent, dep, dependent=True) + if depret is not None: + if not depret: + successstatus.append("softdep %s failed" % (dep)) + else: + successstatus.append("softdep %s disabled" % (dep)) + #swallow errors loading - softdeps are preferred, not required + ret = mod.modstart(parent) - if ret is not None and not ret: + if ret is None: + ret = True + if not ret: del modules[modname] del dependents[modname] for dep in mod.modinfo['depends']: dependents[dep].remove(modname) - return ret + + successstatus = ';'.join(successstatus) + if len(successstatus) > 0 and ret: + if ret == True: + return successstatus + else: + return "%s (%s)" % (ret, successstatus) + else: + return ret else: #if not isloaded...else: return modlib.error('already loaded') diff --git a/erebus.py b/erebus.py index 190c4ba..713c384 100644 --- a/erebus.py +++ b/erebus.py @@ -7,7 +7,7 @@ import os, sys, select, MySQLdb, MySQLdb.cursors, time, random import bot, config, ctlmod class Erebus(object): #singleton to pass around - APIVERSION = 1 + APIVERSION = 2 RELEASE = 0 bots = {} diff --git a/modlib.py b/modlib.py index 89a75d4..05ecf53 100644 --- a/modlib.py +++ b/modlib.py @@ -42,6 +42,13 @@ class modlib(object): self.name = (name.split("."))[-1] def modstart(self, parent): + #modstart can return a few things... + # None: unspecified success + # False: unspecified error + # modlib.error (or anything else False-y): specified error + # True: unspecified success + # non-empty string (or anything else True-y): specified success + #"specified" values will be printed. unspecified values will result in "OK" or "failed" self.parent = parent for cmd, func in self.hooks.iteritems(): self.parent.hook(cmd, func) diff --git a/modules/coins.py b/modules/coins.py index 8548d60..5b3d663 100644 --- a/modules/coins.py +++ b/modules/coins.py @@ -6,8 +6,9 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': [], # other modules required to work properly? + 'compatible': [1,2], + 'depends': [], + 'softdeps': [], } # preamble diff --git a/modules/control.py b/modules/control.py index 74e0a66..572853d 100644 --- a/modules/control.py +++ b/modules/control.py @@ -6,8 +6,9 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], + 'compatible': [1,2], 'depends': [], + 'softdeps': ['help'], } # preamble diff --git a/modules/eval.py b/modules/eval.py index 3e06472..d2b82c8 100644 --- a/modules/eval.py +++ b/modules/eval.py @@ -6,8 +6,9 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], + 'compatible': [1,2], 'depends': [], + 'softdeps': ['help'], } # preamble diff --git a/modules/foo.py b/modules/foo.py index 0278eb1..bb56be0 100644 --- a/modules/foo.py +++ b/modules/foo.py @@ -6,9 +6,15 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions + 'compatible': [1,2], # compatible module API versions 'depends': [], # other modules required to work properly? + 'softdeps': ['help'], # modules which are preferred but not required } +# note: softdeps will be loaded before this module, IF not disabled in the configuration (autoload.module = 0) (and if it exists) +# however, if it is disabled it will be silently ignored, and if it is unloaded at runtime it won't cause this one to unload. +# +# basically, softdeps are things this module will use if available, but does not require (no errors will occur if it's not loaded) +# for example, @lib.help() will attempt to use the help module, but swallow errors if it is not loaded # preamble import modlib diff --git a/modules/help.py b/modules/help.py index 299cf0a..0010470 100644 --- a/modules/help.py +++ b/modules/help.py @@ -6,8 +6,9 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': [], # other modules required to work properly? + 'compatible': [1,2], + 'depends': [], + 'softdeps': [], } # preamble diff --git a/modules/resources.py b/modules/resources.py index 35feebd..016e4bd 100644 --- a/modules/resources.py +++ b/modules/resources.py @@ -1,13 +1,14 @@ # Erebus IRC bot - Author: Erebus Team -# simple module example +# resource-usage module # This file is released into the public domain; see http://unlicense.org/ # module info modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': [], # other modules required to work properly? + 'compatible': [1,2], + 'depends': [], + 'softdeps': ['help'], } # preamble diff --git a/modules/sms.py b/modules/sms.py index 3978ae2..16077a3 100644 --- a/modules/sms.py +++ b/modules/sms.py @@ -1,13 +1,14 @@ # Erebus IRC bot - Author: Erebus Team -# simple module example +# twilio sms module # This file is released into the public domain; see http://unlicense.org/ # module info modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': [], # other modules required to work properly? + 'compatible': [1,2], + 'depends': [], + 'softdeps': ['help'], } # preamble diff --git a/modules/trivia.py b/modules/trivia.py index 3eabbbc..cb4b407 100644 --- a/modules/trivia.py +++ b/modules/trivia.py @@ -6,8 +6,9 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': ['userinfo'], # other modules required to work properly? + 'compatible': [1,2], + 'depends': ['userinfo'], + 'softdeps': ['help'], } # preamble diff --git a/modules/urls.py b/modules/urls.py index 3b41870..fc398e2 100644 --- a/modules/urls.py +++ b/modules/urls.py @@ -6,8 +6,9 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': [], # other modules required to work properly? + 'compatible': [1,2], + 'depends': [], + 'softdeps': [], } # http://embed.ly/tools/generator diff --git a/modules/userinfo.py b/modules/userinfo.py index fb0e111..be6e33b 100644 --- a/modules/userinfo.py +++ b/modules/userinfo.py @@ -1,13 +1,14 @@ # Erebus IRC bot - Author: Erebus Team -# trivia module +# userinfo module # This file is released into the public domain; see http://unlicense.org/ # module info modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': [], # other modules required to work properly? + 'compatible': [1,2], + 'depends': [], + 'softdeps': ['help'], } # preamble