~hudson-openstack/burrow/cactus

« back to all changes in this revision

Viewing changes to burrow/shell.py

  • Committer: Tarmac
  • Author(s): Eric Day
  • Date: 2011-04-20 07:32:27 UTC
  • mfrom: (7.1.3 dev)
  • Revision ID: tarmac-20110420073227-jv4kmex73efsf7b1
Pylint cleanup, reorganized code, added interactive shell.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
 
27
27
class Shell(object):
 
28
    '''Shell session class.'''
28
29
 
29
30
    sections = [
30
31
        dict(name='Global',
96
97
            help=_('What message information to return. Options are: %s') %
97
98
            ', '.join(choices))
98
99
        self.parser.add_option_group(filters)
99
 
 
100
 
    def run(self):
101
 
        (self.options, args) = self.parser.parse_args()
 
100
        (self.options, self.args) = self.parser.parse_args()
102
101
        if self.options.commands:
103
102
            self.print_help()
104
 
        if len(args) == 0:
105
 
            self.print_help(_('No command given'))
106
103
        if self.options.files is None:
107
104
            files = []
108
105
        else:
109
106
            files = self.options.files.split(', ')
110
107
        self.client = burrow.Client(url=self.options.url, config_files=files)
111
 
        for section in self.sections:
112
 
            if args[0] in section['commands']:
113
 
                self.run_command(section, args[0], args[1:])
114
 
        self.print_help(_('Command not found'))
115
 
 
116
 
    def run_command(self, section, command, args):
 
108
 
 
109
    def run(self):
 
110
        '''Run the command given in arguments or enter an interactive shell.'''
 
111
        if len(self.args) == 0:
 
112
            for command in self._get_command():
 
113
                self.run_command(command[0], command[1:])
 
114
        else:
 
115
            self.run_command(self.args[0], self.args[1:])
 
116
 
 
117
    def _get_command(self):
 
118
        '''Get a command from stdin, printing a prompt out if stdin
 
119
        is attached to a TTY.'''
 
120
        prompt = ''
 
121
        if os.isatty(sys.stdin.fileno()):
 
122
            prompt = 'burrow> '
 
123
        try:
 
124
            # Try importing readline to make raw_input functionality more
 
125
            # user friendly.
 
126
            import readline
 
127
        except ImportError:
 
128
            pass
 
129
        while True:
 
130
            try:
 
131
                command = raw_input(prompt)
 
132
            except EOFError:
 
133
                if os.isatty(sys.stdin.fileno()):
 
134
                    print
 
135
                break
 
136
            command = command.split()
 
137
            if len(command) == 0:
 
138
                continue
 
139
            if command[0] == 'exit' or command[0] == 'quit':
 
140
                break
 
141
            yield command
 
142
 
 
143
    def run_command(self, command, args):
 
144
        '''Try running a command with the given arguments.'''
 
145
        section = self._get_section(command)
 
146
        if section is None:
 
147
            print _('Command not found: %s') % command
 
148
            return
117
149
        if len(args) != len(section['args']):
118
 
            self.print_help(_('Wrong number of arguments'))
 
150
            print _('Wrong number of arguments')
 
151
            return
119
152
        if section.get('account', None):
120
153
            args.insert(0, self.options.account)
121
154
        if command in self.stdin_commands:
137
170
                filters['match_hidden'] = self.options.all
138
171
            args.append(filters)
139
172
        getattr(self.client.backend, command)(*args)
140
 
        sys.exit(0)
 
173
 
 
174
    def _get_section(self, command):
 
175
        '''Lookup command in the defined command sections.'''
 
176
        for section in self.sections:
 
177
            if command in section['commands']:
 
178
                return section
 
179
        return None
141
180
 
142
181
    def print_help(self, message=None):
 
182
        '''Print the parser generated help along with burrow command help.'''
143
183
        if message:
144
184
            print message
145
185
            print
148
188
        for section in self.sections:
149
189
            print '%s commands:' % section['name']
150
190
            for command in section['commands']:
151
 
                help = ''
 
191
                help_string = ''
152
192
                if section.get('filters', None):
153
 
                    help += ' [filters]'
 
193
                    help_string += ' [filters]'
154
194
                if command in self.attribute_commands:
155
 
                    help += ' [attributes]'
 
195
                    help_string += ' [attributes]'
156
196
                for arg in section['args']:
157
 
                    help += ' <%s>' % arg
158
 
                print '    %s%s' % (command, help)
 
197
                    help_string += ' <%s>' % arg
 
198
                print '    %s%s' % (command, help_string)
159
199
            print
160
200
        sys.exit(1)
161
201