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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  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.         if line[0] == '#' or line.strip() == '':
  53.             continue
  54.         nextline = line
  55.         while nextline[-2:] == '\\\n':
  56.             nextline = fp.readline()
  57.             if not nextline:
  58.                 nextline = '\n'
  59.             line = line[:-2] + nextline
  60.         (key, fields) = parseline(line)
  61.         if not key and fields:
  62.             continue
  63.         types = key.split('/')
  64.         for j in range(len(types)):
  65.             types[j] = types[j].strip()
  66.         
  67.         key = '/'.join(types).lower()
  68.         if key in caps:
  69.             caps[key].append(fields)
  70.             continue
  71.         caps[key] = [
  72.             fields]
  73.         continue
  74.         return caps
  75.  
  76.  
  77. def parseline(line):
  78.     fields = []
  79.     i = 0
  80.     n = len(line)
  81.     while i < n:
  82.         (field, i) = parsefield(line, i, n)
  83.         fields.append(field)
  84.         i = i + 1
  85.     if len(fields) < 2:
  86.         return (None, None)
  87.     key = None[0]
  88.     view = fields[1]
  89.     rest = fields[2:]
  90.     fields = {
  91.         'view': view }
  92.     for field in rest:
  93.         i = field.find('=')
  94.         if i < 0:
  95.             fkey = field
  96.             fvalue = ''
  97.         else:
  98.             fkey = field[:i].strip()
  99.             fvalue = field[i + 1:].strip()
  100.         if fkey in fields:
  101.             continue
  102.         fields[fkey] = fvalue
  103.     
  104.     return (key, fields)
  105.  
  106.  
  107. def parsefield(line, i, n):
  108.     start = i
  109.     while i < n:
  110.         c = line[i]
  111.         if c == ';':
  112.             break
  113.             continue
  114.         if c == '\\':
  115.             i = i + 2
  116.             continue
  117.         i = i + 1
  118.     return (line[start:i].strip(), i)
  119.  
  120.  
  121. def findmatch(caps, MIMEtype, key = 'view', filename = '/dev/null', plist = []):
  122.     entries = lookup(caps, MIMEtype, key)
  123.     for e in entries:
  124.         if 'test' in e:
  125.             test = subst(e['test'], filename, plist)
  126.             if test and os.system(test) != 0:
  127.                 continue
  128.             
  129.         command = subst(e[key], MIMEtype, filename, plist)
  130.         return (command, e)
  131.     
  132.     return (None, None)
  133.  
  134.  
  135. def lookup(caps, MIMEtype, key = None):
  136.     entries = []
  137.     if MIMEtype in caps:
  138.         entries = entries + caps[MIMEtype]
  139.     MIMEtypes = MIMEtype.split('/')
  140.     MIMEtype = MIMEtypes[0] + '/*'
  141.     if MIMEtype in caps:
  142.         entries = entries + caps[MIMEtype]
  143.     if key is not None:
  144.         entries = filter((lambda e, key = key: key in e), entries)
  145.     return entries
  146.  
  147.  
  148. def subst(field, MIMEtype, filename, plist = []):
  149.     res = ''
  150.     i = 0
  151.     n = len(field)
  152.     while i < n:
  153.         c = field[i]
  154.         i = i + 1
  155.         if c != '%':
  156.             if c == '\\':
  157.                 c = field[i:i + 1]
  158.                 i = i + 1
  159.             res = res + c
  160.             continue
  161.         c = field[i]
  162.         i = i + 1
  163.         if c == '%':
  164.             res = res + c
  165.             continue
  166.         if c == 's':
  167.             res = res + filename
  168.             continue
  169.         if c == 't':
  170.             res = res + MIMEtype
  171.             continue
  172.         if c == '{':
  173.             start = i
  174.             while i < n and field[i] != '}':
  175.                 i = i + 1
  176.             name = field[start:i]
  177.             i = i + 1
  178.             res = res + findparam(name, plist)
  179.             continue
  180.         res = res + '%' + c
  181.     return res
  182.  
  183.  
  184. def findparam(name, plist):
  185.     name = name.lower() + '='
  186.     n = len(name)
  187.     for p in plist:
  188.         if p[:n].lower() == name:
  189.             return p[n:]
  190.     
  191.     return ''
  192.  
  193.  
  194. def test():
  195.     import sys
  196.     caps = getcaps()
  197.     if not sys.argv[1:]:
  198.         show(caps)
  199.         return None
  200.     print 'Executing:', command
  201.     sts = os.system(command)
  202.     if sts:
  203.         print 'Exit status:', sts
  204.         continue
  205.  
  206.  
  207. def show(caps):
  208.     print 'Mailcap files:'
  209.     for fn in listmailcapfiles():
  210.         print '\t' + fn
  211.     
  212.     print 
  213.     if not caps:
  214.         caps = getcaps()
  215.     print 'Mailcap entries:'
  216.     print 
  217.     ckeys = caps.keys()
  218.     ckeys.sort()
  219.     for type in ckeys:
  220.         print type
  221.         entries = caps[type]
  222.         for e in entries:
  223.             keys = e.keys()
  224.             keys.sort()
  225.             for k in keys:
  226.                 print '  %-15s' % k, e[k]
  227.             
  228.             print 
  229.         
  230.     
  231.  
  232. if __name__ == '__main__':
  233.     test()
  234.