Show More
@@ -1,121 +1,121 b'' | |||||
1 | """Utility functions for interacting with the console""" |
|
1 | """Utility functions for interacting with the console""" | |
2 | #----------------------------------------------------------------------------- |
|
2 | #----------------------------------------------------------------------------- | |
3 | # Copyright (c) 2013, the IPython Development Team. |
|
3 | # Copyright (c) 2013, the IPython Development Team. | |
4 | # |
|
4 | # | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 | # |
|
6 | # | |
7 | # The full license is in the file COPYING.txt, distributed with this software. |
|
7 | # The full license is in the file COPYING.txt, distributed with this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | # Used to determine python version |
|
14 | # Used to determine python version | |
15 | import sys |
|
15 | import sys | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Classes and functions |
|
18 | # Classes and functions | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 |
def input( |
|
21 | def input(prompt_text): | |
22 | """ |
|
22 | """ | |
23 | Prompt the user for input. |
|
23 | Prompt the user for input. | |
24 |
|
24 | |||
25 | The input command will change depending on the version of python |
|
25 | The input command will change depending on the version of python | |
26 | installed. To maintain support for 2 and earlier, we must use |
|
26 | installed. To maintain support for 2 and earlier, we must use | |
27 | raw_input in that case. Else use input. |
|
27 | raw_input in that case. Else use input. | |
28 |
|
28 | |||
29 | Parameters |
|
29 | Parameters | |
30 | ---------- |
|
30 | ---------- | |
31 | prompt_text : str |
|
31 | prompt_text : str | |
32 | Prompt to display to the user. |
|
32 | Prompt to display to the user. | |
33 | """ |
|
33 | """ | |
34 |
|
34 | |||
35 | # Try to get the python version. This command is only available in |
|
35 | # Try to get the python version. This command is only available in | |
36 | # python 2 and later, so it's important that we catch the exception |
|
36 | # python 2 and later, so it's important that we catch the exception | |
37 | # if the command isn't found. |
|
37 | # if the command isn't found. | |
38 | try: |
|
38 | try: | |
39 | majorversion = sys.version_info[0] |
|
39 | majorversion = sys.version_info[0] | |
40 | except: |
|
40 | except: | |
41 | majorversion = 1 |
|
41 | majorversion = 1 | |
42 |
|
42 | |||
43 | # Use the correct function to prompt the user for input depending on |
|
43 | # Use the correct function to prompt the user for input depending on | |
44 | # what python version the code is running in. |
|
44 | # what python version the code is running in. | |
45 | if majorversion >= 3: |
|
45 | if majorversion >= 3: | |
46 | return input(prompt_text) |
|
46 | return input(prompt_text) | |
47 | else: |
|
47 | else: | |
48 | return raw_input(prompt_text) |
|
48 | return raw_input(prompt_text) | |
49 |
|
49 | |||
50 |
|
50 | |||
51 |
def prompt_boolean( |
|
51 | def prompt_boolean(prompt, default=False): | |
52 | """ |
|
52 | """ | |
53 | Prompt the user for a boolean response. |
|
53 | Prompt the user for a boolean response. | |
54 |
|
54 | |||
55 | Parameters |
|
55 | Parameters | |
56 | ---------- |
|
56 | ---------- | |
57 | prompt : str |
|
57 | prompt : str | |
58 | prompt to display to the user |
|
58 | prompt to display to the user | |
59 | default : bool, optional |
|
59 | default : bool, optional | |
60 | response to return if none is given by the user |
|
60 | response to return if none is given by the user | |
61 | """ |
|
61 | """ | |
62 |
|
62 | |||
63 |
response = |
|
63 | response = input(prompt) | |
64 | response = response.strip().lower() |
|
64 | response = response.strip().lower() | |
65 |
|
65 | |||
66 | #Catch 1, true, yes as True |
|
66 | #Catch 1, true, yes as True | |
67 | if len(response) > 0 and (response == "1" or response[0] == "t" or response[0] == "y"): |
|
67 | if len(response) > 0 and (response == "1" or response[0] == "t" or response[0] == "y"): | |
68 | return True |
|
68 | return True | |
69 |
|
69 | |||
70 | #Catch 0, false, no as False |
|
70 | #Catch 0, false, no as False | |
71 | elif len(response) > 0 and (response == "0" or response[0] == "f" or response[0] == "n"): |
|
71 | elif len(response) > 0 and (response == "0" or response[0] == "f" or response[0] == "n"): | |
72 | return False |
|
72 | return False | |
73 |
|
73 | |||
74 | else: |
|
74 | else: | |
75 | return default |
|
75 | return default | |
76 |
|
76 | |||
77 |
|
77 | |||
78 |
def prompt_dictionary( |
|
78 | def prompt_dictionary(choices, default_style=1, menu_comments={}): | |
79 | """ |
|
79 | """ | |
80 | Prompt the user to chose one of many selections from a menu. |
|
80 | Prompt the user to chose one of many selections from a menu. | |
81 |
|
81 | |||
82 | Parameters |
|
82 | Parameters | |
83 | ---------- |
|
83 | ---------- | |
84 | choices : dictionary |
|
84 | choices : dictionary | |
85 | Keys - choice numbers (int) |
|
85 | Keys - choice numbers (int) | |
86 | Values - choice value (str), this is what the function will return |
|
86 | Values - choice value (str), this is what the function will return | |
87 | default_style : int, optional |
|
87 | default_style : int, optional | |
88 | Choice to select if the user doesn't respond |
|
88 | Choice to select if the user doesn't respond | |
89 | menu_comments : dictionary, optional |
|
89 | menu_comments : dictionary, optional | |
90 | Additional comments to append to the menu as it is displayed |
|
90 | Additional comments to append to the menu as it is displayed | |
91 | in the console. |
|
91 | in the console. | |
92 | Keys - choice numbers (int) |
|
92 | Keys - choice numbers (int) | |
93 | Values - comment (str), what will be appended to the |
|
93 | Values - comment (str), what will be appended to the | |
94 | corresponding choice |
|
94 | corresponding choice | |
95 | """ |
|
95 | """ | |
96 |
|
96 | |||
97 | # Build the menu that will be displayed to the user with |
|
97 | # Build the menu that will be displayed to the user with | |
98 | # all of the options available. |
|
98 | # all of the options available. | |
99 | prompt = "" |
|
99 | prompt = "" | |
100 | for key, value in choices.iteritems(): |
|
100 | for key, value in choices.iteritems(): | |
101 | prompt += "%d %s " % (key, value) |
|
101 | prompt += "%d %s " % (key, value) | |
102 | if key in menu_comments: |
|
102 | if key in menu_comments: | |
103 | prompt += menu_comments[key] |
|
103 | prompt += menu_comments[key] | |
104 | prompt += "\n" |
|
104 | prompt += "\n" | |
105 |
|
105 | |||
106 | # Continue to ask the user for a style until an appropriate |
|
106 | # Continue to ask the user for a style until an appropriate | |
107 | # one is specified. |
|
107 | # one is specified. | |
108 | response = -1 |
|
108 | response = -1 | |
109 | while (not response in choices): |
|
109 | while (not response in choices): | |
110 | try: |
|
110 | try: | |
111 |
text_response = |
|
111 | text_response = input(prompt) | |
112 |
|
112 | |||
113 | # Use default option if no input. |
|
113 | # Use default option if no input. | |
114 | if len(text_response.strip()) == 0: |
|
114 | if len(text_response.strip()) == 0: | |
115 | response = default_style |
|
115 | response = default_style | |
116 | else: |
|
116 | else: | |
117 | response = int(text_response) |
|
117 | response = int(text_response) | |
118 | except: |
|
118 | except: | |
119 | print("Error: Value is not an available option. 0 selects the default.\n") |
|
119 | print("Error: Value is not an available option. 0 selects the default.\n") | |
120 | return choices[response] |
|
120 | return choices[response] | |
121 | No newline at end of file |
|
121 |
General Comments 0
You need to be logged in to leave comments.
Login now