cleanup, add sort method
Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
parent
b4fc05ba09
commit
3873aefcf5
1 changed files with 10 additions and 4 deletions
|
@ -32,6 +32,8 @@ class FileList(collections.MutableSequence):
|
||||||
def __init__(self, path, initial=None):
|
def __init__(self, path, initial=None):
|
||||||
self._path = path
|
self._path = path
|
||||||
if initial:
|
if initial:
|
||||||
|
# delete existing file
|
||||||
|
os.unlink(self._path)
|
||||||
for i in initial:
|
for i in initial:
|
||||||
self.append(i)
|
self.append(i)
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ class FileList(collections.MutableSequence):
|
||||||
try:
|
try:
|
||||||
with open(self._path) as fd:
|
with open(self._path) as fd:
|
||||||
for line in fd:
|
for line in fd:
|
||||||
lines.append(line.strip())
|
lines.append(line.rstrip('\n'))
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
# error ignored
|
# error ignored
|
||||||
pass
|
pass
|
||||||
|
@ -50,7 +52,7 @@ class FileList(collections.MutableSequence):
|
||||||
try:
|
try:
|
||||||
with open(self._path, 'w') as fd:
|
with open(self._path, 'w') as fd:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
fd.write(line + '\n')
|
fd.write(str(line) + '\n')
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
# error ignored
|
# error ignored
|
||||||
raise
|
raise
|
||||||
|
@ -80,6 +82,10 @@ class FileList(collections.MutableSequence):
|
||||||
lines.insert(index, value)
|
lines.insert(index, value)
|
||||||
self.__write(lines)
|
self.__write(lines)
|
||||||
|
|
||||||
|
def sort(self):
|
||||||
|
lines = sorted(self)
|
||||||
|
self.__write(lines)
|
||||||
|
|
||||||
|
|
||||||
class DirectoryDict(collections.MutableMapping):
|
class DirectoryDict(collections.MutableMapping):
|
||||||
"""A dict that stores it's state in a directory.
|
"""A dict that stores it's state in a directory.
|
||||||
|
@ -98,13 +104,13 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(self._path, key), "r") as fd:
|
with open(os.path.join(self._path, key), "r") as fd:
|
||||||
return fd.read()
|
return fd.read().rstrip('\n')
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
with open(os.path.join(self._path, key), "w") as fd:
|
with open(os.path.join(self._path, key), "w") as fd:
|
||||||
fd.write(value)
|
fd.write(str(value))
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
os.remove(os.path.join(self._path, key))
|
os.remove(os.path.join(self._path, key))
|
||||||
|
|
Loading…
Reference in a new issue