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

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