Ejemplo n.º 1
0
class EventStreamService(service.Service):
    def start(self):
        super(EventStreamService, self).start()

        LOG.debug("Starting evenstream thread")

        self.load_extenstions()

        self.gerrit = GerritEvents(self._map_gerrit)
        tpool.execute(self.gerrit.start)

    def stop(self):
        super(EventStreamService, self).stop()
        LOG.debug("Stopping eventstream")

    def load_extenstions(self):
        self.manager = DispatchExtensionManager("gerritstreamer.subscribers", lambda i: True)

    @staticmethod
    def filter_extension(ext, event):
        """
        Filter what handler the event should be run on
        """
        return isinstance(event, ext.plugin.events)

    def _from_gerrit(self, ext, event):
        ext = ext.plugin(self.gerrit)
        ext.dispatch(event)

    def _map_gerrit(self, event):
        LOG.info("Got event from Gerrit: %s" % event)
        self.manager.map(self.filter_extension, self._from_gerrit, event)
Ejemplo n.º 2
0
 def load(self, invoke_on_load=True):
     self._manager = DispatchExtensionManager(
         "csm.plugin",
         self._check_plugin,
         invoke_on_load=invoke_on_load,
         invoke_args=(self._ctx, ),
         propagate_map_exceptions=True,
         on_load_failure_callback=self._on_load_failure,
     )
     self._build_plugin_list()
Ejemplo n.º 3
0
 def test_dispatch_map_should_invoke_filter_for_extensions(self):
     em = DispatchExtensionManager.make_test_instance(
         [test_extension, test_extension2])
     filter_func = Mock(return_value=False)
     args = ('A', )
     kw = {'big': 'Cheese'}
     em.map(filter_func, None, *args, **kw)
     filter_func.assert_any_call(test_extension, *args, **kw)
     filter_func.assert_any_call(test_extension2, *args, **kw)
Ejemplo n.º 4
0
 def test_dispatch_map_should_invoke_filter_for_extensions(self):
     em = DispatchExtensionManager.make_test_instance([test_extension,
                                                       test_extension2])
     filter_func = Mock(return_value=False)
     args = ('A',)
     kw = {'big': 'Cheese'}
     em.map(filter_func, None, *args, **kw)
     filter_func.assert_any_call(test_extension, *args, **kw)
     filter_func.assert_any_call(test_extension2, *args, **kw)
Ejemplo n.º 5
0
 def load(self, invoke_on_load=True):
     self._manager = DispatchExtensionManager(
         "csm.plugin",
         self._check_plugin,
         invoke_on_load=invoke_on_load,
         invoke_args=(self._ctx,),
         propagate_map_exceptions=True,
         on_load_failure_callback=self._on_load_failure,
     )
     self._build_plugin_list()
Ejemplo n.º 6
0
    def __init__(self, config: Config, invoke_on_load=False):
        self._logger = logging.getLogger(__name__)

        self.failed_plugins = list()
        self._disabled_plugins = config.plugins.disabled

        self._settings_initialized = False

        def check(ext: Extension):
            return self._check_extension(config, ext)

        self.weather = DriverManager(
            "blueweather.plugins.weather",
            config.plugins.weather_driver,
            on_load_failure_callback=self._on_load_fail
        )
        self.plugins = ExtensionManager(
            "blueweather.plugins.plugin",
            on_load_failure_callback=self._on_load_fail
        )
        self.api = EnabledExtensionManager(
            "blueweather.plugins.api",
            check_func=check,
            on_load_failure_callback=self._on_load_fail
        )
        self.startup = EnabledExtensionManager(
            "blueweather.plugins.startup",
            check_func=check,
            on_load_failure_callback=self._on_load_fail
        )
        self.settings = EnabledExtensionManager(
            "blueweather.plugins.settings",
            check_func=check,
            on_load_failure_callback=self._on_load_fail
        )
        self.unitConversion = DispatchExtensionManager(
            "blueweather.plugins.unitconv",
            check_func=check,
            on_load_failure_callback=self._on_load_fail
        )

        if self._logger.isEnabledFor(logging.INFO):
            extensions = self.getAllExtensions()
            for k, v in extensions.items():
                extensions[k] = '\n\t'.join(v.keys())

            extensions = '\n'.join(
                ["%s: \n\t%s" % (k, v) for k, v in extensions.items()]
            )

            self._logger.info("Discovered Extensions: \n%s", extensions)

        if invoke_on_load:
            self.invoke()
Ejemplo n.º 7
0
    def conversions(man: DispatchExtensionManager) -> Dict[str, Set[str]]:
        """
        Get all the basic conversions

        :param man: UnitConversion Extension Manager

        :return: Possible Conversions
        """
        units = dict()
        for name, conversions in man.map(lambda *args, **kwargs: True,
                                         UnitConversion.get_conversion_types):
            if conversions is None:
                continue
            for from_type, to_type in conversions:
                if from_type not in units:
                    units[from_type] = set()
                units[from_type].add(to_type)
        return units
Ejemplo n.º 8
0
    def __init__(self, ctx=None, invoke_on_load=True):
        self._ctx = PluginContext(ctx)
        try:
            self._platform = self._ctx.family
        except AttributeError:
            self._platform = None
        try:
            self._phase = self._ctx.phase
        except AttributeError:
            self._phase = None

        self._name = None
        self._manager = DispatchExtensionManager(
            "csm.plugin",
            self._check_plugin,
            invoke_on_load=invoke_on_load,
            invoke_args=(self._ctx,),
            propagate_map_exceptions=True,
            on_load_failure_callback=self._on_load_failure,
        )
        self._build_plugin_list()
Ejemplo n.º 9
0
 def test_dispatch_instance_should_use_supplied_extensions(self):
     extensions = [test_extension, test_extension2]
     em = DispatchExtensionManager.make_test_instance(extensions)
     self.assertEqual(extensions, em.extensions)
Ejemplo n.º 10
0
 def test_dispatch_instance_should_use_supplied_extensions(self):
     extensions = [test_extension, test_extension2]
     em = DispatchExtensionManager.make_test_instance(extensions)
     self.assertEqual(extensions, em.extensions)
Ejemplo n.º 11
0
 def load_extenstions(self):
     self.manager = DispatchExtensionManager("gerritstreamer.subscribers", lambda i: True)
def test_dispatch_instance_should_use_supplied_extensions():
    extensions = [test_extension, test_extension2]
    em = DispatchExtensionManager.make_test_instance(extensions)

    assert extensions == em.extensions
Ejemplo n.º 13
0
class CSMPluginManager(object):

    def __init__(self, ctx=None, invoke_on_load=True):
        self._ctx = PluginContext(ctx)
        # The context contains device information after discovery phase
        # There is no need to load plugins which does not match the family and os
        try:
            self._platform = self._ctx.family
        except AttributeError:
            self._platform = None
        try:
            self._os = self._ctx.os_type
        except AttributeError:
            self._os = None

        self._phase = None
        self._name = None

        self.load(invoke_on_load=invoke_on_load)

    def load(self, invoke_on_load=True):
        self._manager = DispatchExtensionManager(
            "csm.plugin",
            self._check_plugin,
            invoke_on_load=invoke_on_load,
            invoke_args=(self._ctx,),
            propagate_map_exceptions=True,
            on_load_failure_callback=self._on_load_failure,
        )
        self._build_plugin_list()

    def __getitem__(self, item):
        return self._manager.__getitem__(item)

    def _build_plugin_list(self):
        self.plugins = {}
        for ext in self._manager:
            self.plugins[ext.name] = {
                #  'package_name': ext.entry_point.dist.project_name,
                'package_name': ext.entry_point.module_name.split(".")[0],
                'name': ext.plugin.name,
                'description': ext.plugin.__doc__,
                'phases': ext.plugin.phases,
                'platforms': ext.plugin.platforms,
                'os': ext.plugin.os
            }

    def _filter_func(self, ext, *args, **kwargs):
        if self._platform and bool(ext.plugin.platforms) and self._platform not in ext.plugin.platforms:
            return False
        if self._phase and self._phase not in ext.plugin.phases:
            return False
        if self._name and ext.plugin.name not in self._name:
            return False
        # if detected os is set and plugin os set is not empty and detected os is not in plugin os then
        # plugin does not match
        if self._os and bool(ext.plugin.os) and self._os not in ext.plugin.os:
            return False
        return True

    def _dispatch(self, ext, *args, **kwargs):
        if self._filter_func(ext):
            self._ctx.current_plugin = None
            self._ctx.info("Dispatching: '{}'".format(ext.plugin.name))
            self._ctx.post_status(ext.plugin.name)
            self._ctx.current_plugin = ext.plugin.name
            return True
        return False

    def _on_load_failure(self, manager, entry_point, exc):
        self._ctx.warning("Plugin load error: {}".format(entry_point))
        self._ctx.warning("Exception: {}".format(exc))

    def _check_plugin(self, ext, *args, **kwargs):
        attributes = ['name', 'phases', 'platforms', 'os']
        plugin = ext.plugin
        for attribute in attributes:
            if not hasattr(plugin, attribute):
                self._ctx.warning("Attribute '{}' missing in plugin class: {}".format(
                    attribute, ext.entry_point.module_name))
                return False
        return self._filter_func(ext)

    def get_package_metadata(self, name):
        try:
            meta = pkginfo.Installed(name)
        except ValueError as e:
            print(e)
            return None
        return meta

    def _get_package_names(self):
        return self.get_package_metadata().keys()

    def dispatch(self, func):

        results = []
        current_phase = self._ctx.phase
        if self._ctx.phase in auto_pre_phases:
            phase = "Pre-{}".format(self._ctx.phase)
            self.set_phase_filter(phase)
            self._ctx.info("Phase: {}".format(self._phase))
            try:
                results = self._manager.map_method(self._dispatch, func)
            except NoMatches:
                self._ctx.warning("No {} plugins found".format(phase))
            self._ctx.current_plugin = None

        self.set_phase_filter(current_phase)
        self._ctx.info("Phase: {}".format(self._phase))
        try:
            results += self._manager.map_method(self._dispatch, func)
        except NoMatches:
            self._ctx.post_status("No plugins found for phase {}".format(self._phase))
            self._ctx.error("No plugins found for phase {}".format(self._phase))
        finally:
            self._ctx.info("CSM Plugin Manager Finished")
            self._ctx.finalize()

        self._ctx.current_plugin = None
        self._ctx.success = True

        return results

    def set_platform_filter(self, platform):
        self._platform = platform

    def set_phase_filter(self, phase):
        self._phase = phase

    def set_os_filter(self, os):
        self._os = os

    def set_name_filter(self, name):
        if isinstance(name, str) or isinstance(name, unicode):
            self._name = {name}
        elif isinstance(name, list):
            self._name = set(name)
        elif isinstance(name, set):
            self._name = name
        else:
            self._name = None
Ejemplo n.º 14
0
def test_dispatch_instance_should_use_supplied_extensions():
    extensions = [test_extension, test_extension2]
    em = DispatchExtensionManager.make_test_instance(extensions)

    assert extensions == em.extensions
Ejemplo n.º 15
0
class CSMPluginManager(object):
    def __init__(self, ctx=None, invoke_on_load=True):
        self._ctx = PluginContext(ctx)
        # The context contains device information after discovery phase
        # There is no need to load plugins which does not match the family and os
        try:
            self._platform = self._ctx.family
        except AttributeError:
            self._platform = None
        try:
            self._os = self._ctx.os_type
        except AttributeError:
            self._os = None

        self._phase = None
        self._name = None
        self._vm = "xr"
        self.load(invoke_on_load=invoke_on_load)

    def load(self, invoke_on_load=True):
        self._manager = DispatchExtensionManager(
            "csm.plugin",
            self._check_plugin,
            invoke_on_load=invoke_on_load,
            invoke_args=(self._ctx, ),
            propagate_map_exceptions=True,
            on_load_failure_callback=self._on_load_failure,
        )
        self._build_plugin_list()

    def __getitem__(self, item):
        return self._manager.__getitem__(item)

    def _build_plugin_list(self):
        self.plugins = {}
        for ext in self._manager:
            self.plugins[ext.name] = {
                #  'package_name': ext.entry_point.dist.project_name,
                'package_name': ext.entry_point.module_name.split(".")[0],
                'name': ext.plugin.name,
                'description': ext.plugin.__doc__,
                'phases': ext.plugin.phases,
                'platforms': ext.plugin.platforms,
                'os': ext.plugin.os
            }

    def _filter_func(self, ext, *args, **kwargs):
        if self._platform and self._platform not in ext.plugin.platforms:
            return False
        if self._phase and self._phase not in ext.plugin.phases:
            return False
        if self._name and ext.plugin.name not in self._name:
            return False
        # if detected os is set and plugin os set is not empty and detected os is not in plugin os then
        # plugin does not match
        if self._os and bool(ext.plugin.os) and self._os not in ext.plugin.os:
            return False
        return True

    def _dispatch(self, ext, *args, **kwargs):
        if self._filter_func(ext):
            self._ctx.current_plugin = None
            self._ctx.info("Dispatching: '{}'".format(ext.plugin.name))
            self._ctx.post_status(ext.plugin.name)
            self._ctx.current_plugin = ext.plugin.name
            return True
        return False

    def _on_load_failure(self, manager, entry_point, exc):
        self._ctx.warning("Plugin load error: {}".format(entry_point))
        self._ctx.warning("Exception: {}".format(exc))

    def _check_plugin(self, ext, *args, **kwargs):
        attributes = ['name', 'phases', 'platforms', 'os']
        plugin = ext.plugin
        for attribute in attributes:
            if not hasattr(plugin, attribute):
                self._ctx.warning(
                    "Attribute '{}' missing in plugin class: {}".format(
                        attribute, ext.entry_point.module_name))
                return False
        return self._filter_func(ext)

    def get_package_metadata(self, name):
        try:
            meta = pkginfo.Installed(name)
        except ValueError as e:
            print(e)
            return None
        return meta

    def _get_package_names(self):
        return self.get_package_metadata().keys()

    def dispatch(self, func):
        try:
            self._ctx.connect()
        except ConnectionError as e:
            self._ctx.post_status(e.message)
            self._ctx.error(e.message)
            return False

        results = []
        current_phase = self._ctx.phase
        if self._ctx.phase in auto_pre_phases:
            phase = "Pre-{}".format(self._ctx.phase)
            self.set_phase_filter(phase)
            self._ctx.info("Phase: {}".format(self._phase))
            try:
                results = self._manager.map_method(self._dispatch, func)
            except NoMatches:
                self._ctx.warning("No {} plugins found".format(phase))
            self._ctx.current_plugin = None

        self.set_phase_filter(current_phase)
        self._ctx.info("Phase: {}".format(self._phase))
        try:
            results += self._manager.map_method(self._dispatch, func)
        except NoMatches:
            self._ctx.post_status("No plugins found for phase {}".format(
                self._phase))
            self._ctx.error("No plugins found for phase {}".format(
                self._phase))

        self._ctx.current_plugin = None
        self._ctx.success = True
        self._ctx.info("CSM Plugin Manager finished")
        self._ctx.disconnect()
        return results

    def set_platform_filter(self, platform):
        self._platform = platform

    def set_phase_filter(self, phase):
        self._phase = phase

    def set_os_filter(self, os):
        self._os = os

    def set_name_filter(self, name):
        if isinstance(name, str) or isinstance(name, unicode):
            self._name = {name}
        elif isinstance(name, list):
            self._name = set(name)
        elif isinstance(name, set):
            self._name = name
        else:
            self._name = None
Ejemplo n.º 16
0
class CSMPluginManager(object):

    def __init__(self, ctx=None, invoke_on_load=True):
        self._ctx = PluginContext(ctx)
        try:
            self._platform = self._ctx.family
        except AttributeError:
            self._platform = None
        try:
            self._phase = self._ctx.phase
        except AttributeError:
            self._phase = None

        self._name = None
        self._manager = DispatchExtensionManager(
            "csm.plugin",
            self._check_plugin,
            invoke_on_load=invoke_on_load,
            invoke_args=(self._ctx,),
            propagate_map_exceptions=True,
            on_load_failure_callback=self._on_load_failure,
        )
        self._build_plugin_list()

    def __getitem__(self, item):
        return self._manager.__getitem__(item)

    def _build_plugin_list(self):
        self.plugins = {}
        for ext in self._manager:
            self.plugins[ext.name] = {
                #  'package_name': ext.entry_point.dist.project_name,
                'package_name': ext.entry_point.module_name.split(".")[0],
                'name': ext.plugin.name,
                'description': ext.plugin.__doc__,
                'phases': ext.plugin.phases,
                'platforms': ext.plugin.platforms,
            }

    def _filter_func(self, ext, *args, **kwargs):
        print(ext.plugin.name)
        if self._platform and self._platform not in ext.plugin.platforms:
            return False
        if self._phase and self._phase not in ext.plugin.phases:
            return False
        if self._name and ext.plugin.name not in self._name:
            return False
        return True

    def _dispatch(self, ext, *args, **kwargs):
        if self._filter_func(ext):
            self._ctx.current_plugin = None
            self._ctx.info("Dispatching: '{}'".format(ext.plugin.name))
            self._ctx.current_plugin = ext.plugin.name
            return True
        return False

    def _on_load_failure(self, manager, entry_point, exc):
        self._ctx.warning("Plugin load error: {}".format(entry_point))
        self._ctx.warning("Exception: {}".format(exc))

    def _check_plugin(self, ext, *args, **kwargs):
        attributes = ['name', 'phases', 'platforms']
        plugin = ext.plugin
        for attribute in attributes:
            if not hasattr(plugin, attribute):
                self._ctx.warning("Attribute '{}' missing in plugin class".format(attribute))
        return self._filter_func(ext)

    def _find_plugin_packages(self):
        packages = set()
        for ext in self._manager:
            dist = ext.entry_point.dist
            print(dist.__dict__)
        return list(packages)

    def get_package_metadata(self, name):
        try:
            meta = pkginfo.Installed(name)
        except ValueError as e:
            print(e)
            return None
        return meta

    def get_package_names(self):
        return self.get_package_metadata().keys()

    def dispatch(self, func):
        try:
            self._ctx.connect()
        except ConnectionError as e:
            self._ctx.post_status(e.message)
            self._ctx.error(e.message)
            return False

        results = []
        if self._phase in auto_pre_phases:
            current_phase = self._phase
            phase = "Pre-{}".format(self._phase)
            self.set_phase_filter(phase)
            try:
                results = self._manager.map_method(self._dispatch, func)
            except NoMatches:
                self._ctx.warning("No {} plugins found".format(phase))
            self._ctx.current_plugin = None
            self.set_phase_filter(current_phase)

        try:
            results += self._manager.map_method(self._dispatch, func)
        except NoMatches:
            self._ctx.post_status("No plugins found for phase {}".format(self._phase))
            self._ctx.error("No plugins found for phase {}".format(self._phase))

        self._ctx.current_plugin = None
        self._ctx.success = True
        return results

    def set_platform_filter(self, platform):
        self._platform = platform

    def set_phase_filter(self, phase):
        self._ctx.info("Phase: {}".format(phase))
        self._phase = phase

    def set_name_filter(self, name):
        if isinstance(name, str) or isinstance(name, unicode):
            self._name = set((name,))
        elif isinstance(name, list):
            self._name = set(name)
        elif isinstance(name, set):
            self._name = name
        else:
            self._name = None