Add tempfile utility function.

This commit is contained in:
Bastian Kleineidam 2011-01-06 09:52:11 +01:00
parent 264e574624
commit 5f70b7210f
2 changed files with 15 additions and 7 deletions

View file

@ -27,11 +27,11 @@ import time
import errno
import socket
import select
import tempfile
from . import absolute_url, StoringHandler, get_url_from
from .. import (log, LOG_CHECK, LOG_CACHE, httputil, httplib2 as httplib,
strformat, LinkCheckerError, url as urlutil, trace, clamav, winutil, geoip)
strformat, LinkCheckerError, url as urlutil, trace, clamav, winutil, geoip,
fileutil)
from ..HtmlParser import htmlsax
from ..htmlutil import linkparse
from ..network import iputil
@ -1010,12 +1010,12 @@ class UrlBase (object):
def get_temp_filename (self):
"""Get temporary filename for content to parse."""
# XXX move to utility package
# store content in temporary file
fd, filename = tempfile.mkstemp(suffix='.doc', prefix='lc_')
fp = os.fdopen(fd)
fp.write(self.get_content())
fp.close()
fd, filename = fileutil.get_tempfile(suffix='.doc', prefix='lc_')
try:
fd.write(self.get_content())
finally:
fd.close()
def serialized (self):
"""

View file

@ -24,6 +24,7 @@ import locale
import stat
import fnmatch
import mimetypes
import tempfile
def write_file (filename, content, backup=False, callback=None):
@ -207,3 +208,10 @@ def guess_mimetype (filename, read=None):
# split off not needed extension info
mime = mime.split(';')[0]
return mime.strip().lower()
def get_temp_file (**kwargs):
"""Return tuple (open file object, filename) pointing to a temporary
file."""
fd, filename = tempfile.mkstemp(**kwargs)
return os.fdopen(fd), filename