Merge lp:~reynolds.chin/glance/adaptive_index_listing into lp:glance/diablo

Proposed by Reynolds Chin
Status: Needs review
Proposed branch: lp:~reynolds.chin/glance/adaptive_index_listing
Merge into: lp:glance/diablo
Diff against target: 126 lines (+77/-4)
1 file modified
bin/glance (+77/-4)
To merge this branch: bzr merge lp:~reynolds.chin/glance/adaptive_index_listing
Reviewer Review Type Date Requested Status
Glance Drivers Pending
Brian Waldon Pending
Review via email: mp+74067@code.launchpad.net

Description of the change

the "glance index" provides a more style of listing images.
1. the number of image indexes is "adapted" to the size of the console, while you still can customize the number of output per page through the parameter --limit
2. users "need not" to press the "enter key" to confirm YES or NO.
3. seamless output, you will never see the prompt message by scrolling the console's bar upward.

usage: glance index (for adaptive listing)
        glance index --limit 15 (for customized listing)

To post a comment you must log in.
212. By Reynolds Chin

improvement:
1. you never see prompt message by scrolling the screen upward
2. fully use of rows in listing

213. By Reynolds Chin

remove wrong if-statement

214. By Reynolds Chin

compensation for row while the header is false

Revision history for this message
Kevin L. Mitchell (klmitch) wrote :

First, you should be aware that merging is now done through Gerrit; you should review the workflow at http://wiki.openstack.org/GerritWorkflow for information on that.

Now, on to your patch. My first major concern is that the constructs you use may not be 100% portable. I see that, in most places, you've used try/except blocks to try to mitigate that, but you use termios in getch() without a try/except, and termios is UNIX-specific. I also see some style problems—for instance, you removed a blank line (line 8), so there is only one line between the DEFAULT_COL_LIMIT assignment and the def catch_error(). Also, I think you assign to the wrong variable on line 34 of the patch, and your indentation in the blocks starting at line 40 seems off. The space before the comma on line 72 should probably be removed, and you don't have enough blank lines between def getch() and def user_response().

Have you run this through PEP8? That will highlight certain style problems I may not be seeing here. You should also run the test suite and make sure all the tests pass, particularly the functional tests that hit "glance index". (The test suite also, incidentally, runs PEP8; your patch has to get a clean bill of health from PEP8 before the automated system will allow it to be merged.)

215. By Reynolds Chin

bin/glance get a clean bill of health from PEP8

Unmerged revisions

215. By Reynolds Chin

bin/glance get a clean bill of health from PEP8

214. By Reynolds Chin

compensation for row while the header is false

213. By Reynolds Chin

remove wrong if-statement

212. By Reynolds Chin

improvement:
1. you never see prompt message by scrolling the screen upward
2. fully use of rows in listing

211. By Reynolds Chin

the "glance index" provides a more style of listing images.
1. the number of image indexes is "adapted" to the size of the console,
   while you still can customize the number of output per page through the parameter --limit
2. users need not to press the "enter key" to confirm YES or NO.
3. the prompt "Fetch next page?" wouldn't be concatenated to the output

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/glance'
2--- bin/glance 2011-08-30 21:48:11 +0000
3+++ bin/glance 2011-09-07 03:29:23 +0000
4@@ -48,6 +48,8 @@
5
6 SUCCESS = 0
7 FAILURE = 1
8+DEFAULT_ROW_LIMIT = 128
9+DEFAULT_COL_LIMIT = 128
10
11
12 #TODO(sirp): make more of the actions use this decorator
13@@ -70,6 +72,77 @@
14 return wrap
15
16
17+def getTerminalSize():
18+ """
19+ determine the number of Rows and Cols of a console
20+ code inpired by Johannes WeiB (http://tux4u.de/)
21+ """
22+ def ioctl_GWINSZ(fd):
23+ try:
24+ import fcntl
25+ import termios
26+ import struct
27+ import os
28+ ncol_nrow = struct.unpack('hh', \
29+ fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
30+ except:
31+ return None
32+ return ncol_nrow
33+ nrow_ncol = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
34+ if not nrow_ncol:
35+ try:
36+ fd = os.open(os.ctermid(), os.O_RDONLY)
37+ nrow_ncol = ioctl_GWINSZ(fd)
38+ os.close(fd)
39+ except:
40+ pass
41+ if not nrow_ncol:
42+ try:
43+ import subprocess
44+ cmd = "stty size"
45+ p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
46+ p.wait()
47+ if p.returncode == 0:
48+ return tuple(int(x) for x in p.stdout.read().split())
49+ except:
50+ nrow_ncol = (DEFAULT_COL_LIMIT, DEFAULT_ROW_LIMIT)
51+ return int(nrow_ncol[0]), int(nrow_ncol[1])
52+
53+
54+def getch():
55+ """
56+ getchar
57+ """
58+ import sys
59+ import tty
60+ import termios
61+ fd = sys.stdin.fileno()
62+ old = termios.tcgetattr(fd)
63+ try:
64+ tty.setraw(fd)
65+ return sys.stdin.read(1)
66+ finally:
67+ termios.tcsetattr(fd, termios.TCSADRAIN, old)
68+
69+
70+def user_response(prompt, quit='q'):
71+ """
72+ interactie question dialog with user without pressing the return.
73+ Press any to confirm, while you can map a key for "no"
74+
75+ :param prompt: question/statement to present to user (string)
76+ :param quit: it is the key mapped to NO. The key 'q' is defaultly mapped.
77+ """
78+ print prompt,
79+ key = getch()
80+ if key == quit:
81+ return False
82+ else:
83+ sys.stdout.write('\b' * len(prompt))
84+ sys.stdout.flush()
85+ return True
86+
87+
88 def get_percent_done(image):
89 try:
90 pct_done = image['size'] * 100 / int(image['expected_size'])
91@@ -423,7 +496,7 @@
92 """Driver function for images_index"""
93 parameters = {
94 "filters": filters,
95- "limit": limit,
96+ "limit": limit or getTerminalSize()[0],
97 }
98
99 optional_kwargs = ['marker', 'sort_key', 'sort_dir']
100@@ -454,7 +527,7 @@
101 image['size'])
102
103 if not options.force and len(images) == limit and \
104- not user_confirm("Fetch next page?", True):
105+ not user_response("press any key to continue, or press 'q' to quit"):
106 return SUCCESS
107
108 parameters['marker'] = images[-1]['id']
109@@ -474,7 +547,7 @@
110 fields are treated as image metadata properties"""
111 client = get_client(options)
112 filters = get_image_filters_from_args(args)
113- limit = options.limit
114+ limit = options.limit or getTerminalSize()[0] - 3
115 marker = options.marker
116 sort_key = options.sort_key
117 sort_dir = options.sort_dir
118@@ -990,7 +1063,7 @@
119 metavar="TOKEN", default=None,
120 help="Authentication token to use to identify the "
121 "client to the glance server")
122- parser.add_option('--limit', dest="limit", metavar="LIMIT", default=10,
123+ parser.add_option('--limit', dest="limit", metavar="LIMIT", default=None,
124 type="int", help="Page size to use while "
125 "requesting image metadata")
126 parser.add_option('--marker', dest="marker", metavar="MARKER",

Subscribers

People subscribed via source and target branches

to all changes: