# HG changeset patch # User paulb # Date 1181694060 0 # Node ID b48fa30b302dbbaa84e867507cafdc4a2936e718 # Parent 96f950ed3660408123dbbeda98ecb1bdfbeccc22 [project @ 2007-06-13 00:21:00 by paulb] Introduced a SessionDirectoryRepository class which employs directories, reverting to the file-based model for DirectoryRepository. diff -r 96f950ed3660 -r b48fa30b302d WebStack/Repositories/Directory.py --- a/WebStack/Repositories/Directory.py Wed Jun 13 00:20:17 2007 +0000 +++ b/WebStack/Repositories/Directory.py Wed Jun 13 00:21:00 2007 +0000 @@ -56,8 +56,7 @@ # Create a directory and initialise it with a special file. if not os.path.exists(path): - os.mkdir(path) - os.mkdir(self.full_path(self.new_filename)) + self.create_resource(self.full_path(self.new_filename)) # Guess the filesystem encoding. @@ -207,16 +206,10 @@ try: self.lock(self.new_filename) except KeyError: - os.mkdir(self.edit_path(self.new_filename)) + self.create_resource(self.edit_path(self.new_filename)) try: - os.mkdir(edit_path) - if opener is None: - f = open(os.path.join(edit_path, "data"), "wb") - f.close() - else: - f = opener(os.path.join(edit_path, "data")) - f.close() + self.create_data(edit_path, opener) finally: self.unlock(self.new_filename) @@ -247,12 +240,13 @@ path = self.full_path(key) edit_path = self.edit_path(key) - if os.path.exists(path) or os.path.exists(edit_path): + if os.path.exists(path): + self.lock(key) + + if os.path.exists(edit_path): while 1: try: - for filename in glob.glob(os.path.join(edit_path, "*")): - os.remove(filename) - os.rmdir(edit_path) + self.remove_resource(edit_path) except OSError: time.sleep(self.delay) else: @@ -266,7 +260,7 @@ edit_path = self.lock(key, create=0) try: - f = open(os.path.join(edit_path, "data"), "rb") + f = self.open_resource(edit_path, "rb") s = "" try: s = f.read() @@ -286,7 +280,7 @@ edit_path = self.lock(key, create=1) try: - f = open(os.path.join(edit_path, "data"), "wb") + f = self.open_resource(edit_path, "wb") try: f.write(value) finally: @@ -294,4 +288,44 @@ finally: self.unlock(key) + def create_resource(self, filename): + f = open(filename, "wb") + f.close() + + def create_data(self, edit_path, opener): + if opener is None: + f = open(edit_path, "wb") + f.close() + else: + f = opener(edit_path) + f.close() + + def remove_resource(self, edit_path): + os.remove(edit_path) + + def open_resource(self, edit_path, mode): + return open(edit_path, mode) + +class SessionDirectoryRepository(DirectoryRepository): + + def create_resource(self, filename): + os.mkdir(filename) + + def create_data(self, edit_path, opener): + os.mkdir(edit_path) + if opener is None: + f = open(os.path.join(edit_path, "data"), "wb") + f.close() + else: + f = opener(os.path.join(edit_path, "data")) + f.close() + + def remove_resource(self, edit_path): + for filename in glob.glob(os.path.join(edit_path, "*")): + os.remove(filename) + os.rmdir(edit_path) + + def open_resource(self, edit_path, mode): + return open(os.path.join(edit_path, "data"), mode) + # vim: tabstop=4 expandtab shiftwidth=4