This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
user_interface.py
134 lines (114 loc) · 5.14 KB
/
user_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
r"""
Command line user interface for doctesting
This module provides :class:`DoctestUserInterface`, an implementation of a
:class:`sage.dev.user_interface.UserInterface` on the command line which can be
used for (non-interactive) doctesting.
AUTHORS:
- Julian Rueth: initial version
"""
#*****************************************************************************
# Copyright (C) 2013 Julian Rueth <julian.rueth@fsfe.org>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
import six
from getpass import getpass
from sage.dev.cmd_line_interface import CmdLineInterface, INFO
class DoctestUserInterface(CmdLineInterface, list):
r"""
A :class:`sage.dev.user_interface.UserInterface` which can be used for
doctesting. Whenever the user would normally be prompted for input, the
answer to the question is taken to be `self.pop()`, i.e., answers can be
provided by appending them via :meth:`append`.
EXAMPLES::
sage: from sage.dev.test.config import DoctestConfig
sage: from sage.dev.test.user_interface import DoctestUserInterface
sage: UI = DoctestUserInterface(DoctestConfig()["UI"])
sage: UI.append("Answer")
sage: UI._get_input("Question?")
Question? Answer
'Answer'
sage: UI._get_input("Question?")
Traceback (most recent call last):
...
RuntimeError: no answers left in DoctestUserInterface for question "Question? ".
"""
def _get_input(self, prompt, options=None, default=None, input_func=six.moves.input):
r"""
Overwrites
:meth:`sage.dev.cmd_line_interface.CmdLineInterface._get_input` for
doctesting.
TESTS::
sage: from sage.dev.test.config import DoctestConfig
sage: from sage.dev.test.user_interface import DoctestUserInterface
sage: UI = DoctestUserInterface(DoctestConfig()["UI"])
sage: UI.append('')
sage: UI._get_input("Should I delete your home directory?", ("yes","no","maybe"), default=0)
Should I delete your home directory? [Yes/no/maybe]
'yes'
sage: UI._get_input("Should I delete your home directory?", ("yes","no","maybe"), default=0)
Traceback (most recent call last):
...
RuntimeError: no answers left in DoctestUserInterface for question "Should
I delete your home directory? [Yes/no/maybe] ".
"""
old_input_func = input_func
def input_func(prompt):
if not self:
raise RuntimeError('no answers left in DoctestUserInterface for'
' question "{0}".'.format(prompt))
ret = self.pop()
if old_input_func is getpass:
self.show(prompt)
else:
self.show(prompt+ret)
return ret
return CmdLineInterface._get_input(self, prompt, options, default, input_func)
def _show(self, message, log_level, *args):
r"""
Overwrites :meth:`sage.dev.cmd_line_interface.CmdLineInterface._show`
for doctesting. In particular, no line wrapping is performed.
TESTS::
sage: from sage.dev.test.config import DoctestConfig
sage: from sage.dev.test.user_interface import DoctestUserInterface
sage: UI = DoctestUserInterface(DoctestConfig()["UI"])
sage: UI.info("I ate {0} for dinner, a whole {1} for dessert, and then took a nice walk around the lake.", 'filet mignon', 'apple pie') # indirect doctest
# I ate filet mignon for dinner, a whole apple pie for dessert, and then took a nice walk around the lake.
"""
if len(args) > 0:
message = message.format(*args)
if log_level == INFO:
prefix = '# '
else:
prefix = ''
message = (prefix + line for line in message.splitlines())
print(self._color_code(log_level) +
'\n'.join(message) +
self._color_code())
def edit(self, filename):
r"""
Overwrites :meth:`sage.dev.cmd_line_interface.CmdLineInterface.edit`
for doctesting.
TESTS::
sage: tmp = tmp_filename()
sage: from sage.dev.test.config import DoctestConfig
sage: from sage.dev.test.user_interface import DoctestUserInterface
sage: UI = DoctestUserInterface(DoctestConfig()["UI"])
sage: UI.append("Some\nlines\n")
sage: UI.edit(tmp)
sage: print open(tmp,'r').read()
Some
lines
<BLANKLINE>
sage: UI.edit(tmp)
Traceback (most recent call last):
...
RuntimeError: no answers left in DoctestUserInterface
sage: os.unlink(tmp)
"""
if not self:
raise RuntimeError("no answers left in DoctestUserInterface")
open(filename, 'w').write(self.pop())