def test(f_exists): with make_file() as path: if f_exists: stat = os.stat(path) res = (stat.st_size, stat.st_mtime) else: path += "-not-found" res = None eq_(filestat(path), res)
def is_externally_modified(self): """check if this document has been modified by another program :returns: True if the file has been modified by another program, otherwise False, and None if the file cannot be accessed. """ if self.file_exists(): stat = filestat(self.file_path) if stat is not None: return self._filestat != stat return None
def test(c): """check if this document has been externally modified is_externally_modified returns True, False or None. If a file exists at the path of this document then return True if the document has been modified by another program else False. However, if there is no file at the path of this document this function will return None. """ with make_file() as path, test_app() as app: if not c.exists: path += "-not-found" doc = TextDocument(app, (None if c.url_is_none else path)) if c.exists: if c.change == "modify": stat = filestat(path) with open(path, "a", encoding="utf-8") as file: file.write("more data") assert stat != filestat(path) elif c.change == "remove": os.remove(path) eq_(doc.is_externally_modified(), c.rval)
def test(f_exists): m = Mocker() exists = m.replace("os.path.exists") stat = m.replace("os.stat") path = "<path>" exists(path) >> f_exists if f_exists: stat(path) >> (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) res = (6, 8) else: res = None with m: result = filestat(path) eq_(result, res)
def check_for_external_changes(self, window): if not self.is_externally_modified(): return if self.isDocumentEdited(): if window is None: return # ignore change (no gui for alert) stat = filestat(self.fileURL().path()) if self._filestat == stat: return self._filestat = stat def callback(code): if code == ak.NSAlertFirstButtonReturn: self.reload_document() alert = Alert.alloc().init() alert.setMessageText_("“%s” source document changed" % self.displayName()) alert.setInformativeText_("Discard changes and reload?") alert.addButtonWithTitle_("Reload") alert.addButtonWithTitle_("Cancel") alert.beginSheetModalForWindow_withCallback_(window, callback) else: self.reload_document()
def check_for_external_changes(self, window): if not self.is_externally_modified(): return if self.is_dirty(): if window is None: return # ignore change (no gui for alert) stat = filestat(self.file_path) if self._filestat == stat: # TODO is this check necessary? return self._filestat = stat def callback(code, end_alert): if code == ak.NSAlertFirstButtonReturn: self.reload_document() # should self.file_changed_since_save() -> True on cancel here? alert = Alert.alloc().init() alert.setMessageText_("“%s” source document changed" % self.name) alert.setInformativeText_("Discard changes and reload?") alert.addButtonWithTitle_("Reload") alert.addButtonWithTitle_("Cancel") alert.beginSheetModalForWindow_withCallback_(window, callback) else: self.reload_document()
def _refresh_file_mtime(self): if self.file_exists(): self._filestat = filestat(self.file_path) return self._filestat = None