|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2014, Digital Reasoning |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import os |
| 19 | +from cmd import Cmd |
| 20 | + |
| 21 | +import click |
| 22 | + |
| 23 | +from stackdio.client.version import __version__ |
| 24 | + |
| 25 | + |
| 26 | +try: |
| 27 | + import readline |
| 28 | +except ImportError: |
| 29 | + readline = None |
| 30 | + |
| 31 | + |
| 32 | +HIST_FILE = os.path.join(os.path.expanduser('~'), '.stackdio-cli', 'history') |
| 33 | + |
| 34 | + |
| 35 | +def get_invoke(ctx, command): |
| 36 | + def invoke(self, arg): |
| 37 | + return ctx.invoke(command) |
| 38 | + return invoke |
| 39 | + |
| 40 | + |
| 41 | +def get_help(command): |
| 42 | + def help(self): |
| 43 | + click.echo(command.help) |
| 44 | + return help |
| 45 | + |
| 46 | + |
| 47 | +def get_shell(ctx): |
| 48 | + |
| 49 | + # Make it a new-style class so we can use super |
| 50 | + class StackdioShell(Cmd, object): |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + super(StackdioShell, self).__init__() |
| 54 | + self.old_completer = None |
| 55 | + |
| 56 | + def preloop(self): |
| 57 | + # read our history |
| 58 | + if readline: |
| 59 | + try: |
| 60 | + readline.read_history_file(HIST_FILE) |
| 61 | + except IOError: |
| 62 | + pass |
| 63 | + |
| 64 | + def postloop(self): |
| 65 | + # Write our history |
| 66 | + if readline: |
| 67 | + readline.write_history_file(HIST_FILE) |
| 68 | + |
| 69 | + prompt = 'stackdio > ' |
| 70 | + intro = 'stackdio shell v{0}'.format(__version__) |
| 71 | + |
| 72 | + # We need to override this to fix readline |
| 73 | + def cmdloop(self, intro=None): |
| 74 | + self.preloop() |
| 75 | + if self.use_rawinput and self.completekey and readline: |
| 76 | + self.old_completer = readline.get_completer() |
| 77 | + readline.set_completer(self.complete) |
| 78 | + if 'libedit' in readline.__doc__: |
| 79 | + # For mac |
| 80 | + readline.parse_and_bind('bind ^I rl_complete') |
| 81 | + else: |
| 82 | + # for other platforms |
| 83 | + readline.parse_and_bind(self.completekey + ': complete') |
| 84 | + try: |
| 85 | + if intro is not None: |
| 86 | + self.intro = intro |
| 87 | + if self.intro: |
| 88 | + self.stdout.write(str(self.intro)+"\n") |
| 89 | + stop = None |
| 90 | + while not stop: |
| 91 | + if self.cmdqueue: |
| 92 | + line = self.cmdqueue.pop(0) |
| 93 | + else: |
| 94 | + if self.use_rawinput: |
| 95 | + try: |
| 96 | + line = raw_input(self.prompt) |
| 97 | + except EOFError: |
| 98 | + # We just want to quit here |
| 99 | + self.stdout.write('\n') |
| 100 | + break |
| 101 | + else: |
| 102 | + self.stdout.write(self.prompt) |
| 103 | + self.stdout.flush() |
| 104 | + line = self.stdin.readline() |
| 105 | + if not len(line): |
| 106 | + line = 'EOF' |
| 107 | + else: |
| 108 | + line = line.rstrip('\r\n') |
| 109 | + line = self.precmd(line) |
| 110 | + stop = self.onecmd(line) |
| 111 | + stop = self.postcmd(stop, line) |
| 112 | + |
| 113 | + finally: |
| 114 | + self.postloop() |
| 115 | + if self.use_rawinput and self.completekey and readline: |
| 116 | + readline.set_completer(self.old_completer) |
| 117 | + |
| 118 | + def emptyline(self): |
| 119 | + pass |
| 120 | + |
| 121 | + def do_quit(self, arg): |
| 122 | + return True |
| 123 | + |
| 124 | + def do_exit(self, arg): |
| 125 | + return True |
| 126 | + |
| 127 | + for name, command in ctx.command.commands.items(): |
| 128 | + setattr(StackdioShell, 'do_%s' % name.replace('-', '_'), get_invoke(ctx, command)) |
| 129 | + |
| 130 | + if command.help is not None: |
| 131 | + setattr(StackdioShell, 'help_%s' % name.replace('-', '_'), get_help(command)) |
| 132 | + |
| 133 | + return StackdioShell() |
0 commit comments