Fix DirectoryDict getitem.

This commit is contained in:
Darko Poljak 2018-10-12 19:08:36 +02:00
parent 3b7f39cab7
commit 151edc6398

View file

@ -58,7 +58,7 @@ class FileList(collections.MutableSequence):
with open(self.path) as fd: with open(self.path) as fd:
for line in fd: for line in fd:
lines.append(line.rstrip('\n')) lines.append(line.rstrip('\n'))
except EnvironmentError as e: except EnvironmentError:
# error ignored # error ignored
pass pass
return lines return lines
@ -127,7 +127,16 @@ 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().rstrip('\n') value = fd.read().splitlines()
# if there is no value/empty line then return ''
# if there is only one value then return that value
# if there are multiple lines in file then return list
if not value:
return ''
elif len(value) == 1:
return value[0]
else:
return value
except EnvironmentError: except EnvironmentError:
raise KeyError(key) raise KeyError(key)