Add modify dates handler

This commit is contained in:
lash 2022-03-13 16:36:17 +00:00
parent b92a4e6458
commit d68286ee6c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 40 additions and 0 deletions

View File

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

View File

@ -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.

View File

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