Esempio n. 1
0
 def process_bundle(self, bundle):
     "For each bundle, copy the tree on the given layer in the given zone to another zone."
     if not bundle.has_zone(self.source_language, self.source_selector):
         raise RuntimeException('Bundle does not have a zone at ' +
                                self.source_language + ', ' +
                                self.source_selector)
     source_zone = bundle.get_zone(self.source_language,
                                   self.source_selector)
     target_zone = bundle.get_or_create_zone(self.language, self.selector)
     if not source_zone.has_tree(self.layer):
         raise RuntimeException('Source zone does not have a tree at ' +
                                self.layer + '-layer')
     source_tree = source_zone.get_tree(self.layer)
     target_tree = target_zone.create_tree(self.layer)
     self.copy_subtree(source_tree, target_tree)
Esempio n. 2
0
 def remove(self):
     "Remove the node from the tree."
     if self.get_children():
         raise RuntimeException('Cannot remove a node with children:' +
                                self.id)
     self.parent = None
     self.document.remove_node(self.id)
Esempio n. 3
0
 def process_bundle(self, bundle):
     """\
     Process a bundle. Default behavior is to process the zone
     according to the current language and  selector.
     """
     if self.language is None:
         raise RuntimeException('Undefined language')
     # select the zone and process it
     self.process_zone(bundle.get_zone(self.language, self.selector))
Esempio n. 4
0
 def __filter_features(self, data, classes=None):
     """\
     Filter features according to the pre-selected filter. Return the
     same set of features if the filter is not set.
     """
     if self.feature_filter is None:
         return data
     if not self.feature_filter_trained:
         if classes is None:
             raise RuntimeException('Classes must be given to ' +
                                    'train a feature filter!')
         self.feature_filter.fit(data, classes)
         self.feature_filter_trained = True
     return self.feature_filter.transform(data)
Esempio n. 5
0
 def parent(self, value):
     "Change the parent of the current node."
     # TODO possibly implement moving across documents
     # (would require new ID indexing)
     if value is not None and self.__document != value.__document:
         raise RuntimeException('Cannot move nodes across documents.')
     # filter original parent's children
     if self.__parent:
         self.__parent.__children = [
             child for child in self.__parent.__children if child != self
         ]
     # set new parent and update its children, set new root
     self.__parent = value
     if self.__parent:
         self.__parent.__children.append(self)
         self.__root = self.__parent.__root
     else:
         self.__root = self
Esempio n. 6
0
 def _process_switches(self, nodes, add_self, ordered, preceding_only,
                       following_only):
     """Process all variants on a node list:
     add self, order, filter out only preceding or only following ones."""
     if preceding_only and following_only:
         raise RuntimeException('Cannot return preceding_only ' +
                                'and following_only nodes')
     if preceding_only or following_only:
         ordered = True
     if add_self:
         nodes.append(self)
     # filtering
     if preceding_only:
         nodes = filter(lambda node: node < self, nodes)
     elif following_only:
         nodes = filter(lambda node: node > self, nodes)
     # sorting
     if ordered:
         nodes.sort()
     return nodes
Esempio n. 7
0
 def get_output_file_name(self, doc):
     "Create an output file name for the given document."
     if self.to:
         return self.to
     if self.path:
         _, docfilename = os.path.split(doc.filename)
         return os.path.join(self.path, docfilename)
     # try to add something to the name / change extension
     if doc.filename.endswith('.gz'):
         docfilename, docext = os.path.splitext(doc.filename[:-3])
         compress_e = '.gz'
     else:
         docfilename, docext = os.path.splitext(doc.filename)
         compress_e = ''
     if self.add_to_name:
         return docfilename + self.add_to_name + \
             self.__class__.default_extension + compress_e
     if hasattr(self.__class__, 'default_extension') and \
             self.__class__.default_extension != docext:
         return docfilename + self.__class__.default_extension + compress_e
     # no default, just die
     raise RuntimeException('I don\'t know where to write.')
Esempio n. 8
0
 def ptree(self, value):
     if self.has_ptree():
         raise RuntimeException('Can\'t create a p-tree: tree exists')
     self.__ptree = value