home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / posixfile.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  5.2 KB  |  159 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import warnings
  5. warnings.warn('The posixfile module is deprecated; fcntl.lockf() provides better locking', DeprecationWarning, 2)
  6.  
  7. class _posixfile_:
  8.     states = [
  9.         'open',
  10.         'closed']
  11.     
  12.     def __repr__(self):
  13.         file = self._file_
  14.         return "<%s posixfile '%s', mode '%s' at %s>" % (self.states[file.closed], file.name, file.mode, hex(id(self))[2:])
  15.  
  16.     
  17.     def open(self, name, mode = 'r', bufsize = -1):
  18.         import __builtin__
  19.         return self.fileopen(__builtin__.open(name, mode, bufsize))
  20.  
  21.     
  22.     def fileopen(self, file):
  23.         import types
  24.         if repr(type(file)) != "<type 'file'>":
  25.             raise TypeError, 'posixfile.fileopen() arg must be file object'
  26.         self._file_ = file
  27.         for maybemethod in dir(file):
  28.             if not maybemethod.startswith('_'):
  29.                 attr = getattr(file, maybemethod)
  30.                 if isinstance(attr, types.BuiltinMethodType):
  31.                     setattr(self, maybemethod, attr)
  32.                 
  33.         return self
  34.  
  35.     
  36.     def file(self):
  37.         return self._file_
  38.  
  39.     
  40.     def dup(self):
  41.         import posix
  42.         if not hasattr(posix, 'fdopen'):
  43.             raise AttributeError, 'dup() method unavailable'
  44.         return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
  45.  
  46.     
  47.     def dup2(self, fd):
  48.         import posix
  49.         if not hasattr(posix, 'fdopen'):
  50.             raise AttributeError, 'dup() method unavailable'
  51.         posix.dup2(self._file_.fileno(), fd)
  52.         return posix.fdopen(fd, self._file_.mode)
  53.  
  54.     
  55.     def flags(self, *which):
  56.         import fcntl
  57.         import os
  58.         if which:
  59.             if len(which) > 1:
  60.                 raise TypeError, 'Too many arguments'
  61.             which = which[0]
  62.         else:
  63.             which = '?'
  64.         l_flags = 0
  65.         if 'n' in which:
  66.             l_flags = l_flags | os.O_NDELAY
  67.         if 'a' in which:
  68.             l_flags = l_flags | os.O_APPEND
  69.         if 's' in which:
  70.             l_flags = l_flags | os.O_SYNC
  71.         file = self._file_
  72.         if '=' not in which:
  73.             cur_fl = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)
  74.             if '!' in which:
  75.                 l_flags = cur_fl & ~l_flags
  76.             else:
  77.                 l_flags = cur_fl | l_flags
  78.         l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFL, l_flags)
  79.         if 'c' in which:
  80.             arg = '!' not in which
  81.             l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFD, arg)
  82.         if '?' in which:
  83.             which = ''
  84.             l_flags = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)
  85.             if os.O_APPEND & l_flags:
  86.                 which = which + 'a'
  87.             if fcntl.fcntl(file.fileno(), fcntl.F_GETFD, 0) & 1:
  88.                 which = which + 'c'
  89.             if os.O_NDELAY & l_flags:
  90.                 which = which + 'n'
  91.             if os.O_SYNC & l_flags:
  92.                 which = which + 's'
  93.             return which
  94.  
  95.     
  96.     def lock(self, how, *args):
  97.         import struct
  98.         import fcntl
  99.         if 'w' in how:
  100.             l_type = fcntl.F_WRLCK
  101.         elif 'r' in how:
  102.             l_type = fcntl.F_RDLCK
  103.         elif 'u' in how:
  104.             l_type = fcntl.F_UNLCK
  105.         else:
  106.             raise TypeError, 'no type of lock specified'
  107.         if None in how:
  108.             cmd = fcntl.F_SETLKW
  109.         elif '?' in how:
  110.             cmd = fcntl.F_GETLK
  111.         else:
  112.             cmd = fcntl.F_SETLK
  113.         l_whence = 0
  114.         l_start = 0
  115.         l_len = 0
  116.         if len(args) == 1:
  117.             l_len = args[0]
  118.         elif len(args) == 2:
  119.             (l_len, l_start) = args
  120.         elif len(args) == 3:
  121.             (l_len, l_start, l_whence) = args
  122.         elif len(args) > 3:
  123.             raise TypeError, 'too many arguments'
  124.         import sys
  125.         import os
  126.         if sys.platform in ('netbsd1', 'openbsd2', 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8', 'bsdos2', 'bsdos3', 'bsdos4'):
  127.             flock = struct.pack('lxxxxlxxxxlhh', l_start, l_len, os.getpid(), l_type, l_whence)
  128.         elif sys.platform in ('aix3', 'aix4'):
  129.             flock = struct.pack('hhlllii', l_type, l_whence, l_start, l_len, 0, 0, 0)
  130.         else:
  131.             flock = struct.pack('hhllhh', l_type, l_whence, l_start, l_len, 0, 0)
  132.         flock = fcntl.fcntl(self._file_.fileno(), cmd, flock)
  133.         if '?' in how:
  134.             if sys.platform in ('netbsd1', 'openbsd2', 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'bsdos2', 'bsdos3', 'bsdos4'):
  135.                 (l_start, l_len, l_pid, l_type, l_whence) = struct.unpack('lxxxxlxxxxlhh', flock)
  136.             elif sys.platform in ('aix3', 'aix4'):
  137.                 (l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs) = struct.unpack('hhlllii', flock)
  138.             elif sys.platform == 'linux2':
  139.                 (l_type, l_whence, l_start, l_len, l_pid, l_sysid) = struct.unpack('hhllhh', flock)
  140.             else:
  141.                 (l_type, l_whence, l_start, l_len, l_sysid, l_pid) = struct.unpack('hhllhh', flock)
  142.             if l_type != fcntl.F_UNLCK:
  143.                 if l_type == fcntl.F_RDLCK:
  144.                     return ('r', l_len, l_start, l_whence, l_pid)
  145.                 return (None, l_len, l_start, l_whence, l_pid)
  146.  
  147.  
  148.  
  149. def open(name, mode = 'r', bufsize = -1):
  150.     return _posixfile_().open(name, mode, bufsize)
  151.  
  152.  
  153. def fileopen(file):
  154.     return _posixfile_().fileopen(file)
  155.  
  156. SEEK_SET = 0
  157. SEEK_CUR = 1
  158. SEEK_END = 2
  159.