Skip to content

Commit

Permalink
Analysis will now detect changes in hierarchy and refresh. Closes #196.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Aug 29, 2015
1 parent 0f5b307 commit ad70c77
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
19 changes: 17 additions & 2 deletions agate/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,34 @@ class Analysis(object):
:param func: The analysis function. Must accept a `data` argument that
is the state inherited from ancestors analysis.
:param parent: The parent analysis of this one, if any.
:param cache_path: Where to stored the cache files for this analysis.
"""
def __init__(self, func, cache_path='.agate'):
def __init__(self, func, parent=None, cache_path='.agate'):
self._name = func.__name__
self._func = func
self._parent = parent
self._cache_path = cache_path
self._next_analyses = []

def _trace(self):
"""
Returns the sequence of Analysis instances that lead to this one.
"""
if self._parent:
return self._parent._trace() + [self._name]

return [self._name]

def _fingerprint(self):
"""
Generate a fingerprint for this analysis function.
"""
hasher = hashlib.md5()

trace = self._trace()
hasher.update('\n'.join(trace))

source = inspect.getsource(self._func)
hasher.update(source.encode('utf-8'))

Expand Down Expand Up @@ -99,7 +114,7 @@ def then(self, next_func):
:param func: The analysis function. Must accept a `data` argument that
is the state inherited from ancestors analysis.
"""
analysis = Analysis(next_func, cache_path=self._cache_path)
analysis = Analysis(next_func, parent=self, cache_path=self._cache_path)

self._next_analyses.append(analysis)

Expand Down
38 changes: 38 additions & 0 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def stage2(self, data):

self.data_after_stage2 = deepcopy(data)

def stage_noop(self, data):
pass

def test_data_flow(self):
analysis = Analysis(self.stage1, cache_path=TEST_CACHE)
analysis.then(self.stage2)
Expand Down Expand Up @@ -144,3 +147,38 @@ def test_ancestor_fingerprint_mismatch(self):

self.assertEqual(self.executed_stage1, 2)
self.assertEqual(self.executed_stage2, 2)

def test_cache_reused(self):
analysis = Analysis(self.stage1, cache_path=TEST_CACHE)
analysis.then(self.stage2)

analysis.run()

self.assertEqual(self.executed_stage1, 1)
self.assertEqual(self.executed_stage2, 1)

analysis2 = Analysis(self.stage1, cache_path=TEST_CACHE)
analysis2.then(self.stage2)

analysis2.run()

self.assertEqual(self.executed_stage1, 1)
self.assertEqual(self.executed_stage2, 1)

def test_ancestor_changed(self):
analysis = Analysis(self.stage1, cache_path=TEST_CACHE)
noop = analysis.then(self.stage_noop)
noop.then(self.stage2)

analysis.run()

self.assertEqual(self.executed_stage1, 1)
self.assertEqual(self.executed_stage2, 1)

analysis2 = Analysis(self.stage1, cache_path=TEST_CACHE)
analysis2.then(self.stage2)

analysis2.run()

self.assertEqual(self.executed_stage1, 1)
self.assertEqual(self.executed_stage2, 2)

0 comments on commit ad70c77

Please sign in to comment.