35
32
raise ValueError("invalid AtomicFile mode %r" % mode)
38
self.tmpfilename = '%s.%d.%s.tmp' % (filename, os.getpid(),
35
self.tmpfilename = '%s.tmp.%d.%s' % (filename, os.getpid(),
39
36
socket.gethostname())
40
37
self.realfilename = filename
39
self.f = open(self.tmpfilename, mode)
44
self.f = codecs.open(self.tmpfilename, mode, encoding)
46
self.f = open(self.tmpfilename, mode)
43
self.f = codecs.EncodedFile(self.f, encoding)
48
45
self.write = self.f.write
53
return '%s(%r)' % (self.__class__.__name__,
46
self.closed = property(self.f.closed)
58
"""Close the file and move to final name."""
62
raise Exception('%r is already closed' % self)
68
52
if sys.platform == 'win32':
69
# windows cannot rename over an existing file
71
os.remove(self.realfilename)
74
if e.errno != errno.ENOENT:
53
os.remove(self.realfilename)
77
54
os.rename(self.tmpfilename, self.realfilename)
81
"""Discard temporary file without committing changes."""
85
raise Exception('%r is already closed' % self)
90
59
os.remove(self.tmpfilename)
94
"""Discard the file unless already committed."""
101
warn("%r leaked" % self)