1.1 --- a/WebStack/Repositories/Directory.py Wed Jun 13 00:20:17 2007 +0000
1.2 +++ b/WebStack/Repositories/Directory.py Wed Jun 13 00:21:00 2007 +0000
1.3 @@ -56,8 +56,7 @@
1.4 # Create a directory and initialise it with a special file.
1.5
1.6 if not os.path.exists(path):
1.7 - os.mkdir(path)
1.8 - os.mkdir(self.full_path(self.new_filename))
1.9 + self.create_resource(self.full_path(self.new_filename))
1.10
1.11 # Guess the filesystem encoding.
1.12
1.13 @@ -207,16 +206,10 @@
1.14 try:
1.15 self.lock(self.new_filename)
1.16 except KeyError:
1.17 - os.mkdir(self.edit_path(self.new_filename))
1.18 + self.create_resource(self.edit_path(self.new_filename))
1.19
1.20 try:
1.21 - os.mkdir(edit_path)
1.22 - if opener is None:
1.23 - f = open(os.path.join(edit_path, "data"), "wb")
1.24 - f.close()
1.25 - else:
1.26 - f = opener(os.path.join(edit_path, "data"))
1.27 - f.close()
1.28 + self.create_data(edit_path, opener)
1.29 finally:
1.30 self.unlock(self.new_filename)
1.31
1.32 @@ -247,12 +240,13 @@
1.33
1.34 path = self.full_path(key)
1.35 edit_path = self.edit_path(key)
1.36 - if os.path.exists(path) or os.path.exists(edit_path):
1.37 + if os.path.exists(path):
1.38 + self.lock(key)
1.39 +
1.40 + if os.path.exists(edit_path):
1.41 while 1:
1.42 try:
1.43 - for filename in glob.glob(os.path.join(edit_path, "*")):
1.44 - os.remove(filename)
1.45 - os.rmdir(edit_path)
1.46 + self.remove_resource(edit_path)
1.47 except OSError:
1.48 time.sleep(self.delay)
1.49 else:
1.50 @@ -266,7 +260,7 @@
1.51
1.52 edit_path = self.lock(key, create=0)
1.53 try:
1.54 - f = open(os.path.join(edit_path, "data"), "rb")
1.55 + f = self.open_resource(edit_path, "rb")
1.56 s = ""
1.57 try:
1.58 s = f.read()
1.59 @@ -286,7 +280,7 @@
1.60
1.61 edit_path = self.lock(key, create=1)
1.62 try:
1.63 - f = open(os.path.join(edit_path, "data"), "wb")
1.64 + f = self.open_resource(edit_path, "wb")
1.65 try:
1.66 f.write(value)
1.67 finally:
1.68 @@ -294,4 +288,44 @@
1.69 finally:
1.70 self.unlock(key)
1.71
1.72 + def create_resource(self, filename):
1.73 + f = open(filename, "wb")
1.74 + f.close()
1.75 +
1.76 + def create_data(self, edit_path, opener):
1.77 + if opener is None:
1.78 + f = open(edit_path, "wb")
1.79 + f.close()
1.80 + else:
1.81 + f = opener(edit_path)
1.82 + f.close()
1.83 +
1.84 + def remove_resource(self, edit_path):
1.85 + os.remove(edit_path)
1.86 +
1.87 + def open_resource(self, edit_path, mode):
1.88 + return open(edit_path, mode)
1.89 +
1.90 +class SessionDirectoryRepository(DirectoryRepository):
1.91 +
1.92 + def create_resource(self, filename):
1.93 + os.mkdir(filename)
1.94 +
1.95 + def create_data(self, edit_path, opener):
1.96 + os.mkdir(edit_path)
1.97 + if opener is None:
1.98 + f = open(os.path.join(edit_path, "data"), "wb")
1.99 + f.close()
1.100 + else:
1.101 + f = opener(os.path.join(edit_path, "data"))
1.102 + f.close()
1.103 +
1.104 + def remove_resource(self, edit_path):
1.105 + for filename in glob.glob(os.path.join(edit_path, "*")):
1.106 + os.remove(filename)
1.107 + os.rmdir(edit_path)
1.108 +
1.109 + def open_resource(self, edit_path, mode):
1.110 + return open(os.path.join(edit_path, "data"), mode)
1.111 +
1.112 # vim: tabstop=4 expandtab shiftwidth=4