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