예제 #1
0
    def run_filters(self, filters, files):
        # If all given filters were not found or had wrong argument count, do nothing
        if not filters: return
        
        reader_args = {}
        if self.reader_class_name:
            try:
                reader_class = globals()[self.reader_class_name]
                info("Using reader class %s.", self.reader_class_name)
                
                reader_args['reader_class'] = reader_class
            except KeyError:
                raise RuntimeError("Reader class %s not found." % self.reader_class_name)
        
        for file in self.transform(files):
            if self.is_pair_spec(file):
                meta_reader = PairedReader
            else:
                meta_reader = DirFileGuessReader
                
            try:
                self.last_exceptions = []
                
                for derivation_bundle in meta_reader(file, verbose=self.verbose, **reader_args):
                    if self.verbose: info("Processing %s...", derivation_bundle.label())
                    try:
                        for filter in filters:
                            filter.context = derivation_bundle

                        if filter.accept_leaf is not None:
                            for leaf in leaves(derivation_bundle.derivation):
                                for filter in filters:
                                    filter.accept_leaf(leaf)

                                    if filter.accept_comb_and_slash_index is not None:
                                        try:
                                            for slash_index, comb in enumerate(applications_per_slash(leaf)):
                                                filter.accept_comb_and_slash_index(leaf, comb, slash_index)
                                        except AttributeError: # TODO: hacky and inefficient, need this to work for PTB too
                                            pass

                        for filter in filters:
                            filter.accept_derivation(derivation_bundle)
                            filter.context = None
                            
                    except IOError, e:
                        # If output is going to a pager, and the user requests an interrupt (^C)
                        # the filter fails with IOError: Broken pipe
                        # In that case, running filters on further derivations will continue to
                        # lead to 'Broken pipe', so just bail out
                        if e.errno == errno.EPIPE: return
                            
                    except Exception, e:
                        self.last_exceptions.append( (derivation_bundle, sys.exc_info()) )
                        
                        if self._break_on_exception:
                            raise FilterException(e, None)
예제 #2
0
 def accept_leaf(self, leaf):
     result = find(lambda (c, i): (c == leaf.cat), self.cats_to_split)
     if not (result and leaf.cat.is_complex()): return
     
     cat, slash_index = result
     
     appls = list(applications_per_slash(leaf, False))
     try:
         appl = appls[slash_index]
         if str(appl).endswith('comp'):
             if self.mode_on_slash(leaf.cat, slash_index) != COMP:
                 self.fix_cat_for(leaf, slash_index, "comp")
                 
         elif str(appl).endswith('appl'):
             if self.mode_on_slash(leaf.cat, slash_index) != APPLY:
                 self.fix_cat_for(leaf, slash_index, "apply")
                 
     except IndexError:
         pass
예제 #3
0
    def run_filters(self, filters, files):
        # If all given filters were not found or had wrong argument count, do nothing
        if not filters: return

        reader_args = {}
        if self.reader_class_name:
            try:
                reader_class = globals()[self.reader_class_name]
                info("Using reader class %s.", self.reader_class_name)

                reader_args['reader_class'] = reader_class
            except KeyError:
                raise RuntimeError("Reader class %s not found." %
                                   self.reader_class_name)

        for file in self.transform(files):
            if self.is_pair_spec(file):
                meta_reader = PairedReader
            else:
                meta_reader = DirFileGuessReader

            try:
                self.last_exceptions = []

                for derivation_bundle in meta_reader(file,
                                                     verbose=self.verbose,
                                                     **reader_args):
                    if self.verbose:
                        info("Processing %s...", derivation_bundle.label())
                    try:
                        for filter in filters:
                            filter.context = derivation_bundle

                        if filter.accept_leaf is not None:
                            for leaf in leaves(derivation_bundle.derivation):
                                for filter in filters:
                                    filter.accept_leaf(leaf)

                                    if filter.accept_comb_and_slash_index is not None:
                                        try:
                                            for slash_index, comb in enumerate(
                                                    applications_per_slash(
                                                        leaf)):
                                                filter.accept_comb_and_slash_index(
                                                    leaf, comb, slash_index)
                                        except AttributeError:  # TODO: hacky and inefficient, need this to work for PTB too
                                            pass

                        for filter in filters:
                            filter.accept_derivation(derivation_bundle)
                            filter.context = None

                    except IOError, e:
                        # If output is going to a pager, and the user requests an interrupt (^C)
                        # the filter fails with IOError: Broken pipe
                        # In that case, running filters on further derivations will continue to
                        # lead to 'Broken pipe', so just bail out
                        if e.errno == errno.EPIPE: return

                    except Exception, e:
                        self.last_exceptions.append(
                            (derivation_bundle, sys.exc_info()))

                        if self._break_on_exception:
                            raise FilterException(e, None)