2222
2323import os
2424
25+
2526def files_in_dir (dir ):
2627 if not os .path .exists (dir ):
2728 return 0
2829 num_files = 0
2930 for dir_entry in os .listdir (dir ):
30- full_filename = os .path .join (dir ,dir_entry )
31+ full_filename = os .path .join (dir , dir_entry )
3132 if os .path .isfile (full_filename ):
3233 num_files = num_files + 1
3334 elif os .path .isdir (full_filename ):
3435 num_files = num_files + files_in_dir (full_filename )
3536 return num_files
3637
38+
3739class FileStorage (object ):
3840 """Store files in some directory structure
3941
@@ -63,7 +65,7 @@ class FileStorage(object):
6365
6466 Edit Procotol
6567 -------------
66-
68+
6769 When a file is created or edited, the following protocol is used:
6870
6971 1. The new content of the file is placed in 'file.tmp'.
@@ -91,14 +93,14 @@ class FileStorage(object):
9193 or two web clients running with a multi-process server) attempt
9294 edits at the same time, both will write to 'file.tmp', and the
9395 results will be indeterminate.
94-
96+
9597 Crash Analysis
9698 --------------
97-
99+
98100 There are several situations that may occur if a crash (whether
99101 because the machine crashes, because an unhandled Python exception
100102 is raised, or because the Python process is killed) occurs.
101-
103+
102104 Complexity ensues because backuping up an RDBMS is generally more
103105 complex than simply copying a file. Instead, some command is run
104106 which stores a snapshot of the database in a file. So, if you
@@ -203,22 +205,22 @@ class FileStorage(object):
203205 database will be consistent, so long as unreferenced 'file.v'
204206 files are never removed until after the database has been backed
205207 up.
206- """
208+ """
207209
208210 tempext = '.tmp'
209211 """The suffix added to files indicating that they are uncommitted."""
210-
212+
211213 def __init__ (self , umask ):
212214 self .umask = umask
213215
214216 def subdirFilename (self , classname , nodeid , property = None ):
215217 """Determine what the filename and subdir for nodeid + classname is."""
216218 if property :
217- name = '%s%s.%s' % (classname , nodeid , property )
219+ name = '%s%s.%s' % (classname , nodeid , property )
218220 else :
219221 # roundupdb.FileClass never specified the property name, so don't
220222 # include it
221- name = '%s%s' % (classname , nodeid )
223+ name = '%s%s' % (classname , nodeid )
222224
223225 # have a separate subdir for every thousand messages
224226 subdir = str (int (nodeid ) // 1000 )
@@ -239,11 +241,10 @@ def _editInProgress(self, classname, nodeid, property):
239241
240242 for method , args in self .transactions :
241243 if (method == self .doStoreFile and
242- args == (classname , nodeid , property )):
244+ args == (classname , nodeid , property )):
243245 return True
244246
245247 return False
246-
247248
248249 def filename (self , classname , nodeid , property = None , create = 0 ):
249250 """Determine what the filename for the given node and optionally
@@ -253,8 +254,8 @@ def filename(self, classname, nodeid, property=None, create=0):
253254 usual place, or it could be in a temp file pre-commit *or* it
254255 could be in an old-style, backwards-compatible flat directory.
255256 """
256- filename = os .path .join (self .dir , 'files' , classname ,
257- self .subdirFilename (classname , nodeid , property ))
257+ filename = os .path .join (self .dir , 'files' , classname ,
258+ self .subdirFilename (classname , nodeid , property ))
258259 # If the caller is going to create the file, return the
259260 # post-commit filename. It is the callers responsibility to
260261 # add self.tempext when actually creating the file.
@@ -267,13 +268,13 @@ def filename(self, classname, nodeid, property=None, create=0):
267268 # of the temporary file containing the edited content.
268269 if self ._editInProgress (classname , nodeid , property ):
269270 if not os .path .exists (tempfile ):
270- raise IOError ('content file for %s not found' % tempfile )
271+ raise IOError ('content file for %s not found' % tempfile )
271272 return tempfile
272273
273274 if os .path .exists (filename ):
274275 return filename
275276
276- # Otherwise, if the temporary file exists, then the probable
277+ # Otherwise, if the temporary file exists, then the probable
277278 # explanation is that a crash occurred between the point that
278279 # the database entry recording the creation of the file
279280 # occured and the point at which the file was renamed from the
@@ -288,21 +289,21 @@ def filename(self, classname, nodeid, property=None, create=0):
288289 # at the same time, only one of them will succeed.
289290 # So, tolerate such an error -- but no other.
290291 if not os .path .exists (filename ):
291- raise IOError ('content file for %s not found' % filename )
292+ raise IOError ('content file for %s not found' % filename )
292293 return filename
293294
294295 # ok, try flat (very old-style)
295296 if property :
296- filename = os .path .join (self .dir , 'files' , '%s%s.%s' % ( classname ,
297- nodeid , property ))
297+ filename = os .path .join (self .dir , 'files' , '%s%s.%s' % (
298+ classname , nodeid , property ))
298299 else :
299- filename = os .path .join (self .dir , 'files' , '%s%s' % (classname ,
300- nodeid ))
300+ filename = os .path .join (self .dir , 'files' , '%s%s' % (classname ,
301+ nodeid ))
301302 if os .path .exists (filename ):
302303 return filename
303304
304305 # file just ain't there
305- raise IOError ('content file for %s not found' % filename )
306+ raise IOError ('content file for %s not found' % filename )
306307
307308 def filesize (self , classname , nodeid , property = None , create = 0 ):
308309 filename = self .filename (classname , nodeid , property , create )
@@ -327,7 +328,7 @@ def storefile(self, classname, nodeid, property, content):
327328 if not self ._editInProgress (classname , nodeid , property ):
328329 # save off the rename action
329330 self .transactions .append ((self .doStoreFile , (classname , nodeid ,
330- property )))
331+ property )))
331332 # always set umask before writing to make sure we have the proper one
332333 # in multi-tracker (i.e. multi-umask) or modpython scenarios
333334 # the umask may have changed since last we set it.
@@ -393,7 +394,7 @@ def isStoreFile(self, classname, nodeid):
393394 Is there a better way than using self.filename?
394395 """
395396 try :
396- fname = self .filename (classname , nodeid )
397+ fname = self .filename (classname , nodeid ) # noqa: F841
397398 return True
398399 except IOError :
399400 return False
0 commit comments