[issue28581] Allow running code without explicitly saving the file.
New submission from perilbrain: Current Flow:- - If you create a new file in idle and try to run it, the editor asks to save the file. However if user presses cancel button the code is not executed. Problem:- I have been using idle for over 5 years and this behavior is quite annoying(along with paste :) !!). We are often required to copy code from various sites say some tutorial or code samples which are good for one time usage. Solution:- If a user presses cancel button then set a flag in IOBinding say "optedTemp", save it to a named temporary file, execute the code and close the file. If user again runs the code, check for this flag. If it is true then there is no need to ask for saving the code and again create the temporary file, execute, close. If some one needs to save this code they can use the "save as" menu which sets off the optedTemp flag. Here is the code I propose (check for "#+") ==idlelib/ScriptBinding.py=== # At top import tempfile #+ # New definition of functions:- def _run_module_event(self, event): filename = self.getfilename() tempCode = None #+ if not filename: tempCode = tempfile.NamedTemporaryFile(suffix = ".py") #+ filename = tempCode.name #Added*** self.editwin.io.writefile( filename ) #+ self.editwin.io.optedTemp = True #+ #return 'break' code = self.checksyntax(filename) .. interp.runcode(code) if tempCode is not None: #+ tempCode.close() #+ return 'break' def getfilename(self): filename = self.editwin.io.filename if not self.editwin.get_saved(): autosave = idleConf.GetOption('main', 'General', 'autosave', type='bool') if autosave and filename: self.editwin.io.save(None) elif self.editwin.io.optedTemp: #+ filename = None #+ else: confirm = self.ask_save_dialog() self.editwin.text.focus_set() if confirm: self.editwin.io.save(None) filename = self.editwin.io.filename else: filename = None return filename idlelib/IOBinding.py== def __init__(self, editwin): # self.__id_print = self.text.bind("<>", self.print_window) self.optedTemp = False #+ def save_as(self, event): filename = self.asksavefile() if filename: if self.writefile(filename): self.set_filename(filename) self.set_saved(1) self.optedTemp = False #+ try: self.editwin.store_file_breaks() except AttributeError: -- assignee: terry.reedy components: IDLE messages: 279885 nosy: perilbrain, terry.reedy priority: normal severity: normal status: open title: Allow running code without explicitly saving the file. type: enhancement versions: Python 3.4 ___ Python tracker <http://bugs.python.org/issue28581> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue28581] Allow running code without explicitly saving the file.
perilbrain added the comment: Thanks Terry, I tried searching for a similar issue but failed. Feels like signing contributor form will take a while. Meantime please feel free to tweak/improve and implement code in your way (Mine is a bit rough solution not even abiding by naming convention), if this is some licensing issue. -- ___ Python tracker <http://bugs.python.org/issue28581> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19042] Idle: add option to autosave 'Untitled' edit window
perilbrain added the comment: "To run without saving" was the first idea I got, but it was difficult to pursue in first phase. After your hints I think I have achieved what you have described. I have done a few fixes(luckily in a single file ScriptBinding.py) which are giving satisfactory result. I am attaching the original, modified and the diff version of files named ScriptBindingOriginal.py, ScriptBinding.py and ScriptBinding.diff respectively. Here is what I am getting with the new code Traceback (most recent call last): File "*Untitled*", line 3, in File "*Untitled*", line 2, in f ZeroDivisionError: division by zero This is the summary of the patch (idle-python3.4) +import io #== One member in class for reducing annoyance self.no_save = False #== New definition for function tabnanny== -def tabnanny(self, filename): +def tabnanny(self, source): +f = io.StringIO(source)# , os.linesep *Maybe* -with tokenize.open(filename) as f: #== Added 2 functions = def source_from_file(self, filename): with open(filename, 'rb') as f: source = f.read() if b'\r' in source: source = source.replace(b'\r\n', b'\n') source = source.replace(b'\r', b'\n') if source and source[-1] != ord(b'\n'): source = source + b'\n' return source def source_from_editor(self): self.editwin.io.fixlastline() source = self.editwin.text.get("1.0", "end-1c") return source #== New definition for function checksyntax== -def checksyntax(self, filename): +def checksyntax(self, source, filename): #== Changes in function run_module_event (Main) == def _run_module_event(self, event): filename = self.getfilename() filename = self.getfilename() if not filename: self.no_save = True source = self.source_from_editor() filename = self.editwin.top.wm_title() else: source = self.source_from_file(filename) self.no_save = False code = self.checksyntax(source, filename) if not code: return 'break' if not self.tabnanny(source): return 'break' interp = self.shell.interp if PyShell.use_subprocess: interp.restart_subprocess(with_cwd=False) if not self.no_save: interp.runcode(code) return 'break' #== Finally suppressing the annoyance == if autosave and filename: self.editwin.io.save(None) elif self.no_save: filename = None else: confirm = self.ask_save_dialog() Please have a review and let me know if it can solve this issue. -- nosy: +perilbrain Added file: http://bugs.python.org/file45309/ScriptBinding.diff.zip ___ Python tracker <http://bugs.python.org/issue19042> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19042] Idle: add option to autosave 'Untitled' edit window
perilbrain added the comment: I have signed the CA. Meantime a came across one more problem in function tabnanny(encoding issue) so I am attaching the new version of ScriptBinding.py. Summary:- def tabnanny(self, source, encoding = None): # XXX: tabnanny should work on binary files as well #print(encoding) f = io.StringIO(source.decode(encoding) if encoding else source) def source_from_file(self, filename): with open(filename, 'rb') as f: source = f.read() encoding = tokenize.detect_encoding(f.readline)[0] return source, encoding def _run_module_event(self, event): ... if not filename: self.no_save = True source = self.source_from_editor() filename = self.editwin.top.wm_title() else: source, encoding = self.source_from_file(filename) self.no_save = False code = self.checksyntax(source, filename) if not code: return 'break' if not self.tabnanny(source, encoding): return 'break' -- Added file: http://bugs.python.org/file45328/ScriptBinding.py ___ Python tracker <http://bugs.python.org/issue19042> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com