class FieldWriterWithGraph(FieldWriter): def __init__(self): FieldWriter.__init__(self) # Implementations need to set these attributes self._storage = None self._segment = None self._fieldname = None self._fieldobj = None FST_EXT = CodecWithGraph.FST_EXT def _prep_graph(self): from whoosh.automata.fst import GraphWriter gf = self._segment.create_file(self._storage, self.FST_EXT) self._gwriter = GraphWriter(gf) def _start_graph_field(self, fieldname, fieldobj): spelling = fieldobj.spelling separate = fieldobj.separate_spelling() self._needs_graph = spelling or separate self._auto_graph = spelling and not separate if self._needs_graph: if not hasattr(self, "_gwriter") or self._gwriter is None: self._prep_graph() self._gwriter.start_field(fieldname) def _insert_graph_key(self, btext): if self._auto_graph: key = self._fieldobj.from_bytes(btext) self.add_spell_word(self._fieldname, key) def add_spell_word(self, fieldname, word): assert fieldname == self._fieldname assert isinstance(word, text_type) self._gwriter.insert(word) def _finish_graph_field(self): if self._needs_graph: self._gwriter.finish_field() def _close_graph(self): if hasattr(self, "_gwriter") and self._gwriter: self._gwriter.close()
def _prep_graph(self): from whoosh.automata.fst import GraphWriter gf = self._segment.create_file(self._storage, self.FST_EXT) self._gwriter = GraphWriter(gf)