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

r""" 

Sage's IPython Configuration 

 

TESTS: 

 

We check that Sage stdin can be piped in even if stdout is a tty; In that case 

the IPython simple prompt is being used:: 

 

sage: cmd = 'print([sys.stdin.isatty(), sys.stdout.isatty()])' 

sage: import pexpect 

sage: output = pexpect.run( 

....: 'bash -c \'echo "{0}" | sage\''.format(cmd), 

....: ).decode('utf-8', 'surrogateescape') 

sage: 'sage: [False, True]' in output 

True 

""" 

 

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

# Copyright (C) 2016 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 __future__ import absolute_import 

 

import sys 

import copy 

from traitlets.config.loader import Config 

 

from sage.repl.prompts import SagePrompts 

 

 

# Name of the Sage IPython extension 

SAGE_EXTENSION = 'sage' 

 

 

class SageIpythonConfiguration(object): 

 

def _doctest_mode(self): 

""" 

Whether we are in doctest mode 

 

This returns ``True`` during doctests. 

 

EXAMPLES:: 

 

sage: from sage.repl.configuration import sage_ipython_config 

sage: sage_ipython_config._doctest_mode() 

True 

""" 

from sage.doctest import DOCTEST_MODE 

return DOCTEST_MODE 

 

def _allow_ansi(self): 

""" 

Whether to allow ANSI escape sequences 

 

This returns ``False`` during doctests to avoid ANSI escape 

sequences. 

 

EXAMPLES:: 

 

sage: from sage.repl.configuration import sage_ipython_config 

sage: sage_ipython_config._allow_ansi() 

False 

""" 

return (not self._doctest_mode()) and sys.stdin.isatty() and sys.stdout.isatty() 

 

def colors(self): 

""" 

Return the IPython color palette 

 

This returns ``'NoColor'`` during doctests to avoid ANSI escape 

sequences. 

 

EXAMPLES:: 

 

sage: from sage.repl.configuration import sage_ipython_config 

sage: sage_ipython_config.simple_prompt() 

True 

""" 

return 'LightBG' if self._allow_ansi() else 'NoColor' 

 

def simple_prompt(self): 

""" 

Return whether to use the simple prompt 

 

This returns ``True`` during doctests to avoid ANSI escape sequences. 

 

EXAMPLES:: 

 

sage: from sage.repl.configuration import sage_ipython_config 

sage: sage_ipython_config.simple_prompt() 

True 

""" 

return not self._allow_ansi() 

 

def term_title(self): 

""" 

Return whether to set the terminal title 

 

This returns false during doctests to avoid ANSI escape sequences. 

 

EXAMPLES:: 

 

sage: from sage.repl.configuration import sage_ipython_config 

sage: sage_ipython_config.term_title() 

False 

""" 

return self._allow_ansi() 

 

def default(self): 

""" 

Return a new default configuration object 

 

EXAMPLES:: 

 

sage: from sage.repl.configuration import sage_ipython_config 

sage: sage_ipython_config.default() 

{'InteractiveShell': {'colors': ... 

""" 

from sage.repl.interpreter import SageTerminalInteractiveShell 

cfg = Config( 

TerminalIPythonApp=Config( 

display_banner=False, 

verbose_crash=True, 

test_shell=False, 

shell_class=SageTerminalInteractiveShell, 

), 

InteractiveShell=Config( 

prompts_class=SagePrompts, 

ast_node_interactivity='all', 

colors=self.colors(), 

simple_prompt=self.simple_prompt(), 

term_title=self.term_title(), 

confirm_exit=False, 

separate_in='' 

), 

InteractiveShellApp=Config(extensions=[SAGE_EXTENSION]), 

) 

if self._doctest_mode(): 

# Using the file-backed history causes problems in parallel tests 

cfg.HistoryManager = Config(hist_file=':memory:') 

return cfg 

 

def copy(self): 

""" 

Return a copy of the current configuration 

 

EXAMPLES:: 

 

sage: from sage.repl.configuration import sage_ipython_config 

sage: sage_ipython_config.copy() 

{'InteractiveShell': {'colors': ... 

""" 

try: 

return copy.deepcopy(get_ipython().config) 

except NameError: 

return self.default() 

 

 

sage_ipython_config = SageIpythonConfiguration()