Skip to content

Commit 36e7a78

Browse files
committed
cli: fix autocompletion binding bug, make all params lowercase
- fix weird autocompletion bug which won't let you enter the letter b [0] - make set params, api and secret key vars lowercase - use partition to get strings for do_set - while installing, check and only then add readline (requires gcc and can fail) - show monkey prompt by default, let users change if needed [0] http://superuser.com/questions/297527/cant-type-the-b-letter-in-python-shell-in-os-x Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
1 parent ac8ae30 commit 36e7a78

4 files changed

Lines changed: 53 additions & 31 deletions

File tree

tools/cli/cloudmonkey/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
# under the License.
1717

1818
try:
19-
from cloudmonkey import __version__
19+
from version import __version__
2020
except ImportError, e:
2121
print e

tools/cli/cloudmonkey/cloudmonkey.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525
import logging
2626
import os
2727
import pdb
28-
import readline
29-
import rlcompleter
3028
import shlex
3129
import sys
3230
import types
3331

3432
from clint.textui import colored
3533
from ConfigParser import ConfigParser, SafeConfigParser
3634

35+
from version import __version__
3736
from marvin.cloudstackConnection import cloudConnection
3837
from marvin.cloudstackException import cloudstackAPIException
3938
from marvin.cloudstackAPI import *
@@ -43,10 +42,17 @@
4342
import sys
4443
sys.exit()
4544

46-
# Use following rules for versioning:
47-
# <cli major version>.<cloudstack minor version>.<cloudstack major version>
48-
# Example: For CloudStack 4.1.x, CLI version should be 0.1.4
49-
__version__ = "0.0.4"
45+
# Fix autocompletion issue, can be put in .pythonstartup
46+
try:
47+
import readline
48+
except ImportError, e:
49+
print "Module readline not found, autocompletions will fail", e
50+
else:
51+
import rlcompleter
52+
if 'libedit' in readline.__doc__:
53+
readline.parse_and_bind("bind ^I rl_complete")
54+
else:
55+
readline.parse_and_bind("tab: complete")
5056

5157
log_fmt = '%(asctime)s - %(filename)s:%(lineno)s - [%(levelname)s] %(message)s'
5258
logger = logging.getLogger(__name__)
@@ -65,8 +71,8 @@ class CloudStackShell(cmd.Cmd):
6571

6672
def __init__(self):
6773
self.config_fields = {'host': 'localhost', 'port': '8080',
68-
'apiKey': '', 'secretKey': '',
69-
'prompt': 'cloudmonkey> ', 'color': 'true',
74+
'apikey': '', 'secretkey': '',
75+
'prompt': '🐵 cloudmonkey>', 'color': 'true',
7076
'log_file':
7177
os.path.expanduser('~/.cloudmonkey_log'),
7278
'history_file':
@@ -77,7 +83,7 @@ def __init__(self):
7783
for key in self.config_fields.keys():
7884
setattr(self, key, self.config_fields[key])
7985
config = self.write_config()
80-
print("🐵 Set your apiKey, secretKey, host, port, prompt, color,"
86+
print("Set your apikey, secretkey, host, port, prompt, color,"
8187
" log_file, history_file using the set command!")
8288

8389
for key in self.config_fields.keys():
@@ -93,12 +99,6 @@ def __init__(self):
9399
if not os.path.exists(self.config_file):
94100
config = self.write_config()
95101

96-
# Fix autocompletion issue
97-
if sys.platform == "darwin":
98-
readline.parse_and_bind("bind ^I rl_complete")
99-
else:
100-
readline.parse_and_bind("tab: complete")
101-
102102
# Enable history support
103103
try:
104104
if os.path.exists(self.history_file):
@@ -204,7 +204,7 @@ def print_result_as_instance(node):
204204

205205
def make_request(self, command, requests={}):
206206
conn = cloudConnection(self.host, port=int(self.port),
207-
apiKey=self.apiKey, securityKey=self.secretKey,
207+
apiKey=self.apikey, securityKey=self.secretkey,
208208
logging=logging.getLogger("cloudConnection"))
209209
try:
210210
response = conn.make_request(command, requests)
@@ -317,28 +317,25 @@ def complete_api(self, text, line, begidx, endidx):
317317
def do_set(self, args):
318318
"""
319319
Set config for CloudStack CLI. Available options are:
320-
host, port, apiKey, secretKey, log_file, history_file
320+
host, port, apikey, secretkey, log_file, history_file
321321
You may also edit your ~/.cloudmonkey_config instead of using set.
322322
323323
Example:
324324
set host 192.168.56.2
325325
set prompt 🐵 cloudmonkey>
326326
set log_file /var/log/cloudmonkey.log
327327
"""
328-
args = shlex.split(args.strip())
329-
if len(args) == 2:
330-
key, value = args
331-
# Note: keys and fields should have same names
332-
setattr(self, key, value)
333-
self.write_config()
334-
else:
335-
self.print_shell("Please use the syntax: set valid-key value")
328+
args = args.strip().partition(" ")
329+
key, value = (args[0], args[2])
330+
# Note: keys and class attributes should have same names
331+
setattr(self, key, value)
332+
self.write_config()
336333

337334
def complete_set(self, text, line, begidx, endidx):
338335
mline = line.partition(" ")[2]
339336
offs = len(mline) - len(text)
340337
return [s[offs:] for s in
341-
['host', 'port', 'apiKey', 'secretKey', 'prompt', 'color',
338+
['host', 'port', 'apikey', 'secretkey', 'prompt', 'color',
342339
'log_file', 'history_file'] if s.startswith(mline)]
343340

344341
def do_shell(self, args):

tools/cli/cloudmonkey/version.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. 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,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# Use following rules for versioning:
19+
# <cli major version>.<cloudstack minor version>.<cloudstack major version>
20+
# Example: For CloudStack 4.1.x, CLI version should be 0.1.4
21+
__version__ = "0.0.4"
22+

tools/cli/setup.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626

2727
name = 'cloudmonkey'
2828
version = __version__
29+
requires = ['clint>=0.3.0',]
30+
31+
try:
32+
import readline
33+
except ImportError:
34+
requires.append('readline')
2935

3036
setup(
3137
name = name,
@@ -41,10 +47,7 @@
4147
platforms = ("Any",),
4248
license = 'ASL 2.0',
4349
packages = find_packages(),
44-
install_requires = [
45-
'clint>=0.3.0',
46-
'readline',
47-
],
50+
install_requires = requires,
4851
include_package_data = True,
4952
zip_safe = False,
5053
classifiers = [

0 commit comments

Comments
 (0)