File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed
Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ """Intents and performers for basic user interaction.
2+
3+ Use :obj:`effect.io.stdio_dispatcher` as a dispatcher for :obj:`Display` and
4+ :obj:`Prompt` that uses built-in Python standard io facilities.
5+ """
6+
7+ from __future__ import print_function
8+ import attr
9+ from six .moves import input
10+
11+ from . import sync_performer , TypeDispatcher
12+
13+
14+ @attr .s
15+ class Display (object ):
16+ """Display some text to the user."""
17+ output = attr .ib ()
18+
19+
20+ @attr .s
21+ class Prompt (object ):
22+ """Get some input from the user, with a prompt."""
23+ prompt = attr .ib ()
24+
25+
26+ @sync_performer
27+ def perform_display_print (dispatcher , intent ):
28+ """Perform a :obj:`Display` intent by printing the output."""
29+ print (intent .output )
30+
31+
32+ @sync_performer
33+ def perform_get_input_raw_input (dispatcher , intent ):
34+ """
35+ Perform a :obj:`Prompt` intent by using ``raw_input`` (or ``input`` on
36+ Python 3).
37+ """
38+ return input (intent .prompt )
39+
40+
41+ stdio_dispatcher = TypeDispatcher ({
42+ Display : perform_display_print ,
43+ Prompt : perform_get_input_raw_input ,
44+ })
Original file line number Diff line number Diff line change 1+ from . import Effect , sync_perform
2+ from .io import Display , Prompt , stdio_dispatcher
3+
4+
5+ def test_perform_display_print (capsys ):
6+ """The stdio dispatcher has a performer that prints output."""
7+ assert sync_perform (stdio_dispatcher , Effect (Display ("foo" ))) is None
8+ out , err = capsys .readouterr ()
9+ assert err == ''
10+ assert out == 'foo\n '
11+
12+
13+ def test_perform_get_input_raw_input (monkeypatch ):
14+ """The stdio dispatcher has a performer that reads input."""
15+ monkeypatch .setattr (
16+ 'effect.io.input' ,
17+ lambda p : 'my name' if p == '> ' else 'boo' )
18+ assert sync_perform (stdio_dispatcher , Effect (Prompt ('> ' ))) == 'my name'
You can’t perform that action at this time.
0 commit comments