1. Henning Schröder
  2. pycode

Source

pycode / sourceindex / index.py

Diff from to

File sourceindex/index.py

 import os
+import sys
+sys.path.insert(0, "whoosh.zip")
 from whoosh.index import open_dir, create_in
 import models
-from parser import parse
+import parser
+
 
 
 class Indexer(object):
 		self._writer.commit()
 
 	
-	def add(self, **kwargs):
+	def _add(self, **kwargs):
 		for key, value in kwargs.items():
 			if isinstance(value, str):
 				kwargs[key] = unicode(value)
 		#self._writer.commit()
 
 		
-def find_modules():
-	path = "/usr/lib/python2.7"
-	for name in os.listdir(path):
-		if name.endswith(".py"):
-			filename = os.path.join(path, name)
-			yield filename, name.rstrip(".py")
+	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 filename, mod_name in find_modules():
+	for kind, filename, module_name in parser.modules(parser.stdlib_path()):
 		print filename
-		try:
-			nodes = list(parse(mod_name))
-		except:
-			continue
-		for entry in nodes:
-			kind, scope, name, lineno = entry
-			idx.add(kind=kind, name=name, line=lineno, filename=filename)
-	idx.close()
+		idx.add_filename(filename, module_name)
+	idx.close()