def run(self, filters_to_run, files): '''Performs a processing run, given a list of filter names to run, and a list of file specifiers.''' filters = [] for filter_name, args in filters_to_run: # For a no-args switch, optparse passes in None; we substitute an empty tuple for # consistency if not args: args = () try: filter_class = self.available_filters_dict[filter_name] actual, expected = len(args), get_argcount_for_method(filter_class.__init__) if actual != expected: warn("Skipping filter %s; %d arguments given, %d expected.", filter_name, actual, expected) continue filters.append(filter_class(*args)) except KeyError: err("No filter with name `%s' found.", filter_name) # convert short notation in file specifiers to proper paths def expand_short_notation(fn): # short notation is # corpus:ss,dd,deriv -> corpus/chtb_ssdd.fid:deriv m = re.match(r'([^:]+):(\d+),(\d+),(\d+)', fn) if m: corpus_dir, sec, doc, deriv = m.groups() return os.path.join(corpus_dir, 'chtb_%02d%02d.fid:%d' % (int(sec), int(doc), int(deriv))) return fn files = [expand_short_notation(file) for file in files] self.run_filters(filters, files)
def do_run(self, args): '''Runs the given filter with the given arguments on the working set.''' args = args.split() if not args: return filter_name = args.pop(0) if not filter_name: # no filter with the requested switch was found return if filter_name.startswith('--'): # a long option name was given filter_name = self.get_filter_by_switch(filter_name) elif filter_name.startswith('-'): # a short option name was given filter_name = self.get_filter_by_switch(filter_name) # Special case: for a one-arg filter any number of arguments are treated # as a single argument. if (filter_name in self.tracer and get_argcount_for_method(self.tracer[filter_name].__init__) == 1): filter_args = (' '.join(args), ) elif args is not None: filter_args = args else: filter_args = None def action(): self.tracer.run( [(filter_name, filter_args)], self.files ) print self.redirecting_stdout(action, filter_name, filter_args)
def add_filter_to_optparser(parser, filter): '''Given a filter, this registers it with the option parser, allowing it to be selected on the command line.''' argcount = get_argcount_for_method(filter.__init__) opt_dict = { 'help': filter.__doc__, # Help string is the filter docstring 'dest': filter.long_opt, # Destination variable is the same as the long option name 'metavar': filter.arg_names,# Metavar names are supplied by the filter 'action': 'callback', 'callback': run_builtin_filter, 'callback_args': (filter.__name__, ) # Pass in the filter's class name } # If the filter expects arguments, set the correct number of arguments and assume that it takes # string arguments (for consistency). if argcount > 0: opt_dict['nargs'] = argcount opt_dict['type'] = 'string' parser.add_option("-" + filter.opt, "--" + filter.long_opt, **opt_dict)
def run(self, filters_to_run, files): '''Performs a processing run, given a list of filter names to run, and a list of file specifiers.''' filters = [] for filter_name, args in filters_to_run: # For a no-args switch, optparse passes in None; we substitute an empty tuple for # consistency if not args: args = () try: filter_class = self.available_filters_dict[filter_name] actual, expected = len(args), get_argcount_for_method( filter_class.__init__) if actual != expected: warn( "Skipping filter %s; %d arguments given, %d expected.", filter_name, actual, expected) continue filters.append(filter_class(*args)) except KeyError: err("No filter with name `%s' found.", filter_name) # convert short notation in file specifiers to proper paths def expand_short_notation(fn): # short notation is # corpus:ss,dd,deriv -> corpus/chtb_ssdd.fid:deriv m = re.match(r'([^:]+):(\d+),(\d+),(\d+)', fn) if m: corpus_dir, sec, doc, deriv = m.groups() return os.path.join( corpus_dir, 'chtb_%02d%02d.fid:%d' % (int(sec), int(doc), int(deriv))) return fn files = [expand_short_notation(file) for file in files] self.run_filters(filters, files)
def add_filter_to_optparser(parser, filter): '''Given a filter, this registers it with the option parser, allowing it to be selected on the command line.''' argcount = get_argcount_for_method(filter.__init__) opt_dict = { 'help': filter.__doc__, # Help string is the filter docstring 'dest': filter. long_opt, # Destination variable is the same as the long option name 'metavar': filter.arg_names, # Metavar names are supplied by the filter 'action': 'callback', 'callback': run_builtin_filter, 'callback_args': (filter.__name__, ) # Pass in the filter's class name } # If the filter expects arguments, set the correct number of arguments and assume that it takes # string arguments (for consistency). if argcount > 0: opt_dict['nargs'] = argcount opt_dict['type'] = 'string' parser.add_option("-" + filter.opt, "--" + filter.long_opt, **opt_dict)
def LongTemplate(filter_name, filter): return ("\t%s (%s)\n\t\t(%d args, -%s, --%s%s)" % (filter_name, filter.__module__, get_argcount_for_method( filter.__init__), filter.opt, filter.long_opt, (' ' + filter.arg_names) if filter.arg_names else ''))
def LongTemplate(filter_name, filter): return ("\t%s (%s)\n\t\t(%d args, -%s, --%s%s)" % (filter_name, filter.__module__, get_argcount_for_method(filter.__init__), filter.opt, filter.long_opt, (' '+filter.arg_names) if filter.arg_names else ''))