src/dbi.py: Use open() instead of file().

This commit is contained in:
Valentin Lorentz 2012-08-05 16:56:07 +02:00
parent 14200e002a
commit 535593d555
1 changed files with 13 additions and 13 deletions

View File

@ -97,14 +97,14 @@ class DirMapping(MappingInterface):
self._setMax(1) self._setMax(1)
def _setMax(self, id): def _setMax(self, id):
fd = file(os.path.join(self.dirname, 'max'), 'w') fd = open(os.path.join(self.dirname, 'max'), 'w')
try: try:
fd.write(str(id)) fd.write(str(id))
finally: finally:
fd.close() fd.close()
def _getMax(self): def _getMax(self):
fd = file(os.path.join(self.dirname, 'max')) fd = open(os.path.join(self.dirname, 'max'))
try: try:
i = int(fd.read()) i = int(fd.read())
return i return i
@ -116,7 +116,7 @@ class DirMapping(MappingInterface):
def get(self, id): def get(self, id):
try: try:
fd = file(self._makeFilename(id)) fd = open(self._makeFilename(id))
return fd.read() return fd.read()
except EnvironmentError, e: except EnvironmentError, e:
exn = NoRecordError(id) exn = NoRecordError(id)
@ -124,13 +124,13 @@ class DirMapping(MappingInterface):
raise exn raise exn
def set(self, id, s): def set(self, id, s):
fd = file(self._makeFilename(id), 'w') fd = open(self._makeFilename(id), 'w')
fd.write(s) fd.write(s)
fd.close() fd.close()
def add(self, s): def add(self, s):
id = self._getMax() id = self._getMax()
fd = file(self._makeFilename(id), 'w') fd = open(self._makeFilename(id), 'w')
try: try:
fd.write(s) fd.write(s)
return id return id
@ -147,7 +147,7 @@ class FlatfileMapping(MappingInterface):
def __init__(self, filename, maxSize=10**6): def __init__(self, filename, maxSize=10**6):
self.filename = filename self.filename = filename
try: try:
fd = file(self.filename) fd = open(self.filename)
strId = fd.readline().rstrip() strId = fd.readline().rstrip()
self.maxSize = len(strId) self.maxSize = len(strId)
try: try:
@ -169,7 +169,7 @@ class FlatfileMapping(MappingInterface):
def _incrementCurrentId(self, fd=None): def _incrementCurrentId(self, fd=None):
fdWasNone = fd is None fdWasNone = fd is None
if fdWasNone: if fdWasNone:
fd = file(self.filename, 'a') fd = open(self.filename, 'a')
fd.seek(0) fd.seek(0)
self.currentId += 1 self.currentId += 1
fd.write(self._canonicalId(self.currentId)) fd.write(self._canonicalId(self.currentId))
@ -187,7 +187,7 @@ class FlatfileMapping(MappingInterface):
def add(self, s): def add(self, s):
line = self._joinLine(self.currentId, s) line = self._joinLine(self.currentId, s)
fd = file(self.filename, 'r+') fd = open(self.filename, 'r+')
try: try:
fd.seek(0, 2) # End. fd.seek(0, 2) # End.
fd.write(line) fd.write(line)
@ -199,7 +199,7 @@ class FlatfileMapping(MappingInterface):
def get(self, id): def get(self, id):
strId = self._canonicalId(id) strId = self._canonicalId(id)
try: try:
fd = file(self.filename) fd = open(self.filename)
fd.readline() # First line, nextId. fd.readline() # First line, nextId.
for line in fd: for line in fd:
(lineId, s) = self._splitLine(line) (lineId, s) = self._splitLine(line)
@ -215,7 +215,7 @@ class FlatfileMapping(MappingInterface):
def set(self, id, s): def set(self, id, s):
strLine = self._joinLine(id, s) strLine = self._joinLine(id, s)
try: try:
fd = file(self.filename, 'r+') fd = open(self.filename, 'r+')
self.remove(id, fd) self.remove(id, fd)
fd.seek(0, 2) # End. fd.seek(0, 2) # End.
fd.write(strLine) fd.write(strLine)
@ -227,7 +227,7 @@ class FlatfileMapping(MappingInterface):
strId = self._canonicalId(id) strId = self._canonicalId(id)
try: try:
if fdWasNone: if fdWasNone:
fd = file(self.filename, 'r+') fd = open(self.filename, 'r+')
fd.seek(0) fd.seek(0)
fd.readline() # First line, nextId fd.readline() # First line, nextId
pos = fd.tell() pos = fd.tell()
@ -247,7 +247,7 @@ class FlatfileMapping(MappingInterface):
fd.close() fd.close()
def __iter__(self): def __iter__(self):
fd = file(self.filename) fd = open(self.filename)
fd.readline() # First line, nextId. fd.readline() # First line, nextId.
for line in fd: for line in fd:
(id, s) = self._splitLine(line) (id, s) = self._splitLine(line)
@ -256,7 +256,7 @@ class FlatfileMapping(MappingInterface):
fd.close() fd.close()
def vacuum(self): def vacuum(self):
infd = file(self.filename) infd = open(self.filename)
outfd = utils.file.AtomicFile(self.filename,makeBackupIfSmaller=False) outfd = utils.file.AtomicFile(self.filename,makeBackupIfSmaller=False)
outfd.write(infd.readline()) # First line, nextId. outfd.write(infd.readline()) # First line, nextId.
for line in infd: for line in infd: