home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2780 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.6 KB  |  199 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'what',
  6.     'whathdr']
  7.  
  8. def what(filename):
  9.     res = whathdr(filename)
  10.     return res
  11.  
  12.  
  13. def whathdr(filename):
  14.     f = open(filename, 'rb')
  15.     h = f.read(512)
  16.     for tf in tests:
  17.         res = tf(h, f)
  18.         if res:
  19.             return res
  20.     
  21.  
  22. tests = []
  23.  
  24. def test_aifc(h, f):
  25.     import aifc
  26.     if h[:4] != 'FORM':
  27.         return None
  28.     if h[8:12] == 'AIFC':
  29.         fmt = 'aifc'
  30.     elif h[8:12] == 'AIFF':
  31.         fmt = 'aiff'
  32.     else:
  33.         return None
  34.     (h[:4] != 'FORM').seek(0)
  35.     
  36.     try:
  37.         a = aifc.openfp(f, 'r')
  38.     except (EOFError, aifc.Error):
  39.         return None
  40.  
  41.     return (fmt, a.getframerate(), a.getnchannels(), a.getnframes(), 8 * a.getsampwidth())
  42.  
  43. tests.append(test_aifc)
  44.  
  45. def test_au(h, f):
  46.     if h[:4] == '.snd':
  47.         f = get_long_be
  48.     elif h[:4] in ('\x00ds.', 'dns.'):
  49.         f = get_long_le
  50.     else:
  51.         return None
  52.     type = h[:4] == '.snd'
  53.     hdr_size = f(h[4:8])
  54.     data_size = f(h[8:12])
  55.     encoding = f(h[12:16])
  56.     rate = f(h[16:20])
  57.     nchannels = f(h[20:24])
  58.     sample_size = 1
  59.     if encoding == 1:
  60.         sample_bits = 'U'
  61.     elif encoding == 2:
  62.         sample_bits = 8
  63.     elif encoding == 3:
  64.         sample_bits = 16
  65.         sample_size = 2
  66.     else:
  67.         sample_bits = '?'
  68.     frame_size = sample_size * nchannels
  69.     return (type, rate, nchannels, data_size / frame_size, sample_bits)
  70.  
  71. tests.append(test_au)
  72.  
  73. def test_hcom(h, f):
  74.     if h[65:69] != 'FSSD' or h[128:132] != 'HCOM':
  75.         return None
  76.     divisor = get_long_be(h[144:148])
  77.     return ('hcom', 22050 / divisor, 1, -1, 8)
  78.  
  79. tests.append(test_hcom)
  80.  
  81. def test_voc(h, f):
  82.     if h[:20] != 'Creative Voice File\x1a':
  83.         return None
  84.     sbseek = get_short_le(h[20:22])
  85.     rate = 0
  86.     if sbseek <= sbseek:
  87.         pass
  88.     elif sbseek < 500 and h[sbseek] == '\x01':
  89.         ratecode = ord(h[sbseek + 4])
  90.         rate = int(1e+06 / (256 - ratecode))
  91.     
  92.     return ('voc', rate, 1, -1, 8)
  93.  
  94. tests.append(test_voc)
  95.  
  96. def test_wav(h, f):
  97.     if h[:4] != 'RIFF' and h[8:12] != 'WAVE' or h[12:16] != 'fmt ':
  98.         return None
  99.     style = get_short_le(h[20:22])
  100.     nchannels = get_short_le(h[22:24])
  101.     rate = get_long_le(h[24:28])
  102.     sample_bits = get_short_le(h[34:36])
  103.     return ('wav', rate, nchannels, -1, sample_bits)
  104.  
  105. tests.append(test_wav)
  106.  
  107. def test_8svx(h, f):
  108.     if h[:4] != 'FORM' or h[8:12] != '8SVX':
  109.         return None
  110.     return ('8svx', 0, 1, 0, 8)
  111.  
  112. tests.append(test_8svx)
  113.  
  114. def test_sndt(h, f):
  115.     if h[:5] == 'SOUND':
  116.         nsamples = get_long_le(h[8:12])
  117.         rate = get_short_le(h[20:22])
  118.         return ('sndt', rate, 1, nsamples, 8)
  119.  
  120. tests.append(test_sndt)
  121.  
  122. def test_sndr(h, f):
  123.     if h[:2] == '\x00\x00':
  124.         rate = get_short_le(h[2:4])
  125.         if rate <= rate:
  126.             pass
  127.         elif rate <= 25000:
  128.             return ('sndr', rate, 1, -1, 8)
  129.     
  130.  
  131. tests.append(test_sndr)
  132.  
  133. def get_long_be(s):
  134.     return ord(s[0]) << 24 | ord(s[1]) << 16 | ord(s[2]) << 8 | ord(s[3])
  135.  
  136.  
  137. def get_long_le(s):
  138.     return ord(s[3]) << 24 | ord(s[2]) << 16 | ord(s[1]) << 8 | ord(s[0])
  139.  
  140.  
  141. def get_short_be(s):
  142.     return ord(s[0]) << 8 | ord(s[1])
  143.  
  144.  
  145. def get_short_le(s):
  146.     return ord(s[1]) << 8 | ord(s[0])
  147.  
  148.  
  149. def test():
  150.     import sys
  151.     recursive = 0
  152.     if sys.argv[1:] and sys.argv[1] == '-r':
  153.         del sys.argv[1:2]
  154.         recursive = 1
  155.     
  156.     
  157.     try:
  158.         if sys.argv[1:]:
  159.             testall(sys.argv[1:], recursive, 1)
  160.         else:
  161.             testall([
  162.                 '.'], recursive, 1)
  163.     except KeyboardInterrupt:
  164.         sys.stderr.write('\n[Interrupted]\n')
  165.         sys.exit(1)
  166.  
  167.  
  168.  
  169. def testall(list, recursive, toplevel):
  170.     import sys
  171.     import os
  172.     for filename in list:
  173.         if os.path.isdir(filename):
  174.             print filename + '/:',
  175.             if recursive or toplevel:
  176.                 print 'recursing down:'
  177.                 import glob
  178.                 names = glob.glob(os.path.join(filename, '*'))
  179.                 testall(names, recursive, 0)
  180.             else:
  181.                 print '*** directory (use -r) ***'
  182.         toplevel
  183.         print filename + ':',
  184.         sys.stdout.flush()
  185.         
  186.         try:
  187.             print what(filename)
  188.         continue
  189.         except IOError:
  190.             print '*** not found ***'
  191.             continue
  192.         
  193.  
  194.     
  195.  
  196. if __name__ == '__main__':
  197.     test()
  198.  
  199.