From: Frederic Massart Date: Fri, 9 May 2014 09:00:31 +0000 (+0800) Subject: PluginManager can find out a subsystem or plugin from a path X-Git-Tag: v1.2~8 X-Git-Url: https://git.cameron1729.xyz/?a=commitdiff_plain;h=f3ef6d10a155274a774ff0d869d95891eb30030a;p=mdk.git PluginManager can find out a subsystem or plugin from a path --- diff --git a/lib/plugins.py b/lib/plugins.py index fc384c8..11eee83 100644 --- a/lib/plugins.py +++ b/lib/plugins.py @@ -187,6 +187,55 @@ class PluginManager(object): return path @classmethod + def getSubsystemOrPluginFromPath(cls, path, M=None): + """Get a subsystem from a path. Path should be relative to dirroot or M should be passed. + + This returns a tuple containing the name of the subsystem or plugin type, and the plugin name + if we could resolve one. + """ + + subtypes = {} + if M: + path = '/' + path.replace(M.get('path'), '').strip('/') + admindir = M.get('admin', 'admin') + if path.startswith('/' + admindir): + path = re.sub(r'^/%s' % admindir, '/{admin}', path) + subtypes = cls.getSubtypes(M) + path = '/' + path.lstrip('/') + + pluginOrSubsystem = None + pluginName = None + candidate = path + head = True + tail = None + while head and head != '/' and not pluginOrSubsystem: + # Check subsystems. + for k, v in cls._subSystems.iteritems(): + if v == candidate: + pluginOrSubsystem = k + break + + # Check plugin types. + if not pluginOrSubsystem: + for k, v in cls._pluginTypesPath.iteritems(): + if v == candidate: + pluginOrSubsystem = k + pluginName = tail + break + + # Check sub plugin types. + if not pluginOrSubsystem: + for k, v in subtypes.iteritems(): + if v == candidate: + pluginOrSubsystem = k + pluginName = tail + break + (head, tail) = os.path.split(candidate) + candidate = head + + return (pluginOrSubsystem, pluginName) + + @classmethod def getSubtypes(cls, M): """Get the sub plugins declared in an instance""" regex = re.compile(r'\s*(?P[\'"])(.*?)(?P=brackets)\s*=>\s*(?P=brackets)(.*?)(?P=brackets)')