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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cmd
  5. from itertools import chain
  6. import os
  7. import shlex
  8. import stat
  9. import string
  10. import sys
  11. from bzrlib import osutils
  12. from bzrlib.branch import Branch
  13. from bzrlib.config import config_dir, ensure_config_dir_exists
  14. from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
  15. from bzrlib.errors import BzrError
  16. from bzrlib.workingtree import WorkingTree
  17. import bzrlib.plugin as bzrlib
  18. SHELL_BLACKLIST = set([
  19.     'rm',
  20.     'ls'])
  21. COMPLETION_BLACKLIST = set([
  22.     'shell'])
  23.  
  24. class BlackListedCommand(BzrError):
  25.     
  26.     def __init__(self, command):
  27.         BzrError.__init__(self, 'The command %s is blacklisted for shell use' % command)
  28.  
  29.  
  30.  
  31. class CompletionContext(object):
  32.     
  33.     def __init__(self, text, command = None, prev_opt = None, arg_pos = None):
  34.         self.text = text
  35.         self.command = command
  36.         self.prev_opt = prev_opt
  37.         self.arg_pos = None
  38.  
  39.     
  40.     def get_completions(self):
  41.         
  42.         try:
  43.             return self.get_completions_or_raise()
  44.         except Exception:
  45.             e = None
  46.             print e, type(e)
  47.             return []
  48.  
  49.  
  50.     
  51.     def get_option_completions(self):
  52.         
  53.         try:
  54.             command_obj = get_cmd_object(self.command)
  55.         except BzrError:
  56.             return []
  57.  
  58.         opts = [ o + ' ' for o in iter_opt_completions(command_obj) ]
  59.         return list(filter_completions(opts, self.text))
  60.  
  61.     
  62.     def get_completions_or_raise(self):
  63.         if self.command is None:
  64.             if '/' in self.text:
  65.                 iter = iter_executables(self.text)
  66.             else:
  67.                 iter = (lambda .0: for c in .0:
  68. if c not in COMPLETION_BLACKLIST:
  69. c + ' 'continue)(iter_command_names())
  70.             return list(filter_completions(iter, self.text))
  71.         if self.prev_opt is None:
  72.             completions = self.get_option_completions()
  73.             if self.command == 'cd':
  74.                 iter = iter_dir_completions(self.text)
  75.                 completions.extend(list(filter_completions(iter, self.text)))
  76.             else:
  77.                 iter = iter_file_completions(self.text)
  78.                 completions.extend(filter_completions(iter, self.text))
  79.             return completions
  80.  
  81.  
  82.  
  83. class PromptCmd(cmd.Cmd):
  84.     
  85.     def __init__(self):
  86.         cmd.Cmd.__init__(self)
  87.         self.prompt = 'bzr> '
  88.         
  89.         try:
  90.             self.tree = WorkingTree.open_containing('.')[0]
  91.         except:
  92.             self.tree = None
  93.  
  94.         self.set_title()
  95.         self.set_prompt()
  96.         self.identchars += '-'
  97.         ensure_config_dir_exists()
  98.         self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
  99.         readline.set_completer_delims(string.whitespace)
  100.         if os.access(self.history_file, os.R_OK) and os.path.isfile(self.history_file):
  101.             readline.read_history_file(self.history_file)
  102.         
  103.         self.cwd = os.getcwd()
  104.  
  105.     
  106.     def write_history(self):
  107.         readline.write_history_file(self.history_file)
  108.  
  109.     
  110.     def do_quit(self, args):
  111.         self.write_history()
  112.         raise StopIteration
  113.  
  114.     
  115.     def do_exit(self, args):
  116.         self.do_quit(args)
  117.  
  118.     
  119.     def do_EOF(self, args):
  120.         print 
  121.         self.do_quit(args)
  122.  
  123.     
  124.     def postcmd(self, line, bar):
  125.         self.set_title()
  126.         self.set_prompt()
  127.  
  128.     
  129.     def set_prompt(self):
  130.         if self.tree is not None:
  131.             
  132.             try:
  133.                 prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), self.tree.relpath('.'))
  134.                 prompt = ' %s:%d/%s' % prompt_data
  135.             prompt = ''
  136.  
  137.         else:
  138.             prompt = ''
  139.         self.prompt = 'bzr%s> ' % prompt
  140.  
  141.     
  142.     def set_title(self, command = None):
  143.         
  144.         try:
  145.             b = Branch.open_containing('.')[0]
  146.             version = '%s:%d' % (b.nick, b.revno())
  147.         except:
  148.             version = '[no version]'
  149.  
  150.         if command is None:
  151.             command = ''
  152.         
  153.         sys.stdout.write(terminal.term_title('bzr %s %s' % (command, version)))
  154.  
  155.     
  156.     def do_cd(self, line):
  157.         if line == '':
  158.             line = '~'
  159.         
  160.         line = os.path.expanduser(line)
  161.         if os.path.isabs(line):
  162.             newcwd = line
  163.         else:
  164.             newcwd = self.cwd + '/' + line
  165.         newcwd = os.path.normpath(newcwd)
  166.         
  167.         try:
  168.             os.chdir(newcwd)
  169.             self.cwd = newcwd
  170.         except Exception:
  171.             e = None
  172.             print e
  173.  
  174.         
  175.         try:
  176.             self.tree = WorkingTree.open_containing('.')[0]
  177.         except:
  178.             self.tree = None
  179.  
  180.  
  181.     
  182.     def do_help(self, line):
  183.         self.default('help ' + line)
  184.  
  185.     
  186.     def default(self, line):
  187.         args = shlex.split(line)
  188.         alias_args = get_alias(args[0])
  189.         if alias_args is not None:
  190.             args[0] = alias_args.pop(0)
  191.         
  192.         commandname = args.pop(0)
  193.         for char in ('|', '<', '>'):
  194.             commandname = commandname.split(char)[0]
  195.         
  196.         if commandname[-1] in ('|', '<', '>'):
  197.             commandname = commandname[:-1]
  198.         
  199.         
  200.         try:
  201.             if commandname in SHELL_BLACKLIST:
  202.                 raise BlackListedCommand(commandname)
  203.             commandname in SHELL_BLACKLIST
  204.             cmd_obj = get_cmd_object(commandname)
  205.         except (BlackListedCommand, BzrError):
  206.             return os.system(line)
  207.  
  208.         
  209.         try:
  210.             if too_complicated(line):
  211.                 return os.system('bzr ' + line)
  212.             if not cmd_obj.run_argv_aliases(args, alias_args):
  213.                 pass
  214.             return 0
  215.         except BzrError:
  216.             e = None
  217.             print e
  218.         except KeyboardInterrupt:
  219.             e = None
  220.             print 'Interrupted'
  221.         except Exception:
  222.             e = None
  223.             print 'Unhandled error:\n%s' % e
  224.  
  225.  
  226.     
  227.     def completenames(self, text, line, begidx, endidx):
  228.         return CompletionContext(text).get_completions()
  229.  
  230.     
  231.     def completedefault(self, text, line, begidx, endidx):
  232.         (cmd, args, foo) = self.parseline(line)
  233.         if cmd == 'bzr':
  234.             cmd = None
  235.         
  236.         return CompletionContext(text, command = cmd).get_completions()
  237.  
  238.  
  239.  
  240. def run_shell():
  241.     
  242.     try:
  243.         prompt = PromptCmd()
  244.         
  245.         try:
  246.             prompt.cmdloop()
  247.         finally:
  248.             prompt.write_history()
  249.  
  250.     except StopIteration:
  251.         pass
  252.  
  253.  
  254.  
  255. def iter_opt_completions(command_obj):
  256.     for option_name, option in command_obj.options().items():
  257.         yield '--' + option_name
  258.         short_name = option.short_name()
  259.         if short_name:
  260.             yield '-' + short_name
  261.             continue
  262.     
  263.  
  264.  
  265. def iter_file_completions(arg, only_dirs = False):
  266.     cwd = os.getcwd()
  267.     if cwd != '/':
  268.         extras = [
  269.             '.',
  270.             '..']
  271.     else:
  272.         extras = []
  273.     (dir, file) = os.path.split(arg)
  274.     if dir != '':
  275.         listingdir = os.path.expanduser(dir)
  276.     else:
  277.         listingdir = cwd
  278.     for file in chain(os.listdir(listingdir), extras):
  279.         if dir != '':
  280.             userfile = dir + '/' + file
  281.         else:
  282.             userfile = file
  283.         if userfile.startswith(arg):
  284.             if os.path.isdir(listingdir + '/' + file):
  285.                 userfile += '/'
  286.                 yield userfile
  287.             elif not only_dirs:
  288.                 yield userfile + ' '
  289.             
  290.         os.path.isdir(listingdir + '/' + file)
  291.     
  292.  
  293.  
  294. def iter_dir_completions(arg):
  295.     return iter_file_completions(arg, True)
  296.  
  297.  
  298. def iter_command_names(hidden = False):
  299.     for real_cmd_name, cmd_class in get_all_cmds():
  300.         if not hidden and cmd_class.hidden:
  301.             continue
  302.         
  303.         for name in [
  304.             real_cmd_name] + cmd_class.aliases:
  305.             if name == real_cmd_name or not real_cmd_name.startswith(name):
  306.                 yield name
  307.                 continue
  308.         
  309.     
  310.  
  311.  
  312. def iter_executables(path):
  313.     (dirname, partial) = os.path.split(path)
  314.     for filename in os.listdir(dirname):
  315.         if not filename.startswith(partial):
  316.             continue
  317.         
  318.         fullpath = os.path.join(dirname, filename)
  319.         mode = os.lstat(fullpath)[stat.ST_MODE]
  320.         if stat.S_ISREG(mode) and 73 & mode:
  321.             yield fullpath + ' '
  322.             continue
  323.     
  324.  
  325.  
  326. def filter_completions(iter, arg):
  327.     return (lambda .0: for c in .0:
  328. if c.startswith(arg):
  329. ccontinue)(iter)
  330.  
  331.  
  332. def iter_munged_completions(iter, arg, text):
  333.     for completion in iter:
  334.         completion = str(completion)
  335.         if completion.startswith(arg):
  336.             yield completion[len(arg) - len(text):] + ' '
  337.             continue
  338.     
  339.  
  340.  
  341. def too_complicated(line):
  342.     for char in '|<>*?':
  343.         if char in line:
  344.             return True
  345.     
  346.     return False
  347.  
  348.  
  349. def init_ipython(ip):
  350.     
  351.     def bzr_completer(self, ev):
  352.         tup = ev.line.split(None, 2)
  353.         if len(tup) > 2:
  354.             cmd = tup[1]
  355.         else:
  356.             cmd = None
  357.         return CompletionContext(ev.symbol, command = cmd).get_completions()
  358.  
  359.     bzrlib.plugin.load_plugins()
  360.     ip.set_hook('complete_command', bzr_completer, str_key = 'bzr')
  361.  
  362.