def load_all_plugins(): """ Attempt to load all enabled plugins. Passes the existing config and options object to each plugin and allows them to add/update each. """ for res in run_hooks('pre_plugins_hook'): pass # No result expected namespaces['root'].config['enabled_plugins'] = get_enabled_plugins() for plugin in namespaces['root'].config['enabled_plugins']: load_plugin(plugin) for (namespace, res) in run_hooks('options_hook'): for opt in res._get_all_options(): if opt.get_opt_string() == '--help': pass else: if namespaces.has_key(namespace): namespaces[namespace].options.add_option(opt) else: log.warning("namespace '%s' doesn't exist!" % namespace) for res in run_hooks('post_plugins_hook'): pass # No result expected
def run_command(cmd_name=None, ignore_conflicts=False): """ Run the command or namespace-subcommand as defined by the 'expose()' decorator used on a Controller function. Keyword arguments: cmd_name The command name as store in the global 'namespaces'. For example, namespaces['root'].commands['cmd_name']. """ log.debug("processing passed command '%s'", cmd_name) # bit of a hack... but if cmd_name starts with - then no command passed if cmd_name.startswith('-'): cmd_name = 'default' orig_cmd = cmd_name cmd_name = re.sub('-', '_', cmd_name) if cmd_name in namespaces.keys(): namespace = cmd_name else: namespace = 'root' # Parse cli options and arguments (cli_opts, cli_args) = parse_options(namespace=namespace, ignore_conflicts=ignore_conflicts) # Run all post options hooks for res in run_hooks('post_options_hook', cli_opts, cli_args): pass # Doesn't expect a result # If it isn't the root namespace, then the first arg is the namespace # and the second is the actual command. if namespace == 'root': actual_cmd = cmd_name else: try: actual_cmd = re.sub('-', '_', cli_args[1]) except IndexError: if namespaces[namespace].commands.has_key('default'): actual_cmd = 'default' else: raise CementArgumentError, \ "%s is a namespace* " % namespace + \ "which requires a sub-command. See " + \ "'%s --help'" % namespace if namespaces[namespace].commands.has_key(actual_cmd): cmd = namespaces[namespace].commands[actual_cmd] log.debug("executing command '%s'" % actual_cmd) (res, out_txt) = run_controller_command(cmd['controller_namespace'], cmd['func'], cli_opts, cli_args) return (res, out_txt) else: raise CementArgumentError, "Unknown command '%s', see --help?" % \ orig_cmd
def ex1(self, cli_opts, cli_args): """ This is how to expose a subcommand because it will be under the 'example' namespace. You would access this subcommand as: $ satcli example ex1 """ # You can get the root application config like this: config = get_config("root") # Or you can get your example namespace config like this: config = get_config("example") # You can print or log output however you like since this function # does not render out to a template. # Commands are all passed the cli_opts, cli_args from the command line. # So if you have added cli options in your satcli.bootstrap.example # file, you could access them here as: # # cli_opts.<your_option> # cli_args[0] # first argument *after* your command # # Here we show how to run a hook that we've defined in # satcli.bootstrap.example: for res in run_hooks("my_example_hook"): print res # This command has no template, but if we return something we # can still access the json output via --json. return dict(foo="bar")
def test_my_example_hook(): ok_(hooks.has_key('my_example_hook')) hook_results = [] for res in run_hooks('my_example_hook'): hook_results.append(res) # return results are the weight of the hook and should be in numerical # order res = hook_results = [-100, 0, 99] ok_(res)
def test_my_example_hook(): ok_(hooks.has_key("my_example_hook")) hook_results = [] for res in run_hooks("my_example_hook"): hook_results.append(res) # return results are the weight of the hook and should be in numerical # order res = hook_results = [-100, 0, 99] ok_(res)
def interactive_ircbot_process_hook(config, log, irc): """ This process hook listens on the IRC channel, and responds to interactive requests. NOTE: only one process can do regular 'polls' on the channel. """ while True: res = irc.poll() if res: for hook in run_hooks('ircbot_parsemsg_hook', config, log, irc, res): pass #(from_nick, from_chan, msg, dest) = res sleep(1)
def test_run_hook_that_does_not_exist(): for res in run_hooks('bogus_hook'): pass
def test_run_hook_that_does_not_exist(): for res in run_hooks("bogus_hook"): pass
# Setup logging for console and file setup_logging(to_console=namespaces['root'].config['log_to_console'], clear_loggers=clear_loggers) log = get_logger(__name__) log.debug('logging initialized') log.debug('setup app per the following configs: %s' % \ namespaces['root'].config['config_files']) define_default_hooks() define_default_handler_types() register_default_handlers() __import__("%s.bootstrap" % namespaces['root'].config['app_module'], globals(), locals(), ['root']) for res in run_hooks('post_bootstrap_hook'): pass # load all plugins load_all_plugins() # Allow plugins to add config validation for the global namespace for res in run_hooks('validate_config_hook', config=namespaces['root'].config): pass # Merge namespaces under root dict for nam in namespaces: if nam != 'root': namespaces['root'].config[nam] = get_config(nam)