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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import sys
  9. import os
  10. import shlex
  11. import glob
  12. import re
  13. import cPickle
  14.  
  15. def prints(*args, **kwargs):
  16.     file = kwargs.get('file', sys.stdout)
  17.     sep = kwargs.get('sep', ' ')
  18.     end = kwargs.get('end', '\n')
  19.     enc = 'utf-8'
  20.     safe_encode = kwargs.get('safe_encode', False)
  21.     for i, arg in enumerate(args):
  22.         if isinstance(arg, unicode):
  23.             
  24.             try:
  25.                 arg = arg.encode(enc)
  26.             except UnicodeEncodeError:
  27.                 if not safe_encode:
  28.                     raise 
  29.                 safe_encode
  30.                 arg = repr(arg)
  31.             except:
  32.                 None<EXCEPTION MATCH>UnicodeEncodeError
  33.             
  34.  
  35.         None<EXCEPTION MATCH>UnicodeEncodeError
  36.         if not isinstance(arg, str):
  37.             
  38.             try:
  39.                 arg = str(arg)
  40.             except ValueError:
  41.                 arg = unicode(arg)
  42.  
  43.             if isinstance(arg, unicode):
  44.                 
  45.                 try:
  46.                     arg = arg.encode(enc)
  47.                 except UnicodeEncodeError:
  48.                     if not safe_encode:
  49.                         raise 
  50.                     safe_encode
  51.                     arg = repr(arg)
  52.                 except:
  53.                     None<EXCEPTION MATCH>UnicodeEncodeError
  54.                 
  55.  
  56.             None<EXCEPTION MATCH>UnicodeEncodeError
  57.         
  58.         file.write(arg)
  59.         if i != len(args) - 1:
  60.             file.write(sep)
  61.             continue
  62.     
  63.     file.write(end)
  64.  
  65.  
  66. def split(src):
  67.     
  68.     try:
  69.         return shlex.split(src)
  70.     except ValueError:
  71.         
  72.         try:
  73.             return shlex.split(src + '"')
  74.         except ValueError:
  75.             return shlex.split(src + "'")
  76.         
  77.  
  78.         None<EXCEPTION MATCH>ValueError
  79.  
  80.  
  81.  
  82. def files_and_dirs(prefix, allowed_exts = []):
  83.     prefix = os.path.expanduser(prefix)
  84.     for i in glob.iglob(prefix + '*'):
  85.         (_, ext) = os.path.splitext(i)
  86.         ext = ext.lower().replace('.', '')
  87.         if os.path.isdir(i):
  88.             yield i + os.sep
  89.             continue
  90.         if allowed_exts is None or ext in allowed_exts:
  91.             yield i + ' '
  92.             continue
  93.     
  94.  
  95.  
  96. def get_opts_from_parser(parser, prefix):
  97.     
  98.     def do_opt(opt):
  99.         for x in opt._long_opts:
  100.             if x.startswith(prefix):
  101.                 yield x
  102.                 continue
  103.         
  104.         for x in opt._short_opts:
  105.             if x.startswith(prefix):
  106.                 yield x
  107.                 continue
  108.         
  109.  
  110.     for o in parser.option_list:
  111.         for x in do_opt(o):
  112.             yield x + ' '
  113.             (None,)
  114.         
  115.     
  116.     for g in parser.option_groups:
  117.         for o in g.option_list:
  118.             for x in do_opt(o):
  119.                 yield x + ' '
  120.             
  121.         
  122.     
  123.  
  124.  
  125. def send(ans):
  126.     pat = re.compile('([^0-9a-zA-Z_./-])')
  127.     for x in sorted(set(ans)):
  128.         x = pat.sub((lambda m: '\\' + m.group(1)), x)
  129.         if x.endswith('\\ '):
  130.             x = x[:-2] + ' '
  131.         
  132.         prints(x)
  133.     
  134.  
  135.  
  136. class EbookConvert(object):
  137.     
  138.     def __init__(self, comp_line, pos):
  139.         words = split(comp_line[:pos])
  140.         char_before = comp_line[pos - 1]
  141.         prefix = None if words[-1].endswith(char_before) else ''
  142.         wc = len(words)
  143.         if not prefix:
  144.             wc += 1
  145.         
  146.         self.words = words
  147.         self.prefix = prefix
  148.         self.previous = None[words if prefix else -1]
  149.         self.cache = cPickle.load(open(os.path.join(sys.resources_location, 'ebook-convert-complete.pickle'), 'rb'))
  150.         self.complete(wc)
  151.  
  152.     
  153.     def complete(self, wc):
  154.         if wc == 2:
  155.             self.complete_input()
  156.         elif wc == 3:
  157.             self.complete_output()
  158.         else:
  159.             q = list(self.words[1:3])
  160.             q = [ _[1][os.path.splitext(x) if x.startswith('.') else 1].partition('.')[-1].lower() for x in q ]
  161.             if not q[1]:
  162.                 q[1] = 'oeb'
  163.             
  164.             q = tuple(q)
  165.             if self.previous.startswith('-'):
  166.                 ans += list(files_and_dirs(self.prefix, None))
  167.             
  168.             send(ans)
  169.  
  170.     
  171.     def complete_input(self):
  172.         ans = list(files_and_dirs(self.prefix, self.cache['input_fmts']))
  173.         [] += _[1]
  174.         send(ans)
  175.  
  176.     
  177.     def complete_output(self):
  178.         fmts = self.cache['output']
  179.         ans = list(files_and_dirs(self.prefix, fmts))
  180.         [] += _[1]
  181.         send(ans)
  182.  
  183.  
  184.  
  185. def main(args = sys.argv):
  186.     comp_line = os.environ['COMP_LINE']
  187.     pos = int(os.environ['COMP_POINT'])
  188.     module = split(comp_line)[0].split(os.sep)[-1]
  189.     if module == 'ebook-convert':
  190.         EbookConvert(comp_line, pos)
  191.     
  192.     return 0
  193.  
  194. if __name__ == '__main__':
  195.     raise sys.exit(main())
  196. __name__ == '__main__'
  197.