diff core/functions.py @ 976:f5aa74dcdd7a

Ensure temporary files get deleted during hotlinking.
author Brian Neal <bgneal@gmail.com>
date Mon, 05 Oct 2015 20:07:44 -0500
parents 51a2051588f5
children e932f2ecd4a7
line wrap: on
line diff
--- a/core/functions.py	Mon Oct 05 20:06:24 2015 -0500
+++ b/core/functions.py	Mon Oct 05 20:07:44 2015 -0500
@@ -1,4 +1,5 @@
 """This file houses various core utility functions"""
+from contextlib import contextmanager
 import datetime
 import logging
 import os
@@ -31,6 +32,15 @@
         os.remove(self.filename)
 
 
+@contextmanager
+def remove_file(path):
+    """Context manager for removing a file when the context is exited."""
+    try:
+        yield path
+    finally:
+        os.remove(path)
+
+
 def send_mail(subject, message, from_email, recipient_list, reply_to=None, defer=True):
     """
     The main send email function. Use this function to send email from the