Skip to content

Commit 8ed2442

Browse files
committed
more pylint cleanup in the wizard
1 parent 89bf2cb commit 8ed2442

7 files changed

Lines changed: 78 additions & 46 deletions

File tree

‎paths_cli/tests/wizard/test_load_from_ops.py‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from paths_cli.tests.wizard.mock_wizard import mock_wizard
77

88
from paths_cli.wizard.load_from_ops import (
9-
named_objs_helper, _get_ops_storage, _get_ops_object, load_from_ops
9+
_get_ops_storage, _get_ops_object, load_from_ops
1010
)
1111

1212
# for some reason I couldn't get these to work with MagicMock
@@ -44,12 +44,6 @@ def ops_file_fixture():
4444
storage = FakeStorage(foo)
4545
return storage
4646

47-
def test_named_objs_helper(ops_file_fixture):
48-
helper_func = named_objs_helper(ops_file_fixture, 'foo')
49-
result = helper_func('any')
50-
assert "what I found" in result
51-
assert "bar" in result
52-
assert "baz" in result
5347

5448
@pytest.mark.parametrize('with_failure', [False, True])
5549
def test_get_ops_storage(tmpdir, with_failure):

‎paths_cli/wizard/errors.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
class ImpossibleError(Exception):
2+
"""Error to throw for sections that should be unreachable code"""
23
def __init__(self, msg=None):
34
if msg is None:
45
msg = "Something went really wrong. You should never see this."
56
super().__init__(msg)
67

8+
79
class RestartObjectException(BaseException):
10+
"""Exception to indicate the restart of an object.
11+
12+
Raised when the user issues a command to cause an object restart.
13+
"""
814
pass
915

16+
1017
def not_installed(wizard, package, obj_type):
18+
"""Behavior when an integration is not installed.
19+
20+
In actual practice, this calling code should ensure this doesn't get
21+
used. However, we keep it around as a defensive practice.
22+
23+
Parameters
24+
----------
25+
package : str
26+
name of the missing package
27+
obj_type : str
28+
name of the object category that would have been created
29+
"""
1130
retry = wizard.ask(f"Hey, it looks like you don't have {package} "
1231
"installed. Do you want to try a different "
1332
f"{obj_type}, or do you want to quit?",
1433
options=["[R]etry", "[Q]uit"])
1534
if retry == 'r':
1635
raise RestartObjectException()
17-
elif retry == 'q':
36+
if retry == 'q':
37+
# TODO: maybe raise QuitWizard instead?
1838
exit()
19-
else: # no-cov
20-
raise ImpossibleError()
39+
raise ImpossibleError()
2140

2241

2342
FILE_LOADING_ERROR_MSG = ("Sorry, something went wrong when loading that "

‎paths_cli/wizard/helper.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1+
import sys
12
from .errors import RestartObjectException
23

34

45
class QuitWizard(BaseException):
5-
pass
6+
"""Exception raised when user expresses desire to quit the wizard"""
67

78

89
# the following command functions take cmd and ctx -- future commands might
910
# use the full command text or the context internally.
1011

1112
def raise_quit(cmd, ctx):
13+
"""Command function to quit the wizard (with option to save).
14+
"""
1215
raise QuitWizard()
1316

1417

1518
def raise_restart(cmd, ctx):
19+
"""Command function to restart the current object.
20+
"""
1621
raise RestartObjectException()
1722

1823

1924
def force_exit(cmd, ctx):
25+
"""Command function to force immediate exit.
26+
"""
2027
print("Exiting...")
21-
exit()
28+
sys.exit()
2229

2330

2431
HELPER_COMMANDS = {

‎paths_cli/wizard/joke.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,38 @@
1616
"It would also be a good name for a death metal band.",
1717
]
1818

19+
1920
def _joke1(name, obj_type): # no-cov
2021
return (f"I probably would have named it something like "
2122
f"'{random.choice(_NAMES)}'.")
2223

24+
2325
def _joke2(name, obj_type): # no-cov
2426
thing = random.choice(_THINGS)
2527
joke = (f"I had {a_an(thing)} {thing} named '{name}' "
2628
f"when I was young.")
2729
return joke
2830

31+
2932
def _joke3(name, obj_type): # no-cov
3033
return (f"I wanted to name my {random.choice(_SPAWN)} '{name}', but my "
3134
f"wife wouldn't let me.")
3235

36+
3337
def _joke4(name, obj_type): # no-cov
3438
a_an_thing = a_an(obj_type) + f" {obj_type}"
3539
return random.choice(_MISC).format(name=name, obj_type=obj_type,
3640
a_an_thing=a_an_thing)
3741

42+
3843
def name_joke(name, obj_type): # no-cov
44+
"""Make a joke about the naming process."""
3945
jokes = [_joke1, _joke2, _joke3, _joke4]
4046
weights = [5, 5, 3, 7]
4147
joke = random.choices(jokes, weights=weights)[0]
4248
return joke(name, obj_type)
4349

50+
4451
if __name__ == "__main__": # no-cov
4552
for _ in range(5):
4653
print()

‎paths_cli/wizard/load_from_ops.py‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@
55
LABEL = "Load existing from OPS file"
66

77

8-
def named_objs_helper(storage, store_name):
9-
def list_items(user_input, context=None):
10-
store = getattr(storage, store_name)
11-
names = [obj for obj in store if obj.is_named]
12-
outstr = "\n".join(['* ' + obj.name for obj in names])
13-
return f"Here's what I found:\n\n{outstr}"
14-
15-
return list_items
16-
17-
188
@get_object
199
def _get_ops_storage(wizard):
2010
filename = wizard.ask("What file can it be found in?",
@@ -40,6 +30,24 @@ def _get_ops_object(wizard, storage, store_name, obj_name):
4030

4131

4232
def load_from_ops(wizard, store_name, obj_name):
33+
"""Load an object from an OPS file
34+
35+
Parameters
36+
----------
37+
wizard : :class:`.Wizard`
38+
the wizard for user interaction
39+
store_name : str
40+
name of the store where this object will be found
41+
obj_name : str
42+
name of the object to load
43+
44+
Returns
45+
-------
46+
Any :
47+
the object loaded from the file
48+
"""
49+
# TODO: this might be replaced by something in compiling to load from
50+
# files
4351
wizard.say("Okay, we'll load it from an OPS file.")
4452
storage = _get_ops_storage(wizard)
4553
obj = _get_ops_object(wizard, storage, store_name, obj_name)

‎paths_cli/wizard/openmm.py‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
from paths_cli.wizard.errors import FILE_LOADING_ERROR_MSG, not_installed
2-
from paths_cli.wizard.core import get_object
3-
4-
# should be able to simplify this try block when we drop OpenMM < 7.6
5-
from paths_cli.compat.openmm import mm, HAS_OPENMM
6-
7-
OPENMM_SERIALIZATION_URL=(
8-
"http://docs.openmm.org/latest/api-python/generated/"
9-
"openmm.openmm.XmlSerializer.html"
10-
)
1+
from paths_cli.compat.openmm import HAS_OPENMM
112

123
from paths_cli.wizard.parameters import ProxyParameter
134
from paths_cli.wizard.plugin_classes import WizardParameterObjectPlugin
145

156
from paths_cli.compiling.engines import OPENMM_PLUGIN as OPENMM_COMPILING
167

17-
_where_is_xml = "Where is the XML file for your OpenMM {obj_type}?"
18-
_xml_help = (
8+
OPENMM_SERIALIZATION_URL = (
9+
"http://docs.openmm.org/latest/api-python/generated/"
10+
"openmm.openmm.XmlSerializer.html"
11+
)
12+
13+
_WHERE_IS_XML = "Where is the XML file for your OpenMM {obj_type}?"
14+
_XML_HELP = (
1915
"You can write OpenMM objects like systems and integrators to XML "
2016
"files using the XMLSerializer class. Learn more here:\n"
2117
+ OPENMM_SERIALIZATION_URL
@@ -43,13 +39,13 @@
4339
),
4440
ProxyParameter(
4541
name='integrator',
46-
ask=_where_is_xml.format(obj_type='integrator'),
47-
helper=_xml_help,
42+
ask=_WHERE_IS_XML.format(obj_type='integrator'),
43+
helper=_XML_HELP,
4844
),
4945
ProxyParameter(
5046
name='system',
51-
ask=_where_is_xml.format(obj_type="system"),
52-
helper=_xml_help,
47+
ask=_WHERE_IS_XML.format(obj_type="system"),
48+
helper=_XML_HELP,
5349
),
5450
ProxyParameter(
5551
name='n_steps_per_frame',

‎paths_cli/wizard/parameters.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from paths_cli.compiling.tools import custom_eval
2-
import importlib
31
from collections import namedtuple
42

5-
from paths_cli.wizard.helper import Helper
63
from paths_cli.compiling.root_compiler import _CategoryCompilerProxy
74
from paths_cli.wizard.standard_categories import get_category_info
85

@@ -16,6 +13,7 @@
1613
defaults=[None, NO_DEFAULT, False, None]
1714
)
1815

16+
1917
class WizardParameter:
2018
"""Load a single parameter from the wizard.
2119
@@ -47,7 +45,7 @@ def __init__(self, name, ask, loader, *, helper=None, default=NO_DEFAULT,
4745
self.default = default
4846
self.autohelp = autohelp
4947
if summarize is None:
50-
summarize = lambda obj: str(obj)
48+
summarize = str
5149
self.summarize = summarize
5250

5351
@classmethod
@@ -66,15 +64,17 @@ def from_proxy(cls, proxy, compiler_plugin):
6664
loader = loader_dict[proxy.name]
6765
if isinstance(loader, _CategoryCompilerProxy):
6866
# TODO: can this import now be moved to top of file?
69-
from paths_cli.wizard.plugin_registration import get_category_wizard
67+
from paths_cli.wizard.plugin_registration import \
68+
get_category_wizard
7069
category = loader.category
7170
dct['loader'] = get_category_wizard(category)
7271
dct['ask'] = get_category_info(category).singular
7372
dct['store_name'] = get_category_info(category).storage
74-
cls = _ExistingObjectParameter
73+
cls_ = _ExistingObjectParameter
7574
else:
75+
cls_ = cls
7676
dct['loader'] = loader
77-
return cls(**dct)
77+
return cls_(**dct)
7878

7979
def __call__(self, wizard, context):
8080
"""Load the parameter.
@@ -268,7 +268,8 @@ def __call__(self, wizard, context=None):
268268
if n_existing == self.n_required:
269269
# early return in this case (return silently)
270270
return {self.name: self._get_existing(wizard)}
271-
elif n_existing > self.n_required:
271+
272+
if n_existing > self.n_required:
272273
sel = [self._select_single_existing(wizard)
273274
for _ in range(self.n_required)]
274275
dct = {self.name: sel}

0 commit comments

Comments
 (0)