diff --git a/src/sugar/util.py b/src/sugar/util.py index c8bdfb10..d222f9c4 100644 --- a/src/sugar/util.py +++ b/src/sugar/util.py @@ -26,6 +26,8 @@ import sha import random import binascii import gettext +import tempfile +import logging _ = lambda msg: gettext.dgettext('sugar-toolkit', msg) @@ -261,3 +263,19 @@ def timestamp_to_elapsed_string(timestamp, max_levels=2): return ELAPSED % time_period +class TempFilePath(str): + + def __new__(cls, path=None): + if path is None: + fd, path = tempfile.mkstemp() + os.remove(fd) + logging.debug('TempFilePath created %r' % path) + return str.__new__(cls, path) + + def __del__(self): + if os.path.exists(self): + os.unlink(self) + logging.debug('TempFilePath deleted %r' % self) + else: + logging.warning('TempFilePath already deleted %r' % self) +