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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import posixpath
  6. import urllib
  7. __all__ = [
  8.     'guess_type',
  9.     'guess_extension',
  10.     'guess_all_extensions',
  11.     'add_type',
  12.     'read_mime_types',
  13.     'init']
  14. knownfiles = [
  15.     '/etc/mime.types',
  16.     '/etc/httpd/mime.types',
  17.     '/etc/httpd/conf/mime.types',
  18.     '/etc/apache/mime.types',
  19.     '/etc/apache2/mime.types',
  20.     '/usr/local/etc/httpd/conf/mime.types',
  21.     '/usr/local/lib/netscape/mime.types',
  22.     '/usr/local/etc/httpd/conf/mime.types',
  23.     '/usr/local/etc/mime.types']
  24. inited = False
  25. _db = None
  26.  
  27. class MimeTypes:
  28.     
  29.     def __init__(self, filenames = (), strict = True):
  30.         if not inited:
  31.             init()
  32.         
  33.         self.encodings_map = encodings_map.copy()
  34.         self.suffix_map = suffix_map.copy()
  35.         self.types_map = ({ }, { })
  36.         self.types_map_inv = ({ }, { })
  37.         for ext, type in types_map.items():
  38.             self.add_type(type, ext, True)
  39.         
  40.         for ext, type in common_types.items():
  41.             self.add_type(type, ext, False)
  42.         
  43.         for name in filenames:
  44.             self.read(name, strict)
  45.         
  46.  
  47.     
  48.     def add_type(self, type, ext, strict = True):
  49.         self.types_map[strict][ext] = type
  50.         exts = self.types_map_inv[strict].setdefault(type, [])
  51.         if ext not in exts:
  52.             exts.append(ext)
  53.         
  54.  
  55.     
  56.     def guess_type(self, url, strict = True):
  57.         (scheme, url) = urllib.splittype(url)
  58.         if scheme == 'data':
  59.             comma = url.find(',')
  60.             if comma < 0:
  61.                 return (None, None)
  62.             semi = url.find(';', 0, comma)
  63.             if semi >= 0:
  64.                 type = url[:semi]
  65.             else:
  66.                 type = url[:comma]
  67.             if '=' in type or '/' not in type:
  68.                 type = 'text/plain'
  69.             
  70.             return (type, None)
  71.         (base, ext) = posixpath.splitext(url)
  72.         while ext in self.suffix_map:
  73.             (base, ext) = posixpath.splitext(base + self.suffix_map[ext])
  74.             continue
  75.             scheme == 'data'
  76.         if ext in self.encodings_map:
  77.             encoding = self.encodings_map[ext]
  78.             (base, ext) = posixpath.splitext(base)
  79.         else:
  80.             encoding = None
  81.         types_map = self.types_map[True]
  82.         if ext in types_map:
  83.             return (types_map[ext], encoding)
  84.         if ext.lower() in types_map:
  85.             return (types_map[ext.lower()], encoding)
  86.         if strict:
  87.             return (None, encoding)
  88.         types_map = self.types_map[False]
  89.         if ext in types_map:
  90.             return (types_map[ext], encoding)
  91.         if ext.lower() in types_map:
  92.             return (types_map[ext.lower()], encoding)
  93.         return (None, encoding)
  94.  
  95.     
  96.     def guess_all_extensions(self, type, strict = True):
  97.         type = type.lower()
  98.         extensions = self.types_map_inv[True].get(type, [])
  99.         if not strict:
  100.             for ext in self.types_map_inv[False].get(type, []):
  101.                 if ext not in extensions:
  102.                     extensions.append(ext)
  103.                     continue
  104.             
  105.         
  106.         return extensions
  107.  
  108.     
  109.     def guess_extension(self, type, strict = True):
  110.         extensions = self.guess_all_extensions(type, strict)
  111.         if not extensions:
  112.             return None
  113.         return extensions[0]
  114.  
  115.     
  116.     def read(self, filename, strict = True):
  117.         fp = open(filename)
  118.         self.readfp(fp, strict)
  119.         fp.close()
  120.  
  121.     
  122.     def readfp(self, fp, strict = True):
  123.         while None:
  124.             line = fp.readline()
  125.             if not line:
  126.                 break
  127.             
  128.             words = line.split()
  129.             for i in range(len(words)):
  130.                 if words[i][0] == '#':
  131.                     del words[i:]
  132.                     break
  133.                     continue
  134.             
  135.             if not words:
  136.                 continue
  137.             
  138.             type = words[0]
  139.             suffixes = words[1:]
  140.             for suff in suffixes:
  141.                 self.add_type(type, '.' + suff, strict)
  142.             
  143.             continue
  144.             return None
  145.  
  146.  
  147.  
  148. def guess_type(url, strict = True):
  149.     if _db is None:
  150.         init()
  151.     
  152.     return _db.guess_type(url, strict)
  153.  
  154.  
  155. def guess_all_extensions(type, strict = True):
  156.     if _db is None:
  157.         init()
  158.     
  159.     return _db.guess_all_extensions(type, strict)
  160.  
  161.  
  162. def guess_extension(type, strict = True):
  163.     if _db is None:
  164.         init()
  165.     
  166.     return _db.guess_extension(type, strict)
  167.  
  168.  
  169. def add_type(type, ext, strict = True):
  170.     if _db is None:
  171.         init()
  172.     
  173.     return _db.add_type(type, ext, strict)
  174.  
  175.  
  176. def init(files = None):
  177.     global inited, encodings_map, suffix_map, types_map, common_types, _db
  178.     inited = True
  179.     db = MimeTypes()
  180.     if files is None:
  181.         files = knownfiles
  182.     
  183.     for file in files:
  184.         if os.path.isfile(file):
  185.             db.readfp(open(file))
  186.             continue
  187.     
  188.     encodings_map = db.encodings_map
  189.     suffix_map = db.suffix_map
  190.     types_map = db.types_map[True]
  191.     common_types = db.types_map[False]
  192.     _db = db
  193.  
  194.  
  195. def read_mime_types(file):
  196.     
  197.     try:
  198.         f = open(file)
  199.     except IOError:
  200.         return None
  201.  
  202.     db = MimeTypes()
  203.     db.readfp(f, True)
  204.     return db.types_map[True]
  205.  
  206.  
  207. def _default_mime_types():
  208.     global suffix_map, encodings_map, types_map, common_types
  209.     suffix_map = {
  210.         '.tgz': '.tar.gz',
  211.         '.taz': '.tar.gz',
  212.         '.tz': '.tar.gz',
  213.         '.tbz2': '.tar.bz2' }
  214.     encodings_map = {
  215.         '.gz': 'gzip',
  216.         '.Z': 'compress',
  217.         '.bz2': 'bzip2' }
  218.     types_map = {
  219.         '.a': 'application/octet-stream',
  220.         '.ai': 'application/postscript',
  221.         '.aif': 'audio/x-aiff',
  222.         '.aifc': 'audio/x-aiff',
  223.         '.aiff': 'audio/x-aiff',
  224.         '.au': 'audio/basic',
  225.         '.avi': 'video/x-msvideo',
  226.         '.bat': 'text/plain',
  227.         '.bcpio': 'application/x-bcpio',
  228.         '.bin': 'application/octet-stream',
  229.         '.bmp': 'image/x-ms-bmp',
  230.         '.c': 'text/plain',
  231.         '.cdf': 'application/x-cdf',
  232.         '.cdf': 'application/x-netcdf',
  233.         '.cpio': 'application/x-cpio',
  234.         '.csh': 'application/x-csh',
  235.         '.css': 'text/css',
  236.         '.dll': 'application/octet-stream',
  237.         '.doc': 'application/msword',
  238.         '.dot': 'application/msword',
  239.         '.dvi': 'application/x-dvi',
  240.         '.eml': 'message/rfc822',
  241.         '.eps': 'application/postscript',
  242.         '.etx': 'text/x-setext',
  243.         '.exe': 'application/octet-stream',
  244.         '.gif': 'image/gif',
  245.         '.gtar': 'application/x-gtar',
  246.         '.h': 'text/plain',
  247.         '.hdf': 'application/x-hdf',
  248.         '.htm': 'text/html',
  249.         '.html': 'text/html',
  250.         '.ief': 'image/ief',
  251.         '.jpe': 'image/jpeg',
  252.         '.jpeg': 'image/jpeg',
  253.         '.jpg': 'image/jpeg',
  254.         '.js': 'application/x-javascript',
  255.         '.ksh': 'text/plain',
  256.         '.latex': 'application/x-latex',
  257.         '.m1v': 'video/mpeg',
  258.         '.man': 'application/x-troff-man',
  259.         '.me': 'application/x-troff-me',
  260.         '.mht': 'message/rfc822',
  261.         '.mhtml': 'message/rfc822',
  262.         '.mif': 'application/x-mif',
  263.         '.mov': 'video/quicktime',
  264.         '.movie': 'video/x-sgi-movie',
  265.         '.mp2': 'audio/mpeg',
  266.         '.mp3': 'audio/mpeg',
  267.         '.mp4': 'video/mp4',
  268.         '.mpa': 'video/mpeg',
  269.         '.mpe': 'video/mpeg',
  270.         '.mpeg': 'video/mpeg',
  271.         '.mpg': 'video/mpeg',
  272.         '.ms': 'application/x-troff-ms',
  273.         '.nc': 'application/x-netcdf',
  274.         '.nws': 'message/rfc822',
  275.         '.o': 'application/octet-stream',
  276.         '.obj': 'application/octet-stream',
  277.         '.oda': 'application/oda',
  278.         '.p12': 'application/x-pkcs12',
  279.         '.p7c': 'application/pkcs7-mime',
  280.         '.pbm': 'image/x-portable-bitmap',
  281.         '.pdf': 'application/pdf',
  282.         '.pfx': 'application/x-pkcs12',
  283.         '.pgm': 'image/x-portable-graymap',
  284.         '.pl': 'text/plain',
  285.         '.png': 'image/png',
  286.         '.pnm': 'image/x-portable-anymap',
  287.         '.pot': 'application/vnd.ms-powerpoint',
  288.         '.ppa': 'application/vnd.ms-powerpoint',
  289.         '.ppm': 'image/x-portable-pixmap',
  290.         '.pps': 'application/vnd.ms-powerpoint',
  291.         '.ppt': 'application/vnd.ms-powerpoint',
  292.         '.ps': 'application/postscript',
  293.         '.pwz': 'application/vnd.ms-powerpoint',
  294.         '.py': 'text/x-python',
  295.         '.pyc': 'application/x-python-code',
  296.         '.pyo': 'application/x-python-code',
  297.         '.qt': 'video/quicktime',
  298.         '.ra': 'audio/x-pn-realaudio',
  299.         '.ram': 'application/x-pn-realaudio',
  300.         '.ras': 'image/x-cmu-raster',
  301.         '.rdf': 'application/xml',
  302.         '.rgb': 'image/x-rgb',
  303.         '.roff': 'application/x-troff',
  304.         '.rtx': 'text/richtext',
  305.         '.sgm': 'text/x-sgml',
  306.         '.sgml': 'text/x-sgml',
  307.         '.sh': 'application/x-sh',
  308.         '.shar': 'application/x-shar',
  309.         '.snd': 'audio/basic',
  310.         '.so': 'application/octet-stream',
  311.         '.src': 'application/x-wais-source',
  312.         '.sv4cpio': 'application/x-sv4cpio',
  313.         '.sv4crc': 'application/x-sv4crc',
  314.         '.swf': 'application/x-shockwave-flash',
  315.         '.t': 'application/x-troff',
  316.         '.tar': 'application/x-tar',
  317.         '.tcl': 'application/x-tcl',
  318.         '.tex': 'application/x-tex',
  319.         '.texi': 'application/x-texinfo',
  320.         '.texinfo': 'application/x-texinfo',
  321.         '.tif': 'image/tiff',
  322.         '.tiff': 'image/tiff',
  323.         '.tr': 'application/x-troff',
  324.         '.tsv': 'text/tab-separated-values',
  325.         '.txt': 'text/plain',
  326.         '.ustar': 'application/x-ustar',
  327.         '.vcf': 'text/x-vcard',
  328.         '.wav': 'audio/x-wav',
  329.         '.wiz': 'application/msword',
  330.         '.wsdl': 'application/xml',
  331.         '.xbm': 'image/x-xbitmap',
  332.         '.xlb': 'application/vnd.ms-excel',
  333.         '.xls': 'application/excel',
  334.         '.xls': 'application/vnd.ms-excel',
  335.         '.xml': 'text/xml',
  336.         '.xpdl': 'application/xml',
  337.         '.xpm': 'image/x-xpixmap',
  338.         '.xsl': 'application/xml',
  339.         '.xwd': 'image/x-xwindowdump',
  340.         '.zip': 'application/zip' }
  341.     common_types = {
  342.         '.jpg': 'image/jpg',
  343.         '.mid': 'audio/midi',
  344.         '.midi': 'audio/midi',
  345.         '.pct': 'image/pict',
  346.         '.pic': 'image/pict',
  347.         '.pict': 'image/pict',
  348.         '.rtf': 'application/rtf',
  349.         '.xul': 'text/xul' }
  350.  
  351. _default_mime_types()
  352. if __name__ == '__main__':
  353.     import sys
  354.     import getopt
  355.     USAGE = 'Usage: mimetypes.py [options] type\n\nOptions:\n    --help / -h       -- print this message and exit\n    --lenient / -l    -- additionally search of some common, but non-standard\n                         types.\n    --extension / -e  -- guess extension instead of type\n\nMore than one type argument may be given.\n'
  356.     
  357.     def usage(code, msg = ''):
  358.         print USAGE
  359.         if msg:
  360.             print msg
  361.         
  362.         sys.exit(code)
  363.  
  364.     
  365.     try:
  366.         (opts, args) = getopt.getopt(sys.argv[1:], 'hle', [
  367.             'help',
  368.             'lenient',
  369.             'extension'])
  370.     except getopt.error:
  371.         msg = None
  372.         usage(1, msg)
  373.  
  374.     strict = 1
  375.     extension = 0
  376.     for opt, arg in opts:
  377.         if opt in ('-h', '--help'):
  378.             usage(0)
  379.             continue
  380.         if opt in ('-l', '--lenient'):
  381.             strict = 0
  382.             continue
  383.         if opt in ('-e', '--extension'):
  384.             extension = 1
  385.             continue
  386.     
  387.     for gtype in args:
  388.         if extension:
  389.             guess = guess_extension(gtype, strict)
  390.             if not guess:
  391.                 print "I don't know anything about type", gtype
  392.             else:
  393.                 print guess
  394.         guess
  395.         (guess, encoding) = guess_type(gtype, strict)
  396.         if not guess:
  397.             print "I don't know anything about type", gtype
  398.             continue
  399.         print 'type:', guess, 'encoding:', encoding
  400.     
  401.  
  402.