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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'what']
  6.  
  7. def what(file, h = None):
  8.     if h is None:
  9.         if isinstance(file, basestring):
  10.             f = open(file, 'rb')
  11.             h = f.read(32)
  12.         else:
  13.             location = file.tell()
  14.             h = file.read(32)
  15.             file.seek(location)
  16.             f = None
  17.     else:
  18.         f = None
  19.     
  20.     try:
  21.         for tf in tests:
  22.             res = tf(h, f)
  23.             if res:
  24.                 return res
  25.     finally:
  26.         if f:
  27.             f.close()
  28.         
  29.  
  30.  
  31. tests = []
  32.  
  33. def test_jpeg(h, f):
  34.     if h[6:10] == 'JFIF':
  35.         return 'jpeg'
  36.  
  37. tests.append(test_jpeg)
  38.  
  39. def test_exif(h, f):
  40.     if h[6:10] == 'Exif':
  41.         return 'jpeg'
  42.  
  43. tests.append(test_exif)
  44.  
  45. def test_png(h, f):
  46.     if h[:8] == '\x89PNG\r\n\x1a\n':
  47.         return 'png'
  48.  
  49. tests.append(test_png)
  50.  
  51. def test_gif(h, f):
  52.     if h[:6] in ('GIF87a', 'GIF89a'):
  53.         return 'gif'
  54.  
  55. tests.append(test_gif)
  56.  
  57. def test_tiff(h, f):
  58.     if h[:2] in ('MM', 'II'):
  59.         return 'tiff'
  60.  
  61. tests.append(test_tiff)
  62.  
  63. def test_rgb(h, f):
  64.     if h[:2] == '\x01\xda':
  65.         return 'rgb'
  66.  
  67. tests.append(test_rgb)
  68.  
  69. def test_pbm(h, f):
  70.     if len(h) >= 3 and h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
  71.         return 'pbm'
  72.  
  73. tests.append(test_pbm)
  74.  
  75. def test_pgm(h, f):
  76.     if len(h) >= 3 and h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
  77.         return 'pgm'
  78.  
  79. tests.append(test_pgm)
  80.  
  81. def test_ppm(h, f):
  82.     if len(h) >= 3 and h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
  83.         return 'ppm'
  84.  
  85. tests.append(test_ppm)
  86.  
  87. def test_rast(h, f):
  88.     if h[:4] == 'Y\xa6j\x95':
  89.         return 'rast'
  90.  
  91. tests.append(test_rast)
  92.  
  93. def test_xbm(h, f):
  94.     s = '#define '
  95.     if h[:len(s)] == s:
  96.         return 'xbm'
  97.  
  98. tests.append(test_xbm)
  99.  
  100. def test_bmp(h, f):
  101.     if h[:2] == 'BM':
  102.         return 'bmp'
  103.  
  104. tests.append(test_bmp)
  105.  
  106. def test():
  107.     import sys
  108.     recursive = 0
  109.     if sys.argv[1:] and sys.argv[1] == '-r':
  110.         del sys.argv[1:2]
  111.         recursive = 1
  112.     
  113.     
  114.     try:
  115.         if sys.argv[1:]:
  116.             testall(sys.argv[1:], recursive, 1)
  117.         else:
  118.             testall([
  119.                 '.'], recursive, 1)
  120.     except KeyboardInterrupt:
  121.         sys.stderr.write('\n[Interrupted]\n')
  122.         sys.exit(1)
  123.  
  124.  
  125.  
  126. def testall(list, recursive, toplevel):
  127.     import sys
  128.     import os
  129.     for filename in list:
  130.         if os.path.isdir(filename):
  131.             print filename + '/:',
  132.             if recursive or toplevel:
  133.                 print 'recursing down:'
  134.                 import glob
  135.                 names = glob.glob(os.path.join(filename, '*'))
  136.                 testall(names, recursive, 0)
  137.             else:
  138.                 print '*** directory (use -r) ***'
  139.         toplevel
  140.         print filename + ':',
  141.         sys.stdout.flush()
  142.         
  143.         try:
  144.             print what(filename)
  145.         continue
  146.         except IOError:
  147.             print '*** not found ***'
  148.             continue
  149.         
  150.  
  151.     
  152.  
  153.