Skip to content

Commit

Permalink
find in project utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
amiraliakbari committed Dec 26, 2013
1 parent 9077bce commit 399ae7d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
41 changes: 39 additions & 2 deletions inspector/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ def filter_files(self, cond=None, extension=None):
if extension is not None:
return self.filter_files(cond=lambda f: get_extension(f) in [e.strip() for e in extension.split(',')])

def get_file(self, path, is_qualified=False):
def get_file(self, path, is_qualified=None):
"""
:param str path: source file path, can be relative, abstract, or in java dotted format
"""
if not self._files:
self.rescan_files()

if is_qualified is None:
is_qualified = not ('/' in path or '\\' in path)

if is_qualified:
# java dotted format
found = False
Expand All @@ -149,7 +152,7 @@ def get_file(self, path, is_qualified=False):
found = True
break
if not found:
raise KeyError('File not found in project source roots ({0}).'.format(unicode(self.source_roots)))
raise KeyError('File not found in project source roots: {0}'.format(path))
else:
rel_path = self.build_relative_path(path)

Expand All @@ -171,6 +174,40 @@ def dfs_files(self, handler):
self.rescan_files(handler)
handler.tear_down()

####################
# Find Utilities #
####################
def find_class(self, qualified_name):
p = qualified_name.split('.')
class_name = p[-1]
try:
class_file = self.get_file('.'.join(p[:-1]))
except KeyError:
# the user can omit the class_name if class_name == filename (e.g. a.b.ClassName.ClassName)
# TODO: consider other languages like python! they maybe need camel case conversion
class_file = self.get_file('.'.join(p))
return class_file.get_class(class_name)

def find(self, identifier):
""" Find the code object (file/class/method/etc.) specified by the
identifier in this project.
:param str identifier: object to find, e.g. 'class:a.b.X', 'file:a.b.f'
"""
try:
id_type, id_val = identifier.split(':', 1)
except ValueError:
raise ValueError('Invalid identifer: {0}'.format(identifier))
if id_type == 'file':
return self.get_file(id_val)
if id_type == 'class':
return self.find_class(id_val)
if id_type == 'method':
ind = id_val.rfind('.')
method_class = self.find_class(id_val[:ind])
return method_class.get_method(id_val[ind+1:])
raise ValueError('Invalid identifier: {0}'.format(id_type))

############################
# File Loading & Parsing #
############################
Expand Down
21 changes: 21 additions & 0 deletions inspector/test/android_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest

from inspector.models.android import AndroidProject
from inspector.models.base import Method
from inspector.models.consts import Language
from inspector.utils.strings import has_word

Expand All @@ -12,6 +13,9 @@ def setUp(self):
path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'android', 'github-android')
self.project = AndroidProject(path)

def assertFilenameEqual(self, filename1, filename2):
return filename1.replace('\\', '/').endswith('/github-android/' + filename2)

def test_import_detection(self):
sf = self.project.get_file('sample_files/IssuesFragment.java')
self.assertTrue(sf.language_detected)
Expand All @@ -37,6 +41,23 @@ def test_import_detection(self):
for l in usages:
self.assertTrue(sf.get_line(l).find(identifier) > -1)

def test_find(self):
sf = self.project.find('file:sample_files.IssueFragment')
self.assertFilenameEqual(sf.filename, 'sample_files/IssueFragment.java')

cls = self.project.find('class:sample_files.IssueFragment.IssueFragment')
self.assertEqual(cls.name, 'IssueFragment')
self.assertFilenameEqual(sf.filename, 'sample_files/IssueFragment.java')

# alternative (shortcut) way for the same action
cls = self.project.find('class:sample_files.IssueFragment')
self.assertEqual(cls.name, 'IssueFragment')
self.assertFilenameEqual(sf.filename, 'sample_files/IssueFragment.java')

mt = self.project.find('method:sample_files.IssueFragment.openPullRequestCommits')
self.assertEqual(mt.name, 'openPullRequestCommits')
self.assertEqual(mt.access, Method.ACCESS.PRIVATE)

def test_java_parse_1(self):
sf = self.project.get_file('sample_files/IssueFragment.java')
cls = sf.get_class('IssueFragment')
Expand Down

0 comments on commit 399ae7d

Please sign in to comment.