From faeee47f531a83159f351495e2b96b61426b53fa Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Sat, 03 Jan 2009 15:47:06 +0000 Subject: Add util.TempFilePath to track the creation and release of temporal files --- (limited to 'src') diff --git a/src/sugar/util.py b/src/sugar/util.py index c8bdfb1..d222f9c 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) + -- cgit v0.9.1