remove newline characters when reading password hashes from a file

sort the entries by name during "save"
This commit is contained in:
lars 2010-06-10 14:02:47 +00:00
parent 8dd0b23e74
commit 7612f74f87

View file

@ -57,6 +57,10 @@ class HtpasswdFile:
for line in open(self.filename, 'r').readlines():
if ":" in line:
username, pwhash = line.split(':', 1)
# remove spaces
username = username.strip()
# remove newline characters and spaces
pwhash = pwhash.strip()
self.entries[username] = pwhash
def save(self):
@ -73,8 +77,10 @@ class HtpasswdFile:
if os.path.isfile(self.filename):
# copy the original file mode (mod, timestamps)
shutil.copystat(self.filename, temp_filename)
for name, pwhash in self.entries.items():
os.write(temp_file, "%s:%s%s" % (name, pwhash, os.linesep))
sorted_names = self.entries.keys()
sorted_names.sort()
for name in sorted_names:
os.write(temp_file, "%s:%s%s" % (name, self.entries[name], os.linesep))
os.close(temp_file)
except IOError:
try: