Commits

Henning Schröder  committed b8a9f17

search works now :)

  • Participants
  • Parent commits da6b0ad

Comments (0)

Files changed (4)

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()

File sourceindex/models.py

+import sys
+sys.path.insert(0, "whoosh.zip")
+
 from whoosh.index import create_in
 from whoosh.fields import Schema, TEXT, ID, KEYWORD, NUMERIC
 

File sourceindex/parser.py

 			
 
 
-def parse_rope(filename):
+def parse(filename):
 	prj = project.NoProject()
 	pyc = pycore.PyCore(prj)
 	#resource = resources.File(prj, filename)
 	kind = scope.get_kind()
 	pyobj = scope.pyobject
 	if kind == "Function":
+		parent = scope.parent
+		if parent.get_kind() != "Module":
+			parent_scope = parent.pyobject.get_name()
+		else:
+			parent_scope = ""
 		fname = pyobj.get_name()
 		line = pyobj.get_ast().lineno
-		yield "func", fname, line
+		yield "func", parent_scope, fname, line
 		for param in scope.pyobject.get_parameters().keys():
-			yield "arg", param, line
+			yield "arg", "", param, line
 	elif kind == "Class":
+		parent = scope.parent
+		if parent.get_kind() != "Module":
+			parent_scope = parent.pyobject.get_name()
+		else:
+			parent_scope = ""
 		cname = pyobj.get_name()
 		line = pyobj.get_ast().lineno
-		yield "class", cname, line
+		yield "class", parent_scope, cname, line
 	elif kind == "Module":
-		print "*", scope.get_defined_names()
 		for name, var in scope.get_defined_names().items():
 			pyobject = getattr(var, "pyobject", None)
 			if pyobject:
-				yield "global", name, getattr(pyobject, "get_ast", None)
+				ast = getattr(pyobject, "get_ast", None)
+				if ast:
+					yield "global", "", name, ast().lineno
 	for child in scope.get_scopes():
 		for item in parse_scope(child):
 			yield item
 
 
-	
-parse = parse_rope
+
 
 
 if __name__ == "__main__":

File sourceindex/search.py

+import sys
+sys.path.insert(0, "whoosh.zip")
+
 from whoosh.qparser import QueryParser
 from whoosh.index import open_dir
 
 	def search(self, qs):
 		with self._ix.searcher() as searcher:
 			query = QueryParser("name", self._ix.schema).parse(qs)
-			results = searcher.search(query)
+			results = searcher.search(query, limit=100)
 			for hit in results:
 				yield hit
 
 	import sys
 	s = Search("index")
 	qs = " ".join(sys.argv[1:]) or "line:130 OR template"
-	for hit in s.search(qs):
-		print hit
+	for num, hit in enumerate(s.search(qs)):
+		print num+1, hit