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

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

r""" 

Keep track of attached files 

 

TESTS:: 

 

sage: attach('http://wstein.org/loadtest.py') 

Traceback (most recent call last): 

... 

NotImplementedError: you can't attach a URL 

 

Check that no file clutter is produced:: 

 

sage: dir = tmp_dir() 

sage: src = os.path.join(dir, 'foobar.sage') 

sage: with open(src, 'w') as f: 

....: _ = f.write('print("<output from attached file>")\n') 

sage: attach(src) 

<output from attached file> 

sage: os.listdir(dir) 

['foobar.sage'] 

sage: detach(src) 

 

In debug mode backtraces contain code snippets. We need to manually 

print the traceback because the python doctest module has special 

support for exceptions and does not match them 

character-by-character:: 

 

sage: import traceback 

sage: with open(src, 'w') as f: 

....: _ = f.write('# first line\n') 

....: _ = f.write('# second line\n') 

....: _ = f.write('raise ValueError("third") # this should appear in the source snippet\n') 

....: _ = f.write('# fourth line\n') 

 

sage: load_attach_mode(attach_debug=False) 

sage: try: 

....: attach(src) 

....: except Exception: 

....: traceback.print_exc() 

Traceback (most recent call last): 

... 

exec(preparse_file(f.read()) + "\n", globals) 

File "<string>", line 3, in <module> 

ValueError: third 

sage: detach(src) 

 

sage: load_attach_mode(attach_debug=True) 

sage: try: 

....: attach(src) 

....: except Exception: 

....: traceback.print_exc() 

Traceback (most recent call last): 

... 

exec(code, globals) 

File ".../foobar.sage....py", line ..., in <module> 

raise ValueError("third") # this should appear in the source snippet 

ValueError: third 

sage: detach(src) 

""" 

 

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

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

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License 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 

 

import os 

import six 

import time 

from IPython import get_ipython 

 

from sage.repl.load import load, load_wrap 

import sage.repl.inputhook 

import sage.env 

 

# The attached files as a dict of {filename:mtime} 

attached = {} 

 

 

load_debug_mode = False 

attach_debug_mode = True 

 

def load_attach_mode(load_debug=None, attach_debug=None): 

""" 

Get or modify the current debug mode for the behavior of 

:func:`load` and :func:`attach` on ``.sage`` files. 

 

In debug mode, loaded or attached ``.sage`` files are preparsed 

through a file to make their tracebacks more informative. If not 

in debug mode, then ``.sage`` files are preparsed in memory only 

for performance. 

 

At startup, debug mode is ``True`` for attaching and ``False`` 

for loading. 

 

.. NOTE:: 

 

This function should really be deprecated and code executed 

from memory should raise proper tracebacks. 

 

INPUT: 

 

- ``load_debug`` -- boolean or ``None`` (default); if not 

``None``, then set a new value for the debug mode for loading 

files. 

 

- ``attach_debug`` -- boolean or ``None`` (default); same as 

``load_debug``, but for attaching files. 

 

OUTPUT: 

 

If all input values are ``None``, returns a tuple giving the 

current modes for loading and attaching. 

 

EXAMPLES:: 

 

sage: load_attach_mode() 

(False, True) 

sage: load_attach_mode(attach_debug=False) 

sage: load_attach_mode() 

(False, False) 

sage: load_attach_mode(load_debug=True) 

sage: load_attach_mode() 

(True, False) 

sage: load_attach_mode(load_debug=False, attach_debug=True) 

""" 

global load_debug_mode, attach_debug_mode 

if load_debug is None and attach_debug is None: 

return (load_debug_mode, attach_debug_mode) 

if not load_debug is None: 

load_debug_mode = load_debug 

if not attach_debug is None: 

attach_debug_mode = attach_debug 

 

 

search_paths = [] 

 

def load_attach_path(path=None, replace=False): 

""" 

Get or modify the current search path for :func:`load` and 

:func:`attach`. 

 

INPUT: 

 

- ``path`` -- string or list of strings (default: ``None``); 

path(s) to append to or replace the current path. 

 

- ``replace`` -- boolean (default: ``False``); if ``path`` is not 

``None``, whether to *replace* the search path instead of 

*appending* to it. 

 

OUTPUT: 

 

``None`` or a *reference* to the current search paths. 

 

EXAMPLES: 

 

First, we extend the example given in :func:`load`'s docstring:: 

 

sage: sage.repl.attach.reset(); reset_load_attach_path() 

sage: load_attach_path() 

['.'] 

sage: t_dir = tmp_dir() 

sage: fullpath = os.path.join(t_dir, 'test.py') 

sage: _ = open(fullpath, 'w').write("print(37 * 3)") 

sage: attach('test.py') 

Traceback (most recent call last): 

... 

IOError: did not find file 'test.py' to load or attach 

sage: load_attach_path(t_dir) 

sage: attach('test.py') 

111 

sage: attached_files() == [fullpath] 

True 

sage: sage.repl.attach.reset(); reset_load_attach_path() 

sage: load_attach_path() == ['.'] 

True 

sage: load('test.py') 

Traceback (most recent call last): 

... 

IOError: did not find file 'test.py' to load or attach 

 

The function returns a reference to the path list:: 

 

sage: reset_load_attach_path(); load_attach_path() 

['.'] 

sage: load_attach_path('/path/to/my/sage/scripts'); load_attach_path() 

['.', '/path/to/my/sage/scripts'] 

sage: load_attach_path(['good', 'bad', 'ugly'], replace=True) 

sage: load_attach_path() 

['good', 'bad', 'ugly'] 

sage: p = load_attach_path(); p.pop() 

'ugly' 

sage: p[0] = 'weird'; load_attach_path() 

['weird', 'bad'] 

sage: reset_load_attach_path(); load_attach_path() 

['.'] 

""" 

global search_paths 

if path is None: 

return search_paths 

else: 

if isinstance(path, six.string_types): 

path = [path] 

if replace: 

search_paths = path 

else: 

for p in path: 

if not p: 

continue 

if p not in search_paths: 

search_paths.append(p) 

 

 

def reset_load_attach_path(): 

""" 

Resets the current search path for :func:`load` and 

:func:`attach`. 

 

The default path is ``'.'`` plus any paths specified in the 

environment variable ``SAGE_LOAD_ATTACH_PATH``. 

 

EXAMPLES:: 

 

sage: load_attach_path() 

['.'] 

sage: t_dir = tmp_dir() 

sage: load_attach_path(t_dir) 

sage: t_dir in load_attach_path() 

True 

sage: reset_load_attach_path(); load_attach_path() 

['.'] 

 

At startup, Sage adds colon-separated paths in the environment 

variable ``SAGE_LOAD_ATTACH_PATH``:: 

 

sage: reset_load_attach_path(); load_attach_path() 

['.'] 

sage: os.environ['SAGE_LOAD_ATTACH_PATH'] = '/veni/vidi:vici:' 

sage: import imp 

sage: imp.reload(sage.repl.attach) # Simulate startup 

<module 'sage.repl.attach' from '...'> 

sage: load_attach_path() 

['.', '/veni/vidi', 'vici'] 

sage: del os.environ['SAGE_LOAD_ATTACH_PATH'] 

sage: imp.reload(sage.repl.preparse) # Simulate startup 

<module 'sage.repl.preparse' from '...'> 

sage: reset_load_attach_path(); load_attach_path() 

['.'] 

""" 

global search_paths 

search_paths = ['.'] 

for path in os.environ.get('SAGE_LOAD_ATTACH_PATH', '').split(':'): 

load_attach_path(path=path) 

 

# Set up the initial search path for loading and attaching files. A 

# user can modify the path with the function load_attach_path. 

reset_load_attach_path() 

 

 

def attach(*files): 

""" 

Attach a file or files to a running instance of Sage and also load 

that file. 

 

.. NOTE:: 

 

Attaching files uses the Python inputhook, which will conflict 

with other inputhook users. This generally includes GUI main loop 

integrations, for example tkinter. So you can only use tkinter or 

attach, but not both at the same time. 

 

INPUT: 

 

- ``files`` -- a list of filenames (strings) to attach. 

 

OUTPUT: 

 

Each file is read in and added to an internal list of watched files. 

The meaning of reading in a file depends on the file type: 

 

- ``.py`` files are read in with no preparsing (so, e.g., ``2^3`` is 2 

bit-xor 3); 

 

- ``.sage`` files are preparsed, then the result is read in; 

 

- ``.pyx`` files are *not* preparsed, but rather are compiled to a 

module ``m`` and then ``from m import *`` is executed. 

 

The contents of the file are then loaded, which means they are read 

into the running Sage session. For example, if ``foo.sage`` contains 

``x=5``, after attaching ``foo.sage`` the variable ``x`` will be set 

to 5. Moreover, any time you change ``foo.sage``, before you execute 

a command, the attached file will be re-read automatically (with no 

intervention on your part). 

 

.. SEEALSO:: 

 

:meth:`~sage.repl.load.load` is the same as :func:`attach`, but 

doesn't automatically reload a file when it changes. 

 

EXAMPLES: 

 

You attach a file, e.g., ``foo.sage`` or ``foo.py`` or 

``foo.pyx``, to a running Sage session by typing:: 

 

sage: attach('foo.sage') # not tested 

 

Here we test attaching multiple files at once:: 

 

sage: sage.repl.attach.reset() 

sage: t1 = tmp_filename(ext='.py') 

sage: _ = open(t1,'w').write("print('hello world')") 

sage: t2 = tmp_filename(ext='.py') 

sage: _ = open(t2,'w').write("print('hi there xxx')") 

sage: attach(t1, t2) 

hello world 

hi there xxx 

sage: set(attached_files()) == set([t1,t2]) 

True 

 

.. SEEALSO:: 

 

- :meth:`attached_files` returns a list of 

all currently attached files. 

 

- :meth:`detach` instructs Sage to remove a 

file from the internal list of watched files. 

 

- :meth:`load_attach_path` allows you to 

get or modify the current search path for loading and attaching 

files. 

""" 

try: 

ipy = get_ipython() 

except NameError: 

ipy = None 

global attached 

for filename in files: 

if ipy: 

code = load_wrap(filename, attach=True) 

ipy.run_cell(code) 

else: 

load(filename, globals(), attach=True) 

 

 

def add_attached_file(filename): 

""" 

Add to the list of attached files 

 

This is a callback to be used from 

:func:`~sage.repl.load.load` after evaluating the attached 

file the first time. 

 

INPUT: 

 

- ``filename`` -- string, the fully qualified file name. 

 

EXAMPLES:: 

 

sage: import sage.repl.attach as af 

sage: af.reset() 

sage: t = tmp_filename(ext='.py') 

sage: af.add_attached_file(t) 

sage: af.attached_files() 

['/.../tmp_....py'] 

sage: af.detach(t) 

sage: af.attached_files() 

[] 

""" 

sage.repl.inputhook.install() 

fpath = os.path.abspath(filename) 

attached[fpath] = os.path.getmtime(fpath) 

 

 

def attached_files(): 

""" 

Returns a list of all files attached to the current session with 

:meth:`attach`. 

 

OUTPUT: 

 

The filenames in a sorted list of strings. 

 

EXAMPLES:: 

 

sage: sage.repl.attach.reset() 

sage: t = tmp_filename(ext='.py') 

sage: _ = open(t,'w').write("print('hello world')") 

sage: attach(t) 

hello world 

sage: attached_files() 

['/....py'] 

sage: attached_files() == [t] 

True 

""" 

global attached 

return sorted(attached) 

 

 

def detach(filename): 

""" 

Detach a file. 

 

This is the counterpart to :meth:`attach`. 

 

INPUT: 

 

- ``filename`` -- a string, or a list of strings, or a tuple of strings. 

 

EXAMPLES:: 

 

sage: sage.repl.attach.reset() 

sage: t = tmp_filename(ext='.py') 

sage: _ = open(t,'w').write("print('hello world')") 

sage: attach(t) 

hello world 

sage: attached_files() == [t] 

True 

sage: detach(t) 

sage: attached_files() 

[] 

 

sage: sage.repl.attach.reset(); reset_load_attach_path() 

sage: load_attach_path() 

['.'] 

sage: t_dir = tmp_dir() 

sage: fullpath = os.path.join(t_dir, 'test.py') 

sage: _ = open(fullpath, 'w').write("print(37 * 3)") 

sage: load_attach_path(t_dir) 

sage: attach('test.py') 

111 

sage: attached_files() == [os.path.normpath(fullpath)] 

True 

sage: detach('test.py') 

sage: attached_files() 

[] 

sage: attach('test.py') 

111 

sage: fullpath = os.path.join(t_dir, 'test2.py') 

sage: _ = open(fullpath, 'w').write("print(3)") 

sage: attach('test2.py') 

3 

sage: detach(attached_files()) 

sage: attached_files() 

[] 

 

TESTS:: 

 

sage: detach('/dev/null/foobar.sage') 

Traceback (most recent call last): 

... 

ValueError: file '/dev/null/foobar.sage' is not attached, see attached_files() 

""" 

if isinstance(filename, six.string_types): 

filelist = [filename] 

else: 

filelist = [str(x) for x in filename] 

 

global attached 

for filename in filelist: 

fpath = os.path.expanduser(filename) 

if not os.path.isabs(fpath): 

for path in load_attach_path(): 

epath = os.path.expanduser(path) 

fpath = os.path.join(epath, filename) 

fpath = os.path.abspath(fpath) 

if fpath in attached: 

break 

if fpath in attached: 

attached.pop(fpath) 

else: 

raise ValueError("file '{0}' is not attached, see attached_files()".format(filename)) 

if not attached: 

sage.repl.inputhook.uninstall() 

 

def reset(): 

""" 

Remove all the attached files from the list of attached files. 

 

EXAMPLES:: 

 

sage: sage.repl.attach.reset() 

sage: t = tmp_filename(ext='.py') 

sage: _ = open(t,'w').write("print('hello world')") 

sage: attach(t) 

hello world 

sage: attached_files() == [t] 

True 

sage: sage.repl.attach.reset() 

sage: attached_files() 

[] 

""" 

global attached 

attached = {} 

 

 

def modified_file_iterator(): 

""" 

Iterate over the changed files 

 

As a side effect the stored time stamps are updated with the 

actual time stamps. So if you iterate over the attached files in 

order to reload them and you hit an error then the subsequent 

files are not marked as read. 

 

Files that are in the process of being saved are excluded. 

 

EXAMPLES:: 

 

sage: sage.repl.attach.reset() 

sage: t = tmp_filename(ext='.py') 

sage: attach(t) 

sage: from sage.repl.attach import modified_file_iterator 

sage: list(modified_file_iterator()) 

[] 

sage: sleep(1) # filesystem mtime granularity 

sage: _ = open(t, 'w').write('1') 

sage: list(modified_file_iterator()) 

[('/.../tmp_....py', time.struct_time(...))] 

""" 

global attached 

modified = dict() 

for filename in attached.keys(): 

old_tm = attached[filename] 

if not os.path.exists(filename): 

print('### detaching file {0} because it does not exist (deleted?) ###'.format(filename)) 

detach(filename) 

continue 

new_tm = os.path.getmtime(filename) 

if new_tm > old_tm: 

modified[filename] = new_tm 

 

if not modified: 

return 

time.sleep(0.1) # sleep 100ms to give the editor time to finish saving 

 

for filename in modified.keys(): 

old_tm = modified[filename] 

new_tm = os.path.getmtime(filename) 

if new_tm == old_tm: 

# file was modified but did not change in the last 100ms 

attached[filename] = new_tm 

yield filename, time.gmtime(new_tm) 

 

 

def reload_attached_files_if_modified(): 

r""" 

Reload attached files that have been modified 

 

This is the internal implementation of the attach mechanism. 

 

EXAMPLES:: 

 

sage: sage.repl.attach.reset() 

sage: from sage.repl.interpreter import get_test_shell 

sage: shell = get_test_shell() 

sage: tmp = tmp_filename(ext='.py') 

sage: _ = open(tmp, 'w').write('a = 2\n') 

sage: shell.run_cell('attach({0})'.format(repr(tmp))) 

sage: shell.run_cell('a') 

2 

sage: sleep(1) # filesystem mtime granularity 

sage: _ = open(tmp, 'w').write('a = 3\n') 

 

Note that the doctests are never really at the command prompt 

where the automatic reload is triggered. So we have to do it 

manually:: 

 

sage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified') 

sage: shell.run_cell('reload_attached_files_if_modified()') 

### reloading attached file tmp_....py modified at ... ### 

 

sage: shell.run_cell('a') 

3 

sage: shell.run_cell('detach({0})'.format(repr(tmp))) 

sage: shell.run_cell('attached_files()') 

[] 

sage: shell.quit() 

""" 

ip = get_ipython() 

for filename, mtime in modified_file_iterator(): 

basename = os.path.basename(filename) 

timestr = time.strftime('%T', mtime) 

notice = '### reloading attached file {0} modified at {1} ###'.format(basename, timestr) 

if ip and ip.pt_cli: 

with ip.pt_cli.patch_stdout_context(raw=True): 

print(notice) 

code = load_wrap(filename, attach=True) 

ip.run_cell(code) 

elif ip: 

print(notice) 

code = load_wrap(filename, attach=True) 

ip.run_cell(code) 

else: 

print(notice) 

load(filename, globals(), attach=True)