From c25865e76baf4916abf97ecc15e578331e69bad5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 2 Jan 2007 18:30:11 +0000 Subject: [PATCH] Oops. Forgot to add module ptempfile.py to svn --- libprs500/ptempfile.py | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 libprs500/ptempfile.py diff --git a/libprs500/ptempfile.py b/libprs500/ptempfile.py new file mode 100644 index 0000000000..8cca86fb95 --- /dev/null +++ b/libprs500/ptempfile.py @@ -0,0 +1,59 @@ +## Copyright (C) 2006 Kovid Goyal kovid@kovidgoyal.net +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License along +## with this program; if not, write to the Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.Warning +""" +Provides platform independent temporary files that persist even after +being closed. +""" +import tempfile +import os + +from libprs500 import __version__ + +class _TemporaryFileWrapper(object): + """ + Temporary file wrapper + + This class provides a wrapper around files opened for + temporary use. In particular, it seeks to automatically + remove the file when the object is deleted. + """ + + def __init__(self, _file, name): + self.file = _file + self.name = name + + def __getattr__(self, name): + _file = self.__dict__['file'] + a = getattr(_file, name) + if type(a) != type(0): + setattr(self, name, a) + return a + + def __del__(self): + if os.access(self.name, os.F_OK): + os.unlink(self.name) + + +def PersistentTemporaryFile(suffix="", prefix=""): + """ + Return a temporary file that is available even after being closed on + all platforms. It is automatically deleted when this object is deleted. + Uses tempfile.mkstemp to create the file. The file is opened in mode 'wb'. + """ + if prefix == None: + prefix = "" + fd, name = tempfile.mkstemp(suffix, "libprs500_"+ __version__+"_" + prefix) + _file = os.fdopen(fd, "wb") + return _TemporaryFileWrapper(_file, name)