return rec
raise UnknownUser, user
- def _update(me, rec):
- """Update the record REC in the file."""
+ def _rewrite(me, op, rec):
+ """
+ Rewrite the file, according to OP.
+
+ The OP may be one of the following.
+
+ `create' There must not be a record matching REC; add a new
+ one.
+
+ `remove' There must be a record matching REC: remove it.
+
+ `update' There must be a record matching REC: write REC in its
+ place.
+ """
## The main update function.
def doit():
## Copy the old file to the new one, changing the user's record if
## and when we encounter it.
+ found = False
with OS.fdopen(fd, 'w') as f_out:
with open(me._file) as f_in:
for line in f_in:
r = me._parse(line)
if r.user != rec.user:
f_out.write(line)
+ elif op == 'create':
+ raise U.ExpectedError, \
+ (500, "Record for `%s' already exists" % rec.user)
else:
- f_out.write(rec._format())
+ found = True
+ if op != 'remove': f_out.write(rec._format())
+ if found:
+ pass
+ elif op == 'create':
+ f_out.write(rec._format())
+ else:
+ raise U.ExpectedError, \
+ (500, "Record for `%s' not found" % rec.user)
## Update the permissions on the new file. Don't try to fix the
## ownership (we shouldn't be running as root) or the group (the
"""Convenience function for constructing a record."""
return FlatFileRecord(line, me._delim, me._fmap, backend = me)
+ def _update(me, rec):
+ """Update the record REC in the file."""
+ me._rewrite('update', rec)
+
CONF.export('FlatFileBackend')
###--------------------------------------------------------------------------