Source

pycode / sourceindex / index.py

Full commit
import os
import sys
sys.path.insert(0, "whoosh.zip")
from whoosh.index import open_dir, create_in
import models
import parser



class Indexer(object):


	def __init__(self, path):
		if not os.path.exists(path):
			os.makedirs(path)
		try:
			self._ix = open_dir(path)
		except:
			self._ix = create_in(path, models.name_schema)
		self._writer = self._ix.writer()

	
	def close(self):
		self._writer.commit()

	
	def _add(self, **kwargs):
		for key, value in kwargs.items():
			if isinstance(value, str):
				kwargs[key] = unicode(value)
		self._writer.add_document(**kwargs)
		#self._writer.commit()

		
	def remove_filename(self, filename):
		self._writer.delete_by_term("filename", filename)
		
		
	def add_filename(self, filename, module_name):
		try:
			items = list(parser.parse(filename))
		except Exception, exc:
			print filename, exc
			return
		self.remove_filename(filename)
		for entry in items:
			kind, scope, name, lineno = entry
			self._add(kind=kind, name=name, line=lineno, filename=filename)
		self._add(kind="module", name=module_name, line=0, filename=filename)



		

if __name__ == "__main__":
	idx = Indexer("index")
	for kind, filename, module_name in parser.modules(parser.stdlib_path()):
		print filename
		idx.add_filename(filename, module_name)
	idx.close()