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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. __all__ = [
  6.     'getcaps',
  7.     'findmatch']
  8.  
  9. def getcaps():
  10.     caps = { }
  11.     for mailcap in listmailcapfiles():
  12.         
  13.         try:
  14.             fp = open(mailcap, 'r')
  15.         except IOError:
  16.             continue
  17.  
  18.         morecaps = readmailcapfile(fp)
  19.         fp.close()
  20.         for key, value in morecaps.iteritems():
  21.             if key not in caps:
  22.                 caps[key] = value
  23.                 continue
  24.             caps[key] = caps[key] + value
  25.         
  26.     
  27.     return caps
  28.  
  29.  
  30. def listmailcapfiles():
  31.     if 'MAILCAPS' in os.environ:
  32.         str = os.environ['MAILCAPS']
  33.         mailcaps = str.split(':')
  34.     elif 'HOME' in os.environ:
  35.         home = os.environ['HOME']
  36.     else:
  37.         home = '.'
  38.     mailcaps = [
  39.         home + '/.mailcap',
  40.         '/etc/mailcap',
  41.         '/usr/etc/mailcap',
  42.         '/usr/local/etc/mailcap']
  43.     return mailcaps
  44.  
  45.  
  46. def readmailcapfile(fp):
  47.     caps = { }
  48.     while None:
  49.         line = fp.readline()
  50.         if not line:
  51.             break
  52.         
  53.         if line[0] == '#' or line.strip() == '':
  54.             continue
  55.         
  56.         nextline = line
  57.         while nextline[-2:] == '\\\n':
  58.             nextline = fp.readline()
  59.             if not nextline:
  60.                 nextline = '\n'
  61.             
  62.             line = line[:-2] + nextline
  63.         (key, fields) = parseline(line)
  64.         if not key and fields:
  65.             continue
  66.         
  67.         types = key.split('/')
  68.         for j in range(len(types)):
  69.             types[j] = types[j].strip()
  70.         
  71.         key = '/'.join(types).lower()
  72.         if key in caps:
  73.             caps[key].append(fields)
  74.             continue
  75.         caps[key] = [
  76.             fields]
  77.         continue
  78.         return caps
  79.  
  80.  
  81. def parseline(line):
  82.     fields = []
  83.     i = 0
  84.     n = len(line)
  85.     while i < n:
  86.         (field, i) = parsefield(line, i, n)
  87.         fields.append(field)
  88.         i = i + 1
  89.     if len(fields) < 2:
  90.         return (None, None)
  91.     key = fields[0]
  92.     view = fields[1]
  93.     rest = fields[2:]
  94.     fields = {
  95.         'view': view }
  96.     for field in rest:
  97.         i = field.find('=')
  98.         if i < 0:
  99.             fkey = field
  100.             fvalue = ''
  101.         else:
  102.             fkey = field[:i].strip()
  103.             fvalue = field[i + 1:].strip()
  104.         if fkey in fields:
  105.             continue
  106.         fields[fkey] = fvalue
  107.     
  108.     return (key, fields)
  109.  
  110.  
  111. def parsefield(line, i, n):
  112.     start = i
  113.     while i < n:
  114.         c = line[i]
  115.         if c == ';':
  116.             break
  117.             continue
  118.         if c == '\\':
  119.             i = i + 2
  120.             continue
  121.         i = i + 1
  122.     return (line[start:i].strip(), i)
  123.  
  124.  
  125. def findmatch(caps, MIMEtype, key = 'view', filename = '/dev/null', plist = []):
  126.     entries = lookup(caps, MIMEtype, key)
  127.     for e in entries:
  128.         if 'test' in e:
  129.             test = subst(e['test'], filename, plist)
  130.             if test and os.system(test) != 0:
  131.                 continue
  132.             
  133.         
  134.         command = subst(e[key], MIMEtype, filename, plist)
  135.         return (command, e)
  136.     
  137.     return (None, None)
  138.  
  139.  
  140. def lookup(caps, MIMEtype, key = None):
  141.     entries = []
  142.     if MIMEtype in caps:
  143.         entries = entries + caps[MIMEtype]
  144.     
  145.     MIMEtypes = MIMEtype.split('/')
  146.     MIMEtype = MIMEtypes[0] + '/*'
  147.     if MIMEtype in caps:
  148.         entries = entries + caps[MIMEtype]
  149.     
  150.     if key is not None:
  151.         entries = filter((lambda e, key = key: key in e), entries)
  152.     
  153.     return entries
  154.  
  155.  
  156. def subst(field, MIMEtype, filename, plist = []):
  157.     res = ''
  158.     i = 0
  159.     n = len(field)
  160.     while i < n:
  161.         c = field[i]
  162.         i = i + 1
  163.         if c != '%':
  164.             if c == '\\':
  165.                 c = field[i:i + 1]
  166.                 i = i + 1
  167.             
  168.             res = res + c
  169.             continue
  170.         c = field[i]
  171.         i = i + 1
  172.         if c == '%':
  173.             res = res + c
  174.             continue
  175.         if c == 's':
  176.             res = res + filename
  177.             continue
  178.         if c == 't':
  179.             res = res + MIMEtype
  180.             continue
  181.         if c == '{':
  182.             start = i
  183.             while i < n and field[i] != '}':
  184.                 i = i + 1
  185.             name = field[start:i]
  186.             i = i + 1
  187.             res = res + findparam(name, plist)
  188.             continue
  189.         res = res + '%' + c
  190.     return res
  191.  
  192.  
  193. def findparam(name, plist):
  194.     name = name.lower() + '='
  195.     n = len(name)
  196.     for p in plist:
  197.         if p[:n].lower() == name:
  198.             return p[n:]
  199.     
  200.     return ''
  201.  
  202.  
  203. def test():
  204.     import sys
  205.     caps = getcaps()
  206.     if not sys.argv[1:]:
  207.         show(caps)
  208.         return None
  209.     for i in range(1, len(sys.argv), 2):
  210.         args = sys.argv[i:i + 2]
  211.         if len(args) < 2:
  212.             print 'usage: mailcap [MIMEtype file] ...'
  213.             return None
  214.         MIMEtype = args[0]
  215.         file = args[1]
  216.         (command, e) = findmatch(caps, MIMEtype, 'view', file)
  217.         if not command:
  218.             print 'No viewer found for', type
  219.             continue
  220.         sys.argv[1:]
  221.         print 'Executing:', command
  222.         sts = os.system(command)
  223.         if sts:
  224.             print 'Exit status:', sts
  225.             continue
  226.     
  227.  
  228.  
  229. def show(caps):
  230.     print 'Mailcap files:'
  231.     for fn in listmailcapfiles():
  232.         print '\t' + fn
  233.     
  234.     print 
  235.     if not caps:
  236.         caps = getcaps()
  237.     
  238.     print 'Mailcap entries:'
  239.     print 
  240.     ckeys = caps.keys()
  241.     ckeys.sort()
  242.     for type in ckeys:
  243.         print type
  244.         entries = caps[type]
  245.         for e in entries:
  246.             keys = e.keys()
  247.             keys.sort()
  248.             for k in keys:
  249.                 print '  %-15s' % k, e[k]
  250.             
  251.             print 
  252.         
  253.     
  254.  
  255. if __name__ == '__main__':
  256.     test()
  257.  
  258.