Exemple #1
0
def load_config(*filenames):
    defaults = {}
    for get_defaults in plugins.get(CONFIG_INI_DEFAULTS_PLUGIN):
        defaults.update(get_defaults())
    
    parser = SafeConfigParser(defaults=defaults)
    
    for filename in filenames:
        conf_file = open(filename, 'r')
        parser.readfp(conf_file)

    config = {}
    for section in parser.sections():
        for key, val in parser.items(section):
            if section == 'main': 
                config[key] = val
            else: 
                config['%s.%s' % (section, key)] = val

    # there may need to be a pre-configuration step here to 
    # enable / disable plugins based on configuration before
    # running config plugins.

    for parser in plugins.get(CONFIG_INI_PARSER_PLUGIN):
        parser(config)

    return config
Exemple #2
0
 def __call__(self, mailboxes=None, update_all=False):
     """
     run couch db compaction on each mailbox and design doc
     *sequentially*
     """
     for mb in self._get_mailboxes(mailboxes, get_all=update_all):
         log.info('Compacting mailbox %s' % mb.name)
         mb.compact() # compact the database 
         info = mb.info()
         while (info.get('compact_running', False) == True):
             sleep(3)
             info = mb.info()
         log.info("Finished compacting mailbox %s" % mb.name)
         
         for ddoc in plugins.get(DESIGN_DOC_PLUGIN):
             ddid = ddoc['_id'][len('_design/'):]
             log.info("Compacting mailbox %s / view %s" % (mb.name, ddid))
             try: 
                 mb.compact(ddid)
             
                 # grab something like:
                 # http://localhost:5984/databasename/_design/test/_info
                 # Ack ! can't ask for this due to wierd quoting behavior of 
                 # couchdb library -- use workaround
                 status, headers, data = get_json_raw_url(mb, ['_design', ddid, '_info'])
                 while data.get('view_index', {}).get('compact_running', False) == True: 
                     sleep(3)
                     status, headers, data = get_json_raw_url(mb, ['_design', ddid, '_info'])
                 log.info("Finished compacting mailbox %s / view %s" % (mb.name, ddid))
             except KeyboardInterrupt: 
                 log.info("Exiting at user request.")
                 return
             except: 
                 log.error("Error compacting mailbox %s / view %s: %s" % (mb.name, ddid, traceback.format_exc()))
Exemple #3
0
 def _update_subscription(self, mailbox, sub):
     for handler in plugins.get(SUBSCRIPTION_UPDATE_HANDLER):
         if handler(mailbox, sub, self.config) == True:
             refresh_views(mailbox)
             return True
     log.info('%s: no update handler for subscription "%s" of type "%s"' % 
              (mb.name, sub.id, sub.subscription_type))
     return False
Exemple #4
0
 def create_type(cls, typename):
     instance = None
     if typename:
         for create in plugins.get(cls.SUBTYPE_PLUGIN):
             instance = create(typename)
             if instance is not None:
                 break
     if instance is None:
         instance = cls()
     return instance
Exemple #5
0
def refresh_views(mb):
    for dd in plugins.get(DESIGN_DOC_PLUGIN):
        if "views" in dd and len(dd["views"].keys()) > 0:
            first_view = dd["views"].keys()[0]
            view_url = "%s/%s" % (dd["_id"], first_view)
            log.info("Refreshing views in %s..." % dd["_id"])
            try:
                mb.view(view_url, {"count": 0})
                # aaaand wait...
            except:
                log.error("failed to refresh view %s: %s" % (view_url, traceback.format_exc()))
Exemple #6
0
 def __call__(self, command_name=None):
     if command_name is None: 
         get_basic_option_parser().print_usage()
         print "Commands:"
         for Command in sorted(plugins.get(COMMANDLINE_PLUGIN), key=attrgetter('command_name')):
             print "  %s %s" % (Command.command_name.ljust(20), Command.description)
     else: 
         Command = find_command_type(command_name)
         if Command is None:
             print_unknown_command(command_name)
             return
         else:
             command = Command(self.config)
             command.print_usage()
Exemple #7
0
def load_config(*filenames):
    defaults = {}
    for get_defaults in plugins.get(CONFIG_INI_DEFAULTS_PLUGIN):
        defaults.update(get_defaults())
    
    parser = SafeConfigParser(defaults=defaults)
    
    for filename in filenames:
        conf_file = open(filename, 'r')
        parser.readfp(conf_file)

    config = {}
    for section in parser.sections():
        for key, val in parser.items(section):
            if section == 'main': 
                config[key] = val
            else: 
                config['%s.%s' % (section, key)] = val

    for parser in plugins.get(CONFIG_INI_PARSER_PLUGIN):
        parser(config)

    return config
Exemple #8
0
    def __call__(self):
        class ShellCommands:
            pass

        cmds = ShellCommands()
        ShellCommands.__doc__ = "Commands:"
        for Command in sorted(plugins.get(COMMANDLINE_PLUGIN), key=attrgetter('command_name')):
            # help is skipped because it relates to the command line option
            # info for the commands.  The built-in python help should be 
            # used in the shell.
            if (not hasattr(Command, '__call__') or 
                Command == HelpCommand or 
                isinstance(self, Command)):
                continue
            
            shell_cmd = Command(self.config).__call__
            shell_cmd.__func__.__name__ = Command.command_name
            setattr(cmds, Command.command_name, shell_cmd)
            ShellCommands.__doc__ += "\n  "
            ShellCommands.__doc__ += Command.command_name.ljust(20)
            ShellCommands.__doc__ += Command.description
        ShellCommands.__doc__ += "\n\nType: help(cmds.<function>) for more info"
        
        locs = {'config': self.config, 'cmds': cmds }
        banner_header = 'RadarPost Interactive Shell\n'
        banner_footer = '\n\nYou may access the current config as "config"'
        banner_footer += '\nCLI commands are available as "cmds.<command>"'
        banner_footer += '\nType: help(cmds) for more info'
        try:
            # try to use IPython if possible
            from IPython.Shell import IPShellEmbed
            shell = IPShellEmbed(argv=sys.argv)
            banner = banner_header + shell.IP.BANNER + banner_footer
            shell.set_banner(banner)
            shell(local_ns=locs, global_ns={})
        except ImportError:
            import code
            pyver = 'Python %s' % sys.version
            banner = banner_header +  pyver + banner_footer

            shell = code.InteractiveConsole(locals=locs)
            try:
                import readline
            except ImportError:
                pass
            try:
                shell.interact(banner)
            finally:
                pass
Exemple #9
0
def sync_mailbox(db):
    """
    update database design document and other
    metadata.  This operation unconditionally
    clobbers the current design document in the 
    database.
    """
    if not is_mailbox(db):
        raise PreconditionFailed("database %s is not a mailbox" % db.name)

    for dd in plugins.get(DESIGN_DOC_PLUGIN):
        dd = copy.deepcopy(dd)
        cur = db.get(dd["_id"])
        if cur:
            dd["_rev"] = cur["_rev"]
        db[dd["_id"]] = dd
Exemple #10
0
def _make_template_env(config):
    loader = ChoiceLoader([
        PackageLoader(package) for package in app_ids(config)
    ])

    def escape_ml(template_name):
        if template_name is None:
            return False
        return (template_name.endswith('.html') or 
                template_name.endswith('.xml'))

    env = Environment(loader=loader,
                      autoescape=escape_ml,
                      extensions=['jinja2.ext.autoescape'])
    for filt in plugins.get(TEMPLATE_FILTERS):
        env.filters[filt.__name__] = filt

    return env
Exemple #11
0
def _get_atom_renderer(message, request):
    for renderer in plugins.get(ATOM_RENDERER_PLUGIN):
        r = renderer(message, request)
        if r is not None:
            return r
    return None
Exemple #12
0
def find_command_type(command_name):
    for Command in plugins.get(COMMANDLINE_PLUGIN):
        if command_name == Command.command_name:
            return Command
    return None
Exemple #13
0
 def __init__(self, request, ctx):
     self.update(ctx)
     self.request = request
     for proc in plugins.get(TEMPLATE_CONTEXT_PROCESSORS):
         proc(request, self)