checking external save
@@ -39,6 +39,8 @@ class Editor(ttk.Frame):
self.rowconfigure(0, weight=1)
self._filename = None
+ self._last_save_mtime = None
+ self._last_save_hash = None
if filename is not None:
self._load_file(filename)
@@ -79,6 +81,25 @@ class Editor(ttk.Frame):
return result
+ def check_for_external_changes(self):
+ if self._filename is None:
+ return
+
+ assert self._last_save_mtime is not None
+
+ if os.path.getmtime(self._filename) == self._last_save_mtime:
+ return
+
+ assert self._last_save_hash is not None
+ with open(self._filename, "b") as fp:
+ current_hash_on_disk = hash(fp.read())
+
+ if self._last_save_hash == current_hash_on_disk:
+ # just timestamp has changed (or file attributes?)
+ return
+
+
+
def get_long_description(self):
if self._filename is None:
@@ -145,6 +166,8 @@ class Editor(ttk.Frame):
f.flush()
os.fsync(f) # Force writes on disk, see https://learn.adafruit.com/adafruit-circuit-playground-express/creating-and-editing-code#1-use-an-editor-that-writes-out-the-file-completely-when-you-save-it
f.close()
+ self._last_save_hash = hash(content)
+ self._last_save_mtime = os.path.getmtime(filename)
except PermissionError:
if askyesno("Permission Error",
"Looks like this file or folder is not writable.\n\n"
