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() """ for filename, mtime in modified_file_iterator(): basename = os.path.basename(filename) timestr = time.strftime('%T', mtime) from sage.libs.readline import interleaved_output with interleaved_output(): print('### reloading attached file {0} modified at {1} ###'.format( basename, timestr)) code = load_wrap(filename, attach=True) get_ipython().run_cell(code)
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() """ for filename, mtime in modified_file_iterator(): basename = os.path.basename(filename) timestr = time.strftime('%T', mtime) from sage.libs.readline import interleaved_output with interleaved_output(): print('### reloading attached file {0} modified at {1} ###'.format(basename, timestr)) code = load_wrap(filename, attach=True) get_ipython().run_cell(code)