Named option: backup=y
I have added a few lines to rename the original file before saving by appending a '~' (tilde) in put_file
was:
os.remove(fname)
now:
try:
os.remove(fname+'~')
except:
pass
try:
os.rename(fname,fname+'~')
except:
pass
Your thoughts? Is this the best way to discuss this with you? Are you still interested? Feel free to email me.
So you suggest an option to keep a backup copy of a file. That can be done. The code lines you suggest would be some of the code. Handling of the option would have to be done in __init__() and the respective front ends. So the code in put_file() would then be:
if self.file_backup:
try:
os.remove(fname+'~')
except:
pass
try:
os.rename(fname,fname+'~')
except:
pass
else:
os.remove(fname)
The start of __init__ could look like:
def __init__(self, tab_size, undo_limit, io_device, file_backup):
self.file_backup = file_backup
And the respective section e.g. in pye_gen.py would be:
def pye(*args, tab_size=4, undo=50, file_backup=True):
io_device = IO_DEVICE()
ret = pye_edit(args, tab_size=tab_size, undo=undo, io_device=io_device, file_backup=file_backup)
io_device.deinit_tty()
return ret
Exactly. Will you add it? Shall I create a pull request? Should I just keep its to myself?
Please make a PR. What I'm skeptic about is, that then a short time three copies of the file might exist. Another approach would be:
- remove file~ if it exists.
- rename file to file~, if file exists
- save to file.
- if backup is false, remove file~, if it exists.
Then at most two copies of the file will exist. If saving fails, there is at least file~.
If saving fails:
- remove partial(?) file
- rename file~ to file
- error message announcing failure and display remaining free space (most probable reason for failure)
- expect user to acknowledge.
Maybe(:?) in addition, listdir and ask user if they which file they want to remove and try save again or just discard current changes (require "discard" response
Remove the partial file and rename the file is fine, as well as an error message. An error message can be displayed by assigning text to self.message (see fct get_file()) a text with prompt can be done with self.line_edit(). The free space can be told with:
st = os.statvfs(".")
free_mem = st[1] * st[3] // 1024
But it may not work on every MicroPython variant, Everything else like showing a list of files and allowing to delete one it beyond the "simple" approach. One could think about a separate "delete file" command. But all Ctrl keys are exhausted. File windows can already be displayed by opening a directory, like ".". The current unsaved file is still in the edit window. Leaving that without saving gets you already a prompt, whether you want to save or quit. Anyhow, put_file() needs then a return code, telling if it went well or failed. In case of a fail, the hash value and changed flag must not be reset. See the code after the line "elif key == KEY_WRITE:"
Note: At the moment a fail during save would cause an exception, which is caught at the outer level of the editor, showing the exception message, which can be pretty obsure, like OsError(19,) in case of an invalid path.
Thanks for the ideas and pointers.