Add modify dates handler
This commit is contained in:
parent
b92a4e6458
commit
d68286ee6c
@ -1,5 +1,6 @@
|
||||
# standard imports
|
||||
import re
|
||||
import datetime
|
||||
|
||||
# local imports
|
||||
from shep.error import (
|
||||
@ -39,6 +40,7 @@ class State:
|
||||
self.__keys = {getattr(self, self.base_state_name): []}
|
||||
self.__keys_reverse = {}
|
||||
self.__contents = {}
|
||||
self.__change = {}
|
||||
self.verifier = verifier
|
||||
|
||||
|
||||
@ -302,6 +304,8 @@ class State:
|
||||
if contents != None:
|
||||
self.__contents[key] = contents
|
||||
|
||||
self.register_modify(key)
|
||||
|
||||
return state
|
||||
|
||||
|
||||
@ -348,6 +352,8 @@ class State:
|
||||
current_state_list.pop(idx)
|
||||
self.__add_state_list(to_state, key)
|
||||
|
||||
self.register_modify(key)
|
||||
|
||||
return to_state
|
||||
|
||||
|
||||
@ -549,3 +555,11 @@ class State:
|
||||
"""
|
||||
self.state(key)
|
||||
self.__contents[key] = contents
|
||||
|
||||
|
||||
def modified(self, key):
|
||||
return self.__change[key]
|
||||
|
||||
|
||||
def register_modify(self, key):
|
||||
self.__change[key] = datetime.datetime.now().timestamp()
|
||||
|
@ -103,6 +103,16 @@ class SimpleFileStore:
|
||||
f.close()
|
||||
|
||||
|
||||
def modified(self, k):
|
||||
path = self.path(k)
|
||||
st = os.stat(path)
|
||||
return float(st.st_ctime())
|
||||
|
||||
|
||||
def register_modify(self, k):
|
||||
pass
|
||||
|
||||
|
||||
class SimpleFileStoreFactory:
|
||||
"""Provide a method to instantiate SimpleFileStore instances that provide persistence for individual states.
|
||||
|
||||
|
@ -136,5 +136,21 @@ class TestState(unittest.TestCase):
|
||||
self.assertEqual(states.state('abcd'), states.FOO)
|
||||
|
||||
|
||||
def test_change_dates(self):
|
||||
states = State(3)
|
||||
states.add('foo')
|
||||
states.put('abcd')
|
||||
states.put('bcde')
|
||||
|
||||
a = states.modified('abcd')
|
||||
b = states.modified('bcde')
|
||||
self.assertGreater(b, a)
|
||||
|
||||
states.set('abcd', states.FOO)
|
||||
a = states.modified('abcd')
|
||||
b = states.modified('bcde')
|
||||
self.assertGreater(a, b)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user