OSDN Git Service

Fix failure saving to a dat file when the parent directory does not exist.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Thu, 24 Aug 2006 14:55:21 +0000 (23:55 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Thu, 24 Aug 2006 14:55:21 +0000 (23:55 +0900)
src/Hage1/thread_window.py

index 025f339..7f56e39 100644 (file)
@@ -166,8 +166,31 @@ class WinWrap:
                 self.bbs, self.board, self.thread,
                 self.append_rawres_to_buffer, self.num+1)
 
                 self.bbs, self.board, self.thread,
                 self.append_rawres_to_buffer, self.num+1)
 
+
+        class FileWrap:
+            def __init__(self, path):
+                self._file = None
+                self._path = path
+            def __del__(self):
+                self.close()
+            def seek(self, size):
+                self.file().seek(size)
+            def write(self, data):
+                self.file().write(data)
+            def close(self):
+                if self._file:
+                    self._file.close()
+                    self._file = None
+            def file(self):
+                if not self._file:
+                    basedir = os.path.dirname(self._path)
+                    if not os.path.isdir(basedir):
+                        os.makedirs(basedir)
+                    self._file = file(self._path, "a+")
+                return self._file
+
         dat_path = misc.get_thread_dat_path(self.bbs, self.board, self.thread)
         dat_path = misc.get_thread_dat_path(self.bbs, self.board, self.thread)
-        dat_file = file(dat_path, "a+")
+        dat_file = FileWrap(dat_path)
 
         def save_line_and_append_to_buffer(line):
             dat_file.seek(self.size)
 
         def save_line_and_append_to_buffer(line):
             dat_file.seek(self.size)