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

# -*- coding: utf-8 -*- 

r""" 

SageMath version and banner info 

""" 

 

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

# Copyright (C) 2005 William Stein <wstein@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 print_function 

 

from sage.env import SAGE_VERSION, SAGE_DATE, SAGE_SRC, SAGE_BANNER 

 

def version(): 

""" 

Return the version of Sage. 

 

OUTPUT: 

 

str 

 

EXAMPLES:: 

 

sage: version() 

'SageMath version ..., Release Date: ...' 

""" 

return 'SageMath version %s, Release Date: %s' % (SAGE_VERSION, SAGE_DATE) 

 

 

def banner_text(full=True): 

""" 

Text for the Sage banner. 

 

INPUT: 

 

- ``full`` -- boolean (optional, default=True) 

 

OUTPUT: 

 

A string containing the banner message. 

 

If option full is False, a simplified plain ASCII banner is displayed; if 

True the full banner with box art is displayed. 

 

EXAMPLES:: 

 

sage: print(sage.misc.banner.banner_text(full=True)) 

┌────────────────────────────────────────────────────────────────────┐ 

│ SageMath version ... 

sage: print(sage.misc.banner.banner_text(full=False)) 

SageMath version ..., Release Date: ... 

""" 

if not full: 

return version() 

 

bars = u"─"*68 

s = [] 

a = s.append 

a(u'┌' + bars + u'┐') 

a(u"\n│ %-66s │\n" % version()) 

a(u"│ %-66s │\n" % 'Type "notebook()" for the browser-based notebook interface.') 

a(u"│ %-66s │\n" % 'Type "help()" for help.') 

#s += u"│ %-66s │\n" % 'Distributed under the GNU General Public License V2.' 

a(u'└' + bars + u'┘') 

pre = version_dict()['prerelease'] 

if pre: 

red_in = '\033[31m' 

red_out = '\033[0m' 

bars2 = bars.replace(u'─', u'━') 

a('\n') 

a(red_in + u'┏' + bars2 + u'┓' + '\n') 

a(u"┃ %-66s ┃\n" % 'Warning: this is a prerelease version, and it may be unstable.') 

a(u'┗' + bars2 + u'┛' + red_out) 

return u''.join(s) 

 

 

def banner(): 

""" 

Print the Sage banner. 

 

OUTPUT: None 

 

If the environment variable ``SAGE_BANNER`` is set to ``no``, no 

banner is displayed. If ``SAGE_BANNER`` is set to ``bare``, a 

simplified plain ASCII banner is displayed. Otherwise, the full 

banner with box art is displayed. 

 

EXAMPLES:: 

 

sage: import sage.misc.banner; sage.misc.banner.SAGE_BANNER = '' 

sage: banner() 

┌────────────────────────────────────────────────────────────────────┐ 

│ SageMath version ..., Release Date: ... │ 

│ Type "notebook()" for the browser-based notebook interface. │ 

│ Type "help()" for help. │ 

... 

""" 

typ = SAGE_BANNER.lower() 

 

if typ == "no": 

return 

 

if typ != "bare": 

try: 

print(banner_text(full=True)) 

return 

except UnicodeEncodeError: 

pass 

 

print(banner_text(full=False)) 

 

 

def version_dict(): 

""" 

A dictionary describing the version of Sage. 

 

INPUT: 

 

nothing 

 

OUTPUT: 

 

dictionary with keys 'major', 'minor', 'tiny', 'prerelease' 

 

This process the Sage version string and produces a dictionary. 

It expects the Sage version to be in one of these forms:: 

 

N.N 

N.N.N 

N.N.N.N 

N.N.str 

N.N.N.str 

N.N.N.N.str 

 

where 'N' stands for an integer and 'str' stands for a string. 

The first integer is stored under the 'major' key and the second 

integer under 'minor'. If there is one more integer, it is stored 

under 'tiny'; if there are two more integers, then they are stored 

together as a float N.N under 'tiny'. If there is a string, then 

the key 'prerelease' returns True. 

 

For example, if the Sage version is '3.2.1', then the dictionary 

is {'major': 3, 'minor': 2, 'tiny': 1, 'prerelease': False}. 

If the Sage version is '3.2.1.2', then the dictionary is 

{'major': 3, 'minor': 2, 'tiny': 1.200..., 'prerelease': False}. 

If the Sage version is '3.2.alpha0', then the dictionary is 

{'major': 3, 'minor': 2, 'tiny': 0, 'prerelease': True}. 

 

EXAMPLES:: 

 

sage: from sage.misc.banner import version_dict 

sage: print("SageMath major version is %s" % version_dict()['major']) 

SageMath major version is ... 

sage: version_dict()['major'] == int(sage.version.version.split('.')[0]) 

True 

""" 

v = SAGE_VERSION.split('.') 

dict = {} 

dict['major'] = int(v[0]) 

dict['minor'] = int(v[1]) 

dict['tiny'] = 0 

dict['prerelease'] = False 

try: 

dummy = int(v[-1]) 

except ValueError: # when last entry is not an integer 

dict['prerelease'] = True 

if (len(v) == 3 and not dict['prerelease']) or len(v) > 3: 

dict['tiny'] = int(v[2]) 

try: 

teeny = int(v[3]) 

dict['tiny'] += 0.1 * teeny 

except (ValueError, IndexError): 

pass 

return dict 

 

def require_version(major, minor=0, tiny=0, prerelease=False, 

print_message=False): 

""" 

True if Sage version is at least major.minor.tiny. 

 

INPUT: 

 

- major -- integer 

- minor -- integer (optional, default = 0) 

- tiny -- float (optional, default = 0) 

- prerelease -- boolean (optional, default = False) 

- print_message -- boolean (optional, default = False) 

 

OUTPUT: 

 

True if major.minor.tiny is <= version of Sage, False otherwise 

 

For example, if the Sage version number is 3.1.2, then 

require_version(3, 1, 3) will return False, while 

require_version(3, 1, 2) will return True. 

If the Sage version is 3.1.2.alpha0, then 

require_version(3, 1, 1) will return True, while, by default, 

require_version(3, 1, 2) will return False. Note, though, that 

require_version(3, 1, 2, prerelease=True) will return True: 

if the optional argument prerelease is True, then a prerelease 

version of Sage counts as if it were the released version. 

 

If optional argument print_message is True and this function 

is returning False, print a warning message. 

 

EXAMPLES:: 

 

sage: from sage.misc.banner import require_version 

sage: require_version(2, 1, 3) 

True 

sage: require_version(821, 4) 

False 

sage: require_version(821, 4, print_message=True) 

This code requires at least version 821.4 of SageMath to run correctly. 

You are running version ... 

False 

""" 

vers = version_dict() 

prerelease_checked = (prerelease if vers['prerelease'] else True) 

if (vers['major'] > major 

or (vers['major'] == major and vers['minor'] > minor) 

or (vers['major'] == major and vers['minor'] == minor 

and vers['tiny'] > tiny) 

or (vers['major'] == major and vers['minor'] == minor 

and vers['tiny'] == tiny and prerelease_checked)): 

return True 

else: 

if print_message: 

print("This code requires at least version {} of SageMath to run correctly.". 

format(major + 0.1 * minor + 0.01 * tiny)) 

print("You are running version {}.".format(SAGE_VERSION)) 

return False