Skip to content

Commit 4da1b9a

Browse files
committed
move MockConsole, start to get_object decorator
1 parent 68066b1 commit 4da1b9a

4 files changed

Lines changed: 55 additions & 47 deletions

File tree

‎paths_cli/tests/wizard/mock_wizard.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,36 @@ def make_mock_retry_wizard(inputs):
1010
wizard = Wizard([])
1111
wizard.console.input = mock.Mock(side_effect=inputs)
1212
return wizard
13+
14+
class MockConsole:
15+
def __init__(self, inputs=None):
16+
if isinstance(inputs, str):
17+
inputs = [inputs]
18+
elif inputs is None:
19+
inputs = []
20+
self.inputs = inputs
21+
self._input_iter = iter(inputs)
22+
self.log = []
23+
self.width = 80
24+
self.input_call_count = 0
25+
26+
def print(self, content=""):
27+
self.log.append(content)
28+
29+
def input(self, content):
30+
self.input_call_count += 1
31+
user_input = next(self._input_iter)
32+
self.log.append(content + " " + user_input)
33+
return user_input
34+
35+
@property
36+
def log_text(self):
37+
return "\n".join(self.log)
38+
39+
def mock_wizard(inputs):
40+
wizard = Wizard([])
41+
console = MockConsole(inputs)
42+
wizard.console = console
43+
return wizard
44+
45+

‎paths_cli/tests/wizard/test_wizard.py‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
import mock
33
from paths_cli.tests.wizard.mock_wizard import (
4-
make_mock_wizard, make_mock_retry_wizard
4+
MockConsole
55
)
66
import pathlib
77

@@ -10,30 +10,6 @@
1010

1111
from paths_cli.wizard.wizard import *
1212

13-
class MockConsole:
14-
def __init__(self, inputs=None):
15-
if isinstance(inputs, str):
16-
inputs = [inputs]
17-
elif inputs is None:
18-
inputs = []
19-
self.inputs = inputs
20-
self._input_iter = iter(inputs)
21-
self.log = []
22-
self.width = 80
23-
self.input_call_count = 0
24-
25-
def print(self, content=""):
26-
self.log.append(content)
27-
28-
def input(self, content):
29-
self.input_call_count += 1
30-
user_input = next(self._input_iter)
31-
self.log.append(content + " " + user_input)
32-
return user_input
33-
34-
@property
35-
def log_text(self):
36-
return "\n".join(self.log)
3713

3814

3915
class TestWizard:

‎paths_cli/wizard/core.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ def get_missing_object(wizard, obj_dict, display_name, fallback_func):
6464
obj = obj_dict[sel]
6565
return obj
6666

67+
68+
def get_object(func):
69+
def inner(*args, **kwargs):
70+
obj = None
71+
while obj is None:
72+
obj = func(*args, **kwargs)
73+
return obj
74+
return inner

‎paths_cli/wizard/openmm.py‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from paths_cli.wizard.errors import FILE_LOADING_ERROR_MSG, not_installed
2+
from paths_cli.wizard.core import get_object
23
try:
34
from simtk import openmm as mm
45
import mdtraj as md
@@ -16,6 +17,7 @@ def _openmm_serialization_helper(wizard, user_input): # no-cov
1617
"simtk.openmm.openmm.XmlSerializer.html")
1718

1819

20+
@get_object
1921
def _load_openmm_xml(wizard, obj_type):
2022
xml = wizard.ask(
2123
f"Where is the XML file for your OpenMM {obj_type}?",
@@ -30,20 +32,17 @@ def _load_openmm_xml(wizard, obj_type):
3032
else:
3133
return obj
3234

35+
@get_object
3336
def _load_topology(wizard):
3437
import openpathsampling as paths
35-
topology = None
36-
while topology is None:
37-
filename = wizard.ask("Where is a PDB file describing your system?")
38-
try:
39-
snap = paths.engines.openmm.snapshot_from_pdb(filename)
40-
except Exception as e:
41-
wizard.exception(FILE_LOADING_ERROR_MSG, e)
42-
continue
43-
44-
topology = snap.topology
38+
filename = wizard.ask("Where is a PDB file describing your system?")
39+
try:
40+
snap = paths.engines.openmm.snapshot_from_pdb(filename)
41+
except Exception as e:
42+
wizard.exception(FILE_LOADING_ERROR_MSG, e)
43+
return
4544

46-
return topology
45+
return snap.topology
4746

4847
def openmm(wizard):
4948
import openpathsampling as paths
@@ -56,17 +55,9 @@ def openmm(wizard):
5655
"To use OpenMM in OPS, you need to provide XML versions of "
5756
"your system, integrator, and some file containing "
5857
"topology information.")
59-
system = integrator = topology = None
60-
while system is None:
61-
system = _load_openmm_xml(wizard, 'system')
62-
63-
while integrator is None:
64-
integrator = _load_openmm_xml(wizard, 'integrator')
65-
66-
while topology is None:
67-
topology = _load_topology(wizard)
68-
69-
n_frames_max = n_steps_per_frame = None
58+
system = _load_openmm_xml(wizard, 'system')
59+
integrator = _load_openmm_xml(wizard, 'integrator')
60+
topology = _load_topology(wizard)
7061

7162
n_steps_per_frame = wizard.ask_custom_eval(
7263
"How many MD steps per saved frame?", type_=int

0 commit comments

Comments
 (0)