home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / Lib / plat-mac / applesingle.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-07-20  |  5.4 KB  |  178 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''Routines to decode AppleSingle files
  5. '''
  6. import struct
  7. import sys
  8.  
  9. try:
  10.     import MacOS
  11.     import Carbon.File as Carbon
  12. except:
  13.     
  14.     class MacOS:
  15.         
  16.         def openrf(path, mode):
  17.             return open(path + '.rsrc', mode)
  18.  
  19.         openrf = classmethod(openrf)
  20.  
  21.     
  22.     class Carbon:
  23.         
  24.         class File:
  25.             
  26.             class FSSpec:
  27.                 pass
  28.  
  29.             
  30.             class FSRef:
  31.                 pass
  32.  
  33.             
  34.             class Alias:
  35.                 pass
  36.  
  37.  
  38.  
  39.  
  40.  
  41. class Error(ValueError):
  42.     pass
  43.  
  44. AS_HEADER_FORMAT = '>LL16sh'
  45. AS_HEADER_LENGTH = 26
  46. AS_MAGIC = 333312
  47. AS_VERSION = 131072
  48. AS_ENTRY_FORMAT = '>lll'
  49. AS_ENTRY_LENGTH = 12
  50. AS_DATAFORK = 1
  51. AS_RESOURCEFORK = 2
  52. AS_IGNORE = (3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15)
  53.  
  54. class AppleSingle(object):
  55.     datafork = None
  56.     resourcefork = None
  57.     
  58.     def __init__(self, fileobj, verbose = False):
  59.         header = fileobj.read(AS_HEADER_LENGTH)
  60.         
  61.         try:
  62.             (magic, version, ig, nentry) = struct.unpack(AS_HEADER_FORMAT, header)
  63.         except ValueError:
  64.             arg = None
  65.             raise Error, 'Unpack header error: %s' % (arg,)
  66.  
  67.         if verbose:
  68.             print 'Magic:   0x%8.8x' % (magic,)
  69.             print 'Version: 0x%8.8x' % (version,)
  70.             print 'Entries: %d' % (nentry,)
  71.         
  72.         if magic != AS_MAGIC:
  73.             raise Error, 'Unknown AppleSingle magic number 0x%8.8x' % (magic,)
  74.         
  75.         if version != AS_VERSION:
  76.             raise Error, 'Unknown AppleSingle version number 0x%8.8x' % (version,)
  77.         
  78.         if nentry <= 0:
  79.             raise Error, 'AppleSingle file contains no forks'
  80.         
  81.         headers = [ fileobj.read(AS_ENTRY_LENGTH) for i in xrange(nentry) ]
  82.         self.forks = []
  83.         for hdr in headers:
  84.             
  85.             try:
  86.                 (restype, offset, length) = struct.unpack(AS_ENTRY_FORMAT, hdr)
  87.             except ValueError:
  88.                 []
  89.                 arg = []
  90.                 []
  91.                 raise Error, 'Unpack entry error: %s' % (arg,)
  92.             except:
  93.                 []
  94.  
  95.             if verbose:
  96.                 print 'Fork %d, offset %d, length %d' % (restype, offset, length)
  97.             
  98.             fileobj.seek(offset)
  99.             data = fileobj.read(length)
  100.             if len(data) != length:
  101.                 raise Error, 'Short read: expected %d bytes got %d' % (length, len(data))
  102.             
  103.             self.forks.append((restype, data))
  104.             if restype == AS_DATAFORK:
  105.                 self.datafork = data
  106.                 continue
  107.             if restype == AS_RESOURCEFORK:
  108.                 self.resourcefork = data
  109.                 continue
  110.         
  111.  
  112.     
  113.     def tofile(self, path, resonly = False):
  114.         outfile = open(path, 'wb')
  115.         data = False
  116.         if resonly:
  117.             if self.resourcefork is None:
  118.                 raise Error, 'No resource fork found'
  119.             
  120.             fp = open(path, 'wb')
  121.             fp.write(self.resourcefork)
  122.             fp.close()
  123.         elif self.resourcefork is None and self.datafork is None:
  124.             raise Error, 'No useful forks found'
  125.         elif self.datafork is not None:
  126.             fp = open(path, 'wb')
  127.             fp.write(self.datafork)
  128.             fp.close()
  129.         
  130.         if self.resourcefork is not None:
  131.             fp = MacOS.openrf(path, '*wb')
  132.             fp.write(self.resourcefork)
  133.             fp.close()
  134.         
  135.  
  136.  
  137.  
  138. def decode(infile, outpath, resonly = False, verbose = False):
  139.     """decode(infile, outpath [, resonly=False, verbose=False])
  140.  
  141.     Creates a decoded file from an AppleSingle encoded file.
  142.     If resonly is True, then it will create a regular file at
  143.     outpath containing only the resource fork from infile.
  144.     Otherwise it will create an AppleDouble file at outpath
  145.     with the data and resource forks from infile.  On platforms
  146.     without the MacOS module, it will create inpath and inpath+'.rsrc'
  147.     with the data and resource forks respectively.
  148.  
  149.     """
  150.     if not hasattr(infile, 'read'):
  151.         if isinstance(infile, Carbon.File.Alias):
  152.             infile = infile.ResolveAlias()[0]
  153.         
  154.         if isinstance(infile, (Carbon.File.FSSpec, Carbon.File.FSRef)):
  155.             infile = infile.as_pathname()
  156.         
  157.         infile = open(infile, 'rb')
  158.     
  159.     as = AppleSingle(infile, verbose = verbose)
  160.     as.tofile(outpath, resonly = resonly)
  161.  
  162.  
  163. def _test():
  164.     if (len(sys.argv) < 3 or sys.argv[1] == '-r') and len(sys.argv) != 4:
  165.         print 'Usage: applesingle.py [-r] applesinglefile decodedfile'
  166.         sys.exit(1)
  167.     
  168.     if sys.argv[1] == '-r':
  169.         resonly = True
  170.         del sys.argv[1]
  171.     else:
  172.         resonly = False
  173.     decode(sys.argv[1], sys.argv[2], resonly = resonly)
  174.  
  175. if __name__ == '__main__':
  176.     _test()
  177.  
  178.