|
10 | 10 | flex_length_tps_network, fixed_length_tps_network, tps_scheme |
11 | 11 | ) |
12 | 12 | from paths_cli.wizard.tools import yes_no, a_an |
13 | | -from paths_cli.parsing.core import custom_eval |
| 13 | +from paths_cli.wizard.core import get_object |
14 | 14 | from paths_cli.wizard.errors import ( |
15 | 15 | FILE_LOADING_ERROR_MSG, RestartObjectException |
16 | 16 | ) |
17 | 17 | from paths_cli.wizard.joke import name_joke |
| 18 | +from paths_cli.parsing.core import custom_eval |
18 | 19 |
|
19 | 20 | import shutil |
20 | 21 |
|
@@ -73,14 +74,13 @@ def _speak(self, content, preface): |
73 | 74 | wrapped.append(wrap_line) |
74 | 75 | self.console.print("\n".join(wrapped)) |
75 | 76 |
|
| 77 | + @get_object |
76 | 78 | def ask(self, question, options=None, default=None, helper=None): |
77 | | - result = None |
78 | | - while result is None: |
79 | | - result = self.console.input("🧙 " + question + " ") |
80 | | - self.console.print() |
81 | | - if helper and result.startswith("?"): |
82 | | - helper(self, result) |
83 | | - result = None |
| 79 | + result = self.console.input("🧙 " + question + " ") |
| 80 | + self.console.print() |
| 81 | + if helper and result.startswith("?"): |
| 82 | + helper(self, result) |
| 83 | + return |
84 | 84 | return result |
85 | 85 |
|
86 | 86 | def say(self, content, preface="🧙 "): |
@@ -117,21 +117,20 @@ def ask_enumerate(self, question, options): |
117 | 117 | return result |
118 | 118 |
|
119 | 119 | # this should match the args for wizard.ask |
| 120 | + @get_object |
120 | 121 | def ask_custom_eval(self, question, options=None, default=None, |
121 | 122 | helper=None, type_=float): |
122 | | - result = None |
123 | | - while result is None: |
124 | | - as_str = self.ask(question, options=options, default=default, |
125 | | - helper=helper) |
126 | | - try: |
127 | | - result = type_(custom_eval(as_str)) |
128 | | - except Exception as e: |
129 | | - self.exception(f"Sorry, I couldn't understand the input " |
130 | | - f"'{as_str}'", e) |
131 | | - result = None |
132 | | - |
| 123 | + as_str = self.ask(question, options=options, default=default, |
| 124 | + helper=helper) |
| 125 | + try: |
| 126 | + result = type_(custom_eval(as_str)) |
| 127 | + except Exception as e: |
| 128 | + self.exception(f"Sorry, I couldn't understand the input " |
| 129 | + f"'{as_str}'", e) |
| 130 | + return |
133 | 131 | return result |
134 | 132 |
|
| 133 | + |
135 | 134 | def obj_selector(self, store_name, text_name, create_func): |
136 | 135 | opts = {name: lambda wiz, o=obj: o |
137 | 136 | for name, obj in getattr(self, store_name).items()} |
@@ -173,31 +172,32 @@ def register(self, obj, obj_type, store_name): |
173 | 172 | store_dict[obj.name] = obj |
174 | 173 | return obj |
175 | 174 |
|
| 175 | + @get_object |
176 | 176 | def get_storage(self): |
177 | 177 | from openpathsampling.experimental.storage import Storage |
178 | | - storage = None |
179 | | - while storage is None: |
180 | | - filename = self.ask("Where would you like to save your setup " |
181 | | - "database?") |
182 | | - if not filename.endswith(".db"): |
183 | | - self.bad_input("Files produced by this wizard must end in " |
184 | | - "'.db'.") |
185 | | - continue |
186 | | - |
187 | | - if os.path.exists(filename): |
188 | | - overwrite = self.ask(f"{filename} exists. Overwrite it?", |
189 | | - options=["[Y]es", "[N]o"]) |
190 | | - overwrite = yes_no(overwrite) |
191 | | - if not overwrite: |
192 | | - continue |
193 | | - |
194 | | - try: |
195 | | - storage = Storage(filename, mode='w') |
196 | | - except Exception as e: |
197 | | - self.exception(FILE_LOADING_ERROR_MSG, e) |
| 178 | + filename = self.ask("Where would you like to save your setup " |
| 179 | + "database?") |
| 180 | + if not filename.endswith(".db"): |
| 181 | + self.bad_input("Files produced by this wizard must end in " |
| 182 | + "'.db'.") |
| 183 | + return |
| 184 | + |
| 185 | + if os.path.exists(filename): |
| 186 | + overwrite = self.ask(f"{filename} exists. Overwrite it?", |
| 187 | + options=["[Y]es", "[N]o"]) |
| 188 | + overwrite = yes_no(overwrite) |
| 189 | + if not overwrite: |
| 190 | + return |
| 191 | + |
| 192 | + try: |
| 193 | + storage = Storage(filename, mode='w') |
| 194 | + except Exception as e: |
| 195 | + self.exception(FILE_LOADING_ERROR_MSG, e) |
| 196 | + return |
198 | 197 |
|
199 | 198 | return storage |
200 | 199 |
|
| 200 | + |
201 | 201 | def _storage_description_line(self, store_name): |
202 | 202 | store = getattr(self, store_name) |
203 | 203 | if len(store) == 0: |
|
0 commit comments