Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

# -*- encoding: utf-8 -*- 

r""" 

Test Backend 

 

This backend is only for doctesting purposes. 

 

EXAMPLES: 

 

We switch to the test backend for the remainder of this file:: 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: dm = get_display_manager() 

sage: from sage.repl.rich_output.backend_test import BackendTest, TestObject 

sage: doctest_backend = dm.switch_backend(BackendTest()) 

sage: dm 

The Sage display manager using the test backend 

 

sage: dm._output_promotions 

{<class 'sage.repl.rich_output.output_basic.OutputPlainText'>: 

<class 'sage.repl.rich_output.backend_test.TestOutputPlainText'>} 

sage: dm.displayhook(1/2) 

1/2 [TestOutputPlainText] 

TestOutputPlainText container 

 

sage: test = TestObject() 

sage: test 

called the _repr_ method 

sage: dm.displayhook(test) 

called the _rich_repr_ method [TestOutputPlainText] 

TestOutputPlainText container 

""" 

 

#***************************************************************************** 

# Copyright (C) 2015 Volker Braun <vbraun.name@gmail.com> 

# 

# 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/ 

#***************************************************************************** 

 

 

from sage.structure.sage_object import SageObject 

from sage.repl.rich_output.backend_base import BackendBase 

from sage.repl.rich_output.output_catalog import OutputPlainText, OutputImagePng 

 

 

class TestOutputPlainText(OutputPlainText): 

 

def __init__(self, *args, **kwds): 

""" 

Backend-specific subclass of the plain text output container. 

 

Backends must not influence how the display system constructs 

output containers, they can only control how the output 

container is displayed. In particular, we cannot override the 

constructor (only the 

:class:`~sage.repl.rich_output.output_basic.OutputPlainText` 

constructor is used). 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_test import TestOutputPlainText 

sage: TestOutputPlainText() 

Traceback (most recent call last): 

AssertionError: cannot override constructor 

""" 

raise AssertionError('cannot override constructor') 

 

def print_to_stdout(self): 

""" 

Write the data to stdout. 

 

This is just a convenience method to help with debugging. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: dm = get_display_manager() 

sage: test_output = dm.displayhook(123) 

123 [TestOutputPlainText] 

sage: type(test_output) 

<class 'sage.repl.rich_output.backend_test.TestOutputPlainText'> 

sage: test_output.print_to_stdout() 

123 [TestOutputPlainText] 

""" 

print('{0} [{1}]'.format(self.text.get_str(), self.__class__.__name__)) 

 

 

class TestObject(SageObject): 

""" 

Test object with both :meth:`_repr_` and :meth:`_rich_repr_` 

""" 

 

def _repr_(self): 

""" 

Return string representation 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_test import TestObject 

sage: obj = TestObject() 

sage: obj._repr_() 

'called the _repr_ method' 

""" 

return 'called the _repr_ method' 

 

def _rich_repr_(self, display_manager): 

""" 

Rich Output Magic Method 

 

See :mod:`sage.repl.rich_output` for details. 

 

EXAMPLES:: 

 

sage: display_manager = sage.repl.rich_output.get_display_manager() 

sage: from sage.repl.rich_output.backend_test import TestObject 

sage: obj = TestObject() 

sage: rich_output = obj._rich_repr_(display_manager); rich_output 

OutputPlainText container 

sage: rich_output.text.get_str() 

'called the _rich_repr_ method' 

""" 

tp = display_manager.types 

if tp.OutputPlainText in display_manager.supported_output(): 

return tp.OutputPlainText('called the _rich_repr_ method') 

 

 

class BackendTest(BackendBase): 

 

def _repr_(self): 

""" 

Return the string representation 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: display_manager = sage.repl.rich_output.get_display_manager() 

sage: backend = display_manager._backend 

sage: backend._repr_() 

'test' 

""" 

return 'test' 

 

def supported_output(self): 

""" 

Return the outputs that are supported by the backend. 

 

OUTPUT: 

 

Iterable of output container classes. Only the 

:class:`~sage.repl.rich_repr.backend_test.TestOutputPlainText` 

output container is supported by the test backend. 

 

EXAMPLES:: 

 

sage: display_manager = sage.repl.rich_output.get_display_manager() 

sage: backend = display_manager._backend 

sage: list(backend.supported_output()) 

[<class 'sage.repl.rich_output.backend_test.TestOutputPlainText'>] 

 

The output of this method is used by the display manager to 

set up the actual supported outputs. Compare:: 

 

sage: list(display_manager.supported_output()) 

[<class 'sage.repl.rich_output.output_basic.OutputPlainText'>] 

""" 

return set([TestOutputPlainText]) 

 

def display_immediately(self, plain_text, rich_output): 

""" 

Show output without going back to the command line prompt. 

 

INPUT: 

 

Same as :meth:`displayhook`. 

 

OUTPUT: 

 

This method returns the rich output for doctesting 

convenience. The actual display framework ignores the return 

value. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_basic import OutputPlainText 

sage: plain_text = OutputPlainText.example() 

sage: from sage.repl.rich_output.backend_test import BackendTest 

sage: backend = BackendTest() 

sage: backend.display_immediately(plain_text, plain_text) 

Example plain text output 

OutputPlainText container 

""" 

plain_text.print_to_stdout() 

if plain_text is not rich_output: 

print('rich output type: [{0}]'.format(rich_output.__class__.__name__)) 

return rich_output