Repository navigation
Wizard pauses #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Wizard pauses #62
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
967371d
Start to wizard pauses (including tests)
dwhswenson ec5c859
Merge branch 'main' of github.com:openpathsampling/openpathsampling-c…
dwhswenson 6fb06fc
Use the pauses, add section splitting line
dwhswenson 72669fd
fix tests for pausing
dwhswenson 61b8da4
Update paths_cli/wizard/pause.py
dwhswenson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import pytest | ||
| import time | ||
| from paths_cli.tests.wizard.mock_wizard import mock_wizard | ||
|
|
||
| from paths_cli.wizard.pause import * | ||
|
|
||
| @pytest.fixture(autouse=True, scope="module") | ||
| def use_default_pause_style(): | ||
| with pause_style('default'): | ||
| yield | ||
|
|
||
|
|
||
| def test_get_pause_style(): | ||
| default_style = PAUSE_STYLES['default'] | ||
| assert get_pause_style() == default_style | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('input_type', ['string', 'tuple', 'PauseStyle']) | ||
| def test_set_pause_style(input_type): | ||
| # check that we have the default settings | ||
| default_style = PAUSE_STYLES['default'] | ||
| test_style = PAUSE_STYLES['testing'] | ||
| input_val = { | ||
| 'string': 'testing', | ||
| 'tuple': tuple(test_style), | ||
| 'PauseStyle': PauseStyle(*test_style) | ||
| }[input_type] | ||
| assert input_val is not test_style # always a different object | ||
| assert get_pause_style() == default_style | ||
| set_pause_style(input_val) | ||
| assert get_pause_style() != default_style | ||
| assert get_pause_style() == test_style | ||
| set_pause_style('default') # explicitly reset default | ||
|
|
||
|
|
||
| def test_set_pause_bad_name(): | ||
| with pytest.raises(RuntimeError, match="Unknown pause style"): | ||
| set_pause_style('foo') | ||
|
|
||
| # ensure we didn't break anything for later tests | ||
| assert get_pause_style() == PAUSE_STYLES['default'] | ||
|
|
||
|
|
||
| def test_pause_style_context(): | ||
| assert get_pause_style() == PAUSE_STYLES['default'] | ||
| with pause_style('testing'): | ||
| assert get_pause_style() == PAUSE_STYLES['testing'] | ||
| assert get_pause_style() == PAUSE_STYLES['default'] | ||
|
|
||
|
|
||
| def _run_pause_test(func): | ||
| test_style = PAUSE_STYLES['testing'] | ||
| expected = getattr(test_style, func.__name__) | ||
| wiz = mock_wizard([]) | ||
| with pause_style(test_style): | ||
| start = time.time() | ||
| func(wiz) | ||
| duration = time.time() - start | ||
| assert expected <= duration < 1.1 * duration | ||
| return wiz | ||
|
|
||
|
|
||
| def test_short(): | ||
| _ = _run_pause_test(short) | ||
|
|
||
|
|
||
| def test_long(): | ||
| _ = _run_pause_test(long) | ||
|
|
||
|
|
||
| def test_section(): | ||
| wiz = _run_pause_test(section) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import time | ||
| import contextlib | ||
| from collections import namedtuple | ||
|
|
||
| PauseStyle = namedtuple("PauseStyle", ['short', 'long', 'section']) | ||
|
|
||
| PAUSE_STYLES = { | ||
| 'testing': PauseStyle(0.01, 0.03, 0.05), | ||
| 'default': PauseStyle(0.1, 0.5, 0.75), | ||
| 'nopause': PauseStyle(0.0, 0.0, 0.0), | ||
| } | ||
|
|
||
| _PAUSE_STYLE = PAUSE_STYLES['default'] | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def pause_style(style): | ||
| """Context manager for pause styles. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| style : :class:`.PauseStyle` | ||
| pause style to use within the context | ||
| """ | ||
| old_style = get_pause_style() | ||
| try: | ||
| set_pause_style(style) | ||
| yield | ||
| finally: | ||
| set_pause_style(old_style) | ||
|
|
||
|
|
||
| def get_pause_style(): | ||
| """Get the current pause style""" | ||
| global _PAUSE_STYLE | ||
| return _PAUSE_STYLE | ||
|
|
||
|
|
||
| def set_pause_style(style): | ||
| """Set the pause style | ||
|
|
||
| Parameters | ||
| ---------- | ||
| pause_style : :class:`.PauseStyle` or str | ||
| pause style to use, can be a string if the style is registered in | ||
| pause.PAUSE_STYLES | ||
| """ | ||
| global _PAUSE_STYLE | ||
| if isinstance(style, str): | ||
| try: | ||
| _PAUSE_STYLE = PAUSE_STYLES[style] | ||
| except KeyError as exc: | ||
| raise RuntimeError(f"Unknown pause style: '{style}'") from exc | ||
| else: | ||
| _PAUSE_STYLE = style | ||
|
|
||
|
|
||
| def section(wizard): | ||
| """Section break (pause and possible visual cue). | ||
| """ | ||
| time.sleep(_PAUSE_STYLE.section) | ||
| wizard.console.draw_hline() | ||
|
|
||
|
|
||
| def long(wizard): | ||
| """Long pause from the wizard""" | ||
| time.sleep(_PAUSE_STYLE.long) | ||
|
|
||
|
|
||
| def short(wizard): | ||
| """Short pause from the wizard""" | ||
| time.sleep(_PAUSE_STYLE.short) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.