home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / tempfile.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  4.7 KB  |  150 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import os
  5. tempdir = None
  6. template = None
  7.  
  8. def gettempdir():
  9.     global tempdir
  10.     if tempdir is not None:
  11.         return tempdir
  12.     
  13.     
  14.     try:
  15.         pwd = os.getcwd()
  16.     except (AttributeError, os.error):
  17.         pwd = os.curdir
  18.  
  19.     attempdirs = [
  20.         '/usr/tmp',
  21.         '/tmp',
  22.         pwd]
  23.     if os.name == 'nt':
  24.         attempdirs.insert(0, 'C:\\TEMP')
  25.         attempdirs.insert(0, '\\TEMP')
  26.     elif os.name == 'mac':
  27.         import macfs
  28.         import MACFS
  29.         
  30.         try:
  31.             (refnum, dirid) = macfs.FindFolder(MACFS.kOnSystemDisk, MACFS.kTemporaryFolderType, 1)
  32.             dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
  33.             attempdirs.insert(0, dirname)
  34.         except macfs.error:
  35.             pass
  36.  
  37.     
  38.     for envname in ('TMPDIR', 'TEMP', 'TMP'):
  39.         pass
  40.     
  41.     testfile = gettempprefix() + 'test'
  42.     for dir in attempdirs:
  43.         
  44.         try:
  45.             filename = os.path.join(dir, testfile)
  46.             fp = open(filename, 'w')
  47.             fp.write('blat')
  48.             fp.close()
  49.             os.unlink(filename)
  50.             tempdir = dir
  51.         except IOError:
  52.             0
  53.             0
  54.             attempdirs
  55.         except:
  56.             None if os.environ.has_key(envname) else ('TMPDIR', 'TEMP', 'TMP')
  57.  
  58.     
  59.     if tempdir is None:
  60.         msg = "Can't find a usable temporary directory amongst " + `attempdirs`
  61.         raise IOError, msg
  62.     
  63.     return tempdir
  64.  
  65. _pid = None
  66.  
  67. def gettempprefix():
  68.     global template, _pid, template, template, template, template
  69.     if os.name == 'posix' and _pid and _pid != os.getpid():
  70.         template = None
  71.     
  72.     if template is None:
  73.         if os.name == 'posix':
  74.             _pid = os.getpid()
  75.             template = '@' + `_pid` + '.'
  76.         elif os.name == 'nt':
  77.             template = '~' + `os.getpid()` + '-'
  78.         elif os.name == 'mac':
  79.             template = 'Python-Tmp-'
  80.         else:
  81.             template = 'tmp'
  82.     
  83.     return template
  84.  
  85. counter = 0
  86.  
  87. def mktemp(suffix = ''):
  88.     global counter
  89.     dir = gettempdir()
  90.     pre = gettempprefix()
  91.     while 1:
  92.         counter = counter + 1
  93.         file = os.path.join(dir, pre + `counter` + suffix)
  94.         if not os.path.exists(file):
  95.             return file
  96.         
  97.  
  98.  
  99. class TemporaryFileWrapper:
  100.     '''Temporary file wrapper
  101.  
  102.     This class provides a wrapper around files opened for temporary use.
  103.     In particular, it seeks to automatically remove the file when it is
  104.     no longer needed.
  105.     '''
  106.     
  107.     def __init__(self, file, path):
  108.         self.file = file
  109.         self.path = path
  110.  
  111.     
  112.     def close(self):
  113.         self.file.close()
  114.         os.unlink(self.path)
  115.  
  116.     
  117.     def __del__(self):
  118.         
  119.         try:
  120.             self.close()
  121.         except:
  122.             pass
  123.  
  124.  
  125.     
  126.     def __getattr__(self, name):
  127.         file = self.__dict__['file']
  128.         a = getattr(file, name)
  129.         setattr(self, name, a)
  130.         return a
  131.  
  132.  
  133.  
  134. def TemporaryFile(mode = 'w+b', bufsize = -1, suffix = ''):
  135.     name = mktemp(suffix)
  136.     if os.name == 'posix':
  137.         fd = os.open(name, os.O_RDWR | os.O_CREAT | os.O_EXCL, 448)
  138.         
  139.         try:
  140.             os.unlink(name)
  141.             return os.fdopen(fd, mode, bufsize)
  142.         except:
  143.             os.close(fd)
  144.             raise 
  145.  
  146.     else:
  147.         file = open(name, mode, bufsize)
  148.         return TemporaryFileWrapper(file, name)
  149.  
  150.