コード例 #1
0
ファイル: __init__.py プロジェクト: spikelab/vapourword
def run(args=None):
    """ console app entry point """

    import imp
    import optparse

    parser = optparse.OptionParser()
    parser.add_option("-i", "--input-plugin", 
            dest="input_plugin", 
            default="stdin", 
            metavar="PLUGIN[:ARG]",
    )
    parser.add_option("-o", "--output-plugin", 
            dest="output_plugin", 
            default="stdout", 
            metavar="PLUGIN[:ARG]",
    )
    parser.add_option("-s", "--split-plugin", 
            dest="split_plugin", 
            default="word", 
            metavar="PLUGIN[:ARG]",
    )
    parser.add_option("-f", "--filter-plugin", 
            dest="filter_plugin", 
            default="common_words", 
            metavar="PLUGIN[:ARG]",
    )
    parser.add_option("-w", "--weight-plugin", 
            dest="weight_plugin", 
            default="occurance", 
            metavar="PLUGIN[:ARG]",
    )

    parser.add_option("-d", "--plugin-path",
            dest="plugin_paths",
            default=[],
            action="append",
            metavar="PATH",
            help="extra paths to search when loading plugins - you can specify this option more than once",
    )

    parser.add_option("-m", "--plugin-module",
            dest="plugin_modules",
            default=[],
            action="append",
            metavar="MODULE",
            help="extra modules to load plugins from - you can specify this option more than once",
    )

    parser.add_option("-L", "--list-plugins", 
            dest="list_plugins",
            default=False,
            action="store_true",
    )
      

    (options, args) = parser.parse_args(args)

    # load any modules specified on the command line 
    Plugins.load_modules(options.plugin_modules, paths=options.plugin_paths)

    # print a list of plugins 
    if options.list_plugins:
        for typ in sorted(Plugins.all.keys()):
            print "%s:" % typ
            for name in sorted(Plugins.all[typ].keys()):
                print "    %s" % name, 
                f = Plugins.all[typ][name].__doc__
                if f:
                    print "- %s" % f.split('\n')[0].strip()
                else:
                    print 
        return 

    # not quite sure on the best way to pass arguments to plugins 
    # so this lets you do 
    # -<opt> <plugin>:arg_string
    # arg string will be passed to the plugin function 
    def get_plugin(typ, s):
        p = s.split(':', 1)
        plugin = p[0]
        if len(p) == 2:
            arg = p[1]
        else:
            arg = None
            
        func = Plugins.all[typ].get(plugin)
        if func:
            return func(arg)
        else:
            print >>sys.stderr, "%s plugin %s not found" % (typ, plugin)
            return None

    # get all our functions ready to do stuff - we'll have 
    # the inner functions of the plugins here - or None if the plugin 
    # wasnt found/loaded  

    funcs = {}

    for name in ('input', 'output', 'split', 'filter', 'weight'):
        try:
            funcs[name] = get_plugin(name, getattr(options, '%s_plugin' % name))
        except Plugins.ArgumentError, e:
            print >>sys.stderr, "%s: %s" % (type(e).__name__, str(e))
            return 1
        if name not in ('split', 'filter') and funcs[name] is None:
            print >>sys.stderr, "%s plugin cannot be None" % name
            return 1
コード例 #2
0
ファイル: __init__.py プロジェクト: spikelab/vapourword
import sys
import os
import functools
from string import punctuation
from vapourword.plugins import Plugins


Plugins.register_types('input', 'output', 'split', 'filter', 'weight')


@Plugins.register_input
def input_file(arg):
    """ read input from a file """

    if not arg:
        raise Plugins.ArgumentError("file input plugin needs a filename argument")

    def input():
        return open(arg).read()
    return input

@Plugins.register_input
def input_stdin(arg):
    """ read input from stdin """
    def input():
        return sys.stdin.read()
    return input


@Plugins.register_output
def output_stdout(arg):