home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from __future__ import with_statement
- __license__ = 'GPL v3'
- __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
- __docformat__ = 'restructuredtext en'
- import sys
- import os
- import shlex
- import glob
- import re
- import cPickle
-
- def prints(*args, **kwargs):
- file = kwargs.get('file', sys.stdout)
- sep = kwargs.get('sep', ' ')
- end = kwargs.get('end', '\n')
- enc = 'utf-8'
- safe_encode = kwargs.get('safe_encode', False)
- for i, arg in enumerate(args):
- if isinstance(arg, unicode):
-
- try:
- arg = arg.encode(enc)
- except UnicodeEncodeError:
- if not safe_encode:
- raise
- safe_encode
- arg = repr(arg)
- except:
- None<EXCEPTION MATCH>UnicodeEncodeError
-
-
- None<EXCEPTION MATCH>UnicodeEncodeError
- if not isinstance(arg, str):
-
- try:
- arg = str(arg)
- except ValueError:
- arg = unicode(arg)
-
- if isinstance(arg, unicode):
-
- try:
- arg = arg.encode(enc)
- except UnicodeEncodeError:
- if not safe_encode:
- raise
- safe_encode
- arg = repr(arg)
- except:
- None<EXCEPTION MATCH>UnicodeEncodeError
-
-
- None<EXCEPTION MATCH>UnicodeEncodeError
-
- file.write(arg)
- if i != len(args) - 1:
- file.write(sep)
- continue
-
- file.write(end)
-
-
- def split(src):
-
- try:
- return shlex.split(src)
- except ValueError:
-
- try:
- return shlex.split(src + '"')
- except ValueError:
- return shlex.split(src + "'")
-
-
- None<EXCEPTION MATCH>ValueError
-
-
-
- def files_and_dirs(prefix, allowed_exts = []):
- prefix = os.path.expanduser(prefix)
- for i in glob.iglob(prefix + '*'):
- (_, ext) = os.path.splitext(i)
- ext = ext.lower().replace('.', '')
- if os.path.isdir(i):
- yield i + os.sep
- continue
- if allowed_exts is None or ext in allowed_exts:
- yield i + ' '
- continue
-
-
-
- def get_opts_from_parser(parser, prefix):
-
- def do_opt(opt):
- for x in opt._long_opts:
- if x.startswith(prefix):
- yield x
- continue
-
- for x in opt._short_opts:
- if x.startswith(prefix):
- yield x
- continue
-
-
- for o in parser.option_list:
- for x in do_opt(o):
- yield x + ' '
- (None,)
-
-
- for g in parser.option_groups:
- for o in g.option_list:
- for x in do_opt(o):
- yield x + ' '
-
-
-
-
-
- def send(ans):
- pat = re.compile('([^0-9a-zA-Z_./-])')
- for x in sorted(set(ans)):
- x = pat.sub((lambda m: '\\' + m.group(1)), x)
- if x.endswith('\\ '):
- x = x[:-2] + ' '
-
- prints(x)
-
-
-
- class EbookConvert(object):
-
- def __init__(self, comp_line, pos):
- words = split(comp_line[:pos])
- char_before = comp_line[pos - 1]
- prefix = None if words[-1].endswith(char_before) else ''
- wc = len(words)
- if not prefix:
- wc += 1
-
- self.words = words
- self.prefix = prefix
- self.previous = None[words if prefix else -1]
- self.cache = cPickle.load(open(os.path.join(sys.resources_location, 'ebook-convert-complete.pickle'), 'rb'))
- self.complete(wc)
-
-
- def complete(self, wc):
- if wc == 2:
- self.complete_input()
- elif wc == 3:
- self.complete_output()
- else:
- q = list(self.words[1:3])
- q = [ _[1][os.path.splitext(x) if x.startswith('.') else 1].partition('.')[-1].lower() for x in q ]
- if not q[1]:
- q[1] = 'oeb'
-
- q = tuple(q)
- if self.previous.startswith('-'):
- ans += list(files_and_dirs(self.prefix, None))
-
- send(ans)
-
-
- def complete_input(self):
- ans = list(files_and_dirs(self.prefix, self.cache['input_fmts']))
- [] += _[1]
- send(ans)
-
-
- def complete_output(self):
- fmts = self.cache['output']
- ans = list(files_and_dirs(self.prefix, fmts))
- [] += _[1]
- send(ans)
-
-
-
- def main(args = sys.argv):
- comp_line = os.environ['COMP_LINE']
- pos = int(os.environ['COMP_POINT'])
- module = split(comp_line)[0].split(os.sep)[-1]
- if module == 'ebook-convert':
- EbookConvert(comp_line, pos)
-
- return 0
-
- if __name__ == '__main__':
- raise sys.exit(main())
- __name__ == '__main__'
-