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

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

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

""" 

The backend used for doctests 

 

This backend is active during doctests. It should mimic the behavior 

of the IPython command line as close as possible. Without actually 

launching image viewers, of course. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: get_display_manager() 

The Sage display manager using the doctest backend 

""" 

 

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

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

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

 

import sys 

 

import six 

 

from sage.repl.rich_output.backend_base import BackendBase 

from sage.repl.rich_output.output_catalog import * 

 

 

 

class BackendDoctest(BackendBase): 

 

def _repr_(self): 

""" 

Return a string representation. 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_doctest import BackendDoctest 

sage: backend = BackendDoctest() 

sage: backend._repr_() 

'doctest' 

""" 

return 'doctest' 

 

def default_preferences(self): 

""" 

Return the backend's display preferences 

 

Matches the IPython command line display preferences to keep 

the differences between that and the doctests to a minimum. 

 

OUTPUT: 

 

Instance of 

:class:`~sage.repl.rich_output.preferences.DisplayPreferences`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline 

sage: backend = BackendIPythonCommandline() 

sage: backend.default_preferences() 

Display preferences: 

* graphics is not specified 

* supplemental_plot = never 

* text is not specified 

""" 

from sage.repl.rich_output.preferences import DisplayPreferences 

return DisplayPreferences(supplemental_plot='never') 

 

def install(self, **kwds): 

""" 

Switch to the doctest backend. 

 

This method is being called from within 

:meth:`~sage.repl.rich_output.display_manager.DisplayManager.switch_backend`. You 

should never call it by hand. 

 

INPUT: 

 

None of the optional keyword arguments are used in the doctest 

backend. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_doctest import BackendDoctest 

sage: backend = BackendDoctest() 

sage: backend.install() 

sage: backend.uninstall() 

""" 

self._old_displayhook = sys.displayhook 

sys.displayhook = self.get_display_manager().displayhook 

 

def uninstall(self): 

""" 

Switch away from the doctest backend 

 

This method is being called from within 

:meth:`~sage.repl.rich_output.display_manager.DisplayManager.switch_backend`. You 

should never call it by hand. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_doctest import BackendDoctest 

sage: backend = BackendDoctest() 

sage: backend.install() 

sage: backend.uninstall() 

""" 

sys.displayhook = self._old_displayhook 

 

def supported_output(self): 

""" 

Return the supported output types 

 

OUTPUT: 

 

Set of subclasses of 

:class:`~sage.repl.rich_output.output_basic.OutputBase`, the 

supported output container types. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_doctest import BackendDoctest 

sage: from sage.repl.rich_output.output_catalog import * 

sage: backend = BackendDoctest() 

sage: OutputPlainText in backend.supported_output() 

True 

sage: OutputSceneJmol in backend.supported_output() 

True 

""" 

return set([ 

OutputPlainText, OutputAsciiArt, OutputUnicodeArt, OutputLatex, 

OutputImagePng, OutputImageGif, OutputImageJpg, 

OutputImageSvg, OutputImagePdf, OutputImageDvi, 

OutputSceneJmol, OutputSceneCanvas3d, OutputSceneWavefront, 

OutputVideoOgg, OutputVideoWebM, OutputVideoMp4, 

OutputVideoFlash, OutputVideoMatroska, OutputVideoAvi, 

OutputVideoWmv, OutputVideoQuicktime, 

]) 

 

def displayhook(self, plain_text, rich_output): 

""" 

Display object from displayhook 

 

INPUT: 

 

- ``plain_text`` -- instance of 

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

plain text version of the output. 

 

- ``rich_output`` -- instance of an output container class 

(subclass of 

:class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed 

to be one of the output containers returned from 

:meth:`supported_output`, possibly the same as 

``plain_text``. 

 

EXAMPLES: 

 

This ends up calling the displayhook:: 

 

sage: plt = plot(sin) 

sage: plt 

Graphics object consisting of 1 graphics primitive 

sage: plt.show() 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: dm = get_display_manager() 

sage: dm.displayhook(plt) # indirect doctest 

Graphics object consisting of 1 graphics primitive 

""" 

self.validate(rich_output) 

if any(isinstance(rich_output, cls) 

for cls in [OutputPlainText, OutputAsciiArt, OutputLatex]): 

rich_output.print_to_stdout() 

else: 

plain_text.print_to_stdout() 

 

def display_immediately(self, plain_text, rich_output): 

""" 

Display object immediately 

 

INPUT: 

 

Same as :meth:`displayhook`. 

 

EXAMPLES: 

 

The following example does not call the displayhook. More 

precisely, the ``show()`` method returns ``None`` which is 

ignored by the displayhook. When running the example on a Sage 

display backend capable of displaying graphics outside of the 

displayhook, the plot is still shown. Nothing is shown during 

doctests:: 

 

sage: plt = plot(sin) 

sage: plt 

Graphics object consisting of 1 graphics primitive 

sage: plt.show() 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: dm = get_display_manager() 

sage: dm.display_immediately(plt) # indirect doctest 

""" 

self.validate(rich_output) 

types_to_print = [OutputPlainText, OutputAsciiArt, OutputUnicodeArt, OutputHtml] 

if isinstance(rich_output, OutputLatex): 

print(rich_output.mathjax(display=False)) 

elif any(isinstance(rich_output, cls) for cls in types_to_print): 

rich_output.print_to_stdout() 

 

def validate(self, rich_output): 

""" 

Perform checks on ``rich_output`` 

 

INPUT: 

 

- ``rich_output`` -- instance of a subclass of 

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

 

OUTPUT: 

 

An assertion is triggered if ``rich_output`` is invalid. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: dm = get_display_manager() 

sage: invalid = dm.types.OutputImagePng('invalid') 

sage: backend = dm._backend; backend 

doctest 

sage: backend.validate(invalid) 

Traceback (most recent call last): 

... 

AssertionError 

sage: backend.validate(dm.types.OutputPlainText.example()) 

sage: backend.validate(dm.types.OutputAsciiArt.example()) 

sage: backend.validate(dm.types.OutputLatex.example()) 

sage: backend.validate(dm.types.OutputImagePng.example()) 

sage: backend.validate(dm.types.OutputImageGif.example()) 

sage: backend.validate(dm.types.OutputImageJpg.example()) 

sage: backend.validate(dm.types.OutputImageSvg.example()) 

sage: backend.validate(dm.types.OutputImagePdf.example()) 

sage: backend.validate(dm.types.OutputImageDvi.example()) 

sage: backend.validate(dm.types.OutputSceneJmol.example()) 

sage: backend.validate(dm.types.OutputSceneWavefront.example()) 

sage: backend.validate(dm.types.OutputSceneCanvas3d.example()) 

sage: backend.validate(dm.types.OutputVideoOgg.example()) 

sage: backend.validate(dm.types.OutputVideoWebM.example()) 

sage: backend.validate(dm.types.OutputVideoMp4.example()) 

sage: backend.validate(dm.types.OutputVideoFlash.example()) 

sage: backend.validate(dm.types.OutputVideoMatroska.example()) 

sage: backend.validate(dm.types.OutputVideoAvi.example()) 

sage: backend.validate(dm.types.OutputVideoWmv.example()) 

sage: backend.validate(dm.types.OutputVideoQuicktime.example()) 

""" 

if isinstance(rich_output, OutputPlainText): 

pass 

elif isinstance(rich_output, OutputAsciiArt): 

pass 

elif isinstance(rich_output, OutputUnicodeArt): 

pass 

elif isinstance(rich_output, OutputLatex): 

assert rich_output.mathjax().startswith('<html>') 

elif isinstance(rich_output, OutputImagePng): 

assert rich_output.png.get().startswith(b'\x89PNG') 

elif isinstance(rich_output, OutputImageGif): 

assert rich_output.gif.get().startswith(b'GIF89a') 

elif isinstance(rich_output, OutputImageJpg): 

assert rich_output.jpg.get().startswith(b'\xff\xd8\xff\xe0\x00\x10JFIF') 

elif isinstance(rich_output, OutputImageSvg): 

assert b'</svg>' in rich_output.svg.get() 

elif isinstance(rich_output, OutputImagePdf): 

assert rich_output.pdf.get().startswith(b'%PDF-') 

elif isinstance(rich_output, OutputImageDvi): 

assert b'TeX output' in rich_output.dvi.get() 

elif isinstance(rich_output, OutputSceneJmol): 

assert rich_output.preview_png.get().startswith(b'\x89PNG') 

assert rich_output.scene_zip.get().startswith(b'PK') # zip archive 

elif isinstance(rich_output, OutputSceneWavefront): 

assert rich_output.obj.get().startswith(b'mtllib ') 

assert rich_output.mtl.get().startswith(b'newmtl ') 

elif isinstance(rich_output, OutputSceneCanvas3d): 

assert rich_output.canvas3d.get().startswith(b'[{"vertices":') 

elif isinstance(rich_output, OutputVideoOgg): 

assert rich_output.video.get().startswith(b'OggS') 

elif isinstance(rich_output, OutputVideoWebM): 

data = rich_output.video.get() 

assert data.startswith(b'\x1a\x45\xdf\xa3') 

assert b'\x42\x82\x84webm' in data 

elif isinstance(rich_output, OutputVideoMp4): 

data = rich_output.video.get() 

assert data[4:8] == b'ftyp' 

assert data.startswith(b'\0\0\0') 

# See http://www.ftyps.com/ 

if six.PY2: 

ftyps = [data[i:i+4] for i in range(8, ord(data[3]), 4)] 

else: 

ftyps = [data[i:i+4] for i in range(8, data[3], 4)] 

del ftyps[1] # version number, not an ftyp 

expected = [b'avc1', b'iso2', b'mp41', b'mp42'] 

assert any(i in ftyps for i in expected) 

elif isinstance(rich_output, OutputVideoFlash): 

assert rich_output.video.get().startswith(b'FLV\x01') 

elif isinstance(rich_output, OutputVideoMatroska): 

data = rich_output.video.get() 

assert data.startswith(b'\x1a\x45\xdf\xa3') 

assert b'\x42\x82\x88matroska' in data 

elif isinstance(rich_output, OutputVideoAvi): 

data = rich_output.video.get() 

assert data[:4] == b'RIFF' and data[8:12] == b'AVI ' 

elif isinstance(rich_output, OutputVideoWmv): 

assert rich_output.video.get().startswith(b'\x30\x26\xb2\x75') 

elif isinstance(rich_output, OutputVideoQuicktime): 

data = rich_output.video.get() 

assert data[4:12] == b'ftypqt ' or data[4:8] == b'moov' 

else: 

raise TypeError('rich_output type not supported')