home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / mimetypes.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  10.6 KB  |  396 lines

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