def _register_handlers(): """ Walk thru the handlers specified in ``settings.NOTIFICATION_HANDLERS`` and register them internally. Any built-in event handlers need to be defined above this function. """ global HANDLERS_REGISTERED from trigger.conf import settings for handler_path in settings.NOTIFICATION_HANDLERS: # Get the module and func name try: h_module, h_funcname = handler_path.rsplit('.', 1) except ValueError: raise exceptions.ImproperlyConfigured("%s isn't a handler module" % handler_path) # Import the module and get the module object try: mod = import_module(h_module) except ImportError as err: raise exceptions.ImproperlyConfigured('Error importing handler %s: "%s"' % (h_module, err)) # Get the handler function try: handler = getattr(mod, h_funcname) except AttributeError: raise exceptions.ImproperlyConfigured('Handler module "%s" does not define a "%s" function' % (h_module, h_funcname)) # Register the handler function if handler not in _registered_handlers: _registered_handlers.append(handler) HANDLERS_REGISTERED = True
def find_data_loader(loader): """ Given a ``loader`` string/list/tuple, try to unpack, load it, and return the callable loader object. If ``loader`` is specified as string, treat it as the fully-qualified Python path to the callable Loader object. Optionally, if `loader`` is a list/tuple, the first item in the tuple should be the Loader's module path, and subsequent items are passed to the Loader object during initialization. This could be useful in initializing a custom Loader for a database backend, for example. :param loader: A string represnting the Python path to a Loader object, or list/tuple of loader path and args to pass to the Loader. """ if isinstance(loader, (tuple, list)): loader, args = loader[0], loader[1:] else: args = [] log.msg("BUILDING LOADER: %s; WITH ARGS: %s" % (loader, args)) err_template = "Error importing data source loader %s: '%s'" if isinstance(loader, basestring): module, attr = loader.rsplit('.', 1) try: mod = import_module(module) except ImportError as err: raise ImproperlyConfigured(err_template % (loader, err)) try: DataLoader = getattr(mod, attr) except AttributeError as err: raise ImproperlyConfigured(err_template % (loader, err)) if hasattr(DataLoader, 'load_data_source'): func = DataLoader(*args) else: # Try loading module the old-fashioned way where string is full # path to callabale. if args: raise ImproperlyConfigured( "Error importing data source loader %s: Can't pass arguments to function-based loader!" % loader) func = DataLoader if not func.is_usable: import warnings warnings.warn( "Your NETDEVICES_LOADERS setting includes %r, but your Python installation doesn't support that type of data loading. Consider removing that line from NETDEVICES_LOADERS." % loader) return None else: return func else: raise ImproperlyConfigured( 'Loader does not define a "load_data" callable data source loader.' )
def find_data_loader(loader): """ Given a ``loader`` string/list/tuple, try to unpack, load it, and return the callable loader object. If ``loader`` is specified as string, treat it as the fully-qualified Python path to the callable Loader object. Optionally, if `loader`` is a list/tuple, the first item in the tuple should be the Loader's module path, and subsequent items are passed to the Loader object during initialization. This could be useful in initializing a custom Loader for a database backend, for example. :param loader: A string represnting the Python path to a Loader object, or list/tuple of loader path and args to pass to the Loader. """ if isinstance(loader, (tuple, list)): loader, args = loader[0], loader[1:] else: args = [] log.msg("BUILDING LOADER: %s; WITH ARGS: %s" % (loader, args)) err_template = "Error importing data source loader %s: '%s'" if isinstance(loader, basestring): module, attr = loader.rsplit('.', 1) try: mod = import_module(module) except ImportError as err: raise ImproperlyConfigured(err_template % (loader, err)) try: DataLoader = getattr(mod, attr) except AttributeError as err: raise ImproperlyConfigured(err_template % (loader, err)) if hasattr(DataLoader, 'load_data_source'): func = DataLoader(*args) else: # Try loading module the old-fashioned way where string is full # path to callabale. if args: raise ImproperlyConfigured("Error importing data source loader %s: Can't pass arguments to function-based loader!" % loader) func = DataLoader if not func.is_usable: import warnings warnings.warn("Your NETDEVICES_LOADERS setting includes %r, but your Python installation doesn't support that type of data loading. Consider removing that line from NETDEVICES_LOADERS." % loader) return None else: return func else: raise ImproperlyConfigured('Loader does not define a "load_data" callable data source loader.')
def xmlrpc_add_handler(self, mod_name, task_name, force=False): """ Add a handler object from a remote call. """ module = None if mod_name in sys.modules: # Check if module is already loaded if force: log.msg("Forcing reload of handler: %r" % task_name) # Allow user to force reload of module module = reload(sys.modules[mod_name]) else: # If not forcing reload, don't bother with the rest log.msg("%r already loaded" % mod_name) return None else: log.msg("Trying to add handler: %r" % task_name) try: module = importlib.import_module(mod_name, __name__) except NameError as msg: log.msg('NameError: %s' % msg) except: pass if not module: log.msg(" Unable to load module: %s" % mod_name) return None else: handler = getattr(module, 'xmlrpc_' + task_name) # XMLRPC methods will not accept kwargs. Instead, we pass 2 position # args: args and kwargs, to a shell method (dummy) that will explode # them when sending to the user defined method (handler). def dummy(self, args, kwargs): return handler(*args, **kwargs) # TODO (jathan): Make this work!! # This just simply does not work. I am not sure why, but it results in a # "<Fault 8001: 'procedure config_device not found'>" error! # # Bind the dummy shell method to TriggerXMLRPCServer. The function's # # name will be used to map it to the "dummy" handler object. # dummy.__name__ = task_name # self.addHandler(dummy) # This does work. # Bind the dummy shell method to TriggerXMLRPCServer as 'xmlrpc_' + task_name setattr(TriggerXMLRPCServer, 'xmlrpc_' + task_name, dummy)
def _register_handlers(): """ Walk thru the handlers specified in ``settings.NOTIFICATION_HANDLERS`` and register them internally. Any built-in event handlers need to be defined above this function. """ global HANDLERS_REGISTERED from trigger.conf import settings for handler_path in settings.NOTIFICATION_HANDLERS: # Get the module and func name try: h_module, h_funcname = handler_path.rsplit('.', 1) except ValueError: raise exceptions.ImproperlyConfigured("%s isn't a handler module" % handler_path) # Import the module and get the module object try: mod = import_module(h_module) except ImportError as err: raise exceptions.ImproperlyConfigured( 'Error importing handler %s: "%s"' % (h_module, err)) # Get the handler function try: handler = getattr(mod, h_funcname) except AttributeError: raise exceptions.ImproperlyConfigured( 'Handler module "%s" does not define a "%s" function' % (h_module, h_funcname)) # Register the handler function if handler not in _registered_handlers: _registered_handlers.append(handler) HANDLERS_REGISTERED = True