Commit 811b3de7 authored by Martin Matějek's avatar Martin Matějek

fixup! Refactor removing files from FS to use python native os.* functions

parent 406c7eba
......@@ -20,6 +20,3 @@ class NoSuchNotificationException(NotifylibError):
class NotificationNotDismissibleException(NotifylibError):
pass
class RemoveFailedException(NotifylibError):
pass
......@@ -4,7 +4,6 @@ import logging
from datetime import datetime
from functools import lru_cache
from .exceptions import RemoveFailedException
from .notification import Notification
logger = logging.getLogger(__name__)
......@@ -129,8 +128,8 @@ class NotificationStorage:
to_delete.append(n)
for n in to_delete:
self.remove(n.notif_id)
logger.debug("Deleting notification '%s' due to timeout", n.notif_id)
self.remove(n.notif_id)
def remove(self, msgid):
"""Remove single notification"""
......@@ -150,14 +149,19 @@ class NotificationStorage:
filename = os.path.join(storage_dir, "{}.json".format(msgid))
tmp_filename = os.path.join("/tmp", "{}.json".format(msgid))
# TODO: figure out how to pass what exactly failed to user
try:
os.rename(filename, tmp_filename)
except FileNotFoundError as e:
logger.error(e)
return
except IsADirectoryError:
logger.warning("Cannot rename file. There already is a directory with the same name!")
raise RemoveFailedException("Moving to tmpfile failed. Please see the logs.")
logger.error("Cannot rename file. There already is a directory with the same name!")
return
try:
os.unlink(tmp_filename)
except OSError as e:
logger.warning("Can't remove '%s'. Reason: %s", tmp_filename, e)
raise RemoveFailedException("Failed to remove file. Please see the logs.")
logger.error("Cannot remove tempfile '%s'. Reason: %s", tmp_filename, e)
return
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment