Commits

Henning Schröder committed da6b0ad

new

Comments (0)

Files changed (3)

sourceindex/index.py

 
 
 class Indexer(object):
-	
+
+
 	def __init__(self, path):
 		if not os.path.exists(path):
 			os.makedirs(path)
 		#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")
+
+		
 if __name__ == "__main__":
 	idx = Indexer("index")
-	for entry in parse("string"):
-		kind, scope, name, lineno = entry
-		idx.add(kind=kind, name=name, line=lineno)
+	for filename, mod_name in find_modules():
+		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()

sourceindex/parser.py

+import sys
+import os
 import inspect
 
+from rope.base import ast
+from rope.refactor import functionutils
+from rope.base import pycore, evaluate, pyobjects, project, resources
+from rope.base.pyobjectsdef import PyModule, PyPackage, PyClass
+		
 
-def parse_dynamic(module_name):
 
-	def parse_scope(obj, scope):
-		
-		def parse_args(obj, lineno, scope):
-			argspec = inspect.getargspec(obj)
-			for arg in argspec.args:
-				yield ("argument", ".".join(scope), arg, lineno)
-		
-		def findsource(obj):
-			try:
-				code, lineno = inspect.findsource(obj)
-			except IOError:
-				code = lineno = ""
-			except TypeError:
-				code = lineno = ""
-			return code, lineno
-	
-		doc = inspect.getdoc(obj) or inspect.getcomments(obj)
-		for (name, member) in inspect.getmembers(obj):
-			if inspect.isfunction(member):
-				code, lineno = findsource(member)
-				yield ("func", ".".join(scope), name, lineno)
-				for result in parse_args(member, lineno, scope + [name]):
-					yield result
-			elif inspect.isclass(member) and  member.__module__ == scope[0]:
-				code, lineno = findsource(member)
-				yield ("class", ".".join(scope), name, lineno)
-				for result in parse_scope(member, scope + [name]):
-					yield result
-			elif inspect.ismethod(member):
-				code, lineno = findsource(member)
-				yield ("method", ".".join(scope), name, lineno)
-				for result in parse_args(member, lineno, scope + [name]):
-					yield result
-	
-	mod = __import__(module_name, {}, {}, ["foobardoesnotexist"])
-	for result in parse_scope(mod, [mod.__name__]):
-		yield result
+def stdlib_path():
+	path = os.path.dirname(inspect.getsourcefile(inspect))
+	if path not in sys.path:
+		print >>sys.stderr, "Warning: inspect-module not in sys.path. Could not find determine stdlib path properly"
+	return path
 	
 
+def modules(path):
+	for name in os.listdir(path):
+		filename = os.path.join(path, name)
+		if os.path.isdir(filename) and "-" not in name:
+			if os.path.exists(os.path.join(filename, "__init__.py")):
+				# skip check that __init__.py is not a directory
+				yield "pkg", filename, name
+		elif name.endswith(".py"):
+			yield "mod", filename, name[:-3]
+	
+			
+def libraries(path):
+	dynload_path = os.path.join(path, "lib-dynload")
+	for name in os.listdir(path):
+		filename = os.path.join(path, name)
+		if not os.path.isdir(filename):
+			if name.endswith((".dll", ".so")):
+				yield "lib", filename, os.path.splitext(name)[0]
+			
 
-parse = parse_dynamic
 
+def parse_rope(filename):
+	prj = project.NoProject()
+	pyc = pycore.PyCore(prj)
+	#resource = resources.File(prj, filename)
+	#if resource.is_folder():
+	#	pyobject = PyPackage(pyc, resource, force_errors=False)
+	#else:
+	#	pyobject = PyModule(pyc, resource=resource, force_errors=False)
+	pyobject = pyc.get_string_module(open(filename).read())
+	#ast = pyobject.ast_node
+	for item in parse_scope(pyobject.get_scope()):
+		yield item
+
+
+def parse_scope(scope):
+	kind = scope.get_kind()
+	pyobj = scope.pyobject
+	if kind == "Function":
+		fname = pyobj.get_name()
+		line = pyobj.get_ast().lineno
+		yield "func", fname, line
+		for param in scope.pyobject.get_parameters().keys():
+			yield "arg", param, line
+	elif kind == "Class":
+		cname = pyobj.get_name()
+		line = pyobj.get_ast().lineno
+		yield "class", 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)
+	for child in scope.get_scopes():
+		for item in parse_scope(child):
+			yield item
+
+
+	
+parse = parse_rope
 
 
 if __name__ == "__main__":
-	for item in parse("string"):
-		print item
+	std = stdlib_path()
+	print std
+	mods = list(modules(std))
+	print mods
+	fn = mods[-1][1]
+	print fn
+	for item in parse_rope(fn):
+		print item

sourceindex/search.py

 
 				
 if __name__ == "__main__":
+	import sys
 	s = Search("index")
-	for hit in s.search("line:130 OR template"):
+	qs = " ".join(sys.argv[1:]) or "line:130 OR template"
+	for hit in s.search(qs):
 		print hit
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.