From 00318cc7a1343ef8609b3a0c7fbd3d0ba620084c Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Wed, 11 Feb 2026 14:20:59 +0100 Subject: [PATCH] frappy-edit: add default cfg path given typically at psi Change-Id: I91032c912d76f555ae88e3e7ae313fa3c03948bd --- frappy/tools/cfgedit.py | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/frappy/tools/cfgedit.py b/frappy/tools/cfgedit.py index 8bb9bb80..59de0e6c 100644 --- a/frappy/tools/cfgedit.py +++ b/frappy/tools/cfgedit.py @@ -23,11 +23,13 @@ KEY.add( VERSION_SEPARATOR = "\n''''" TIMESTAMP_FMT = '%Y-%m-%d-%H%M%S' + + +def get_timestamp(file): + return time.strftime(TIMESTAMP_FMT, time.localtime(file.stat().st_mtime)) + # TODO: -# - ctrl-K: on previous versions copy and advance to next module # - use also shift-Tab for level up? -# - directory to save to -# - remove older versions, for which a newer exist already: a restore removes the older one def unix_cmd(cmd, *args): @@ -749,7 +751,14 @@ class EditorMain(Main): except FileNotFoundError: sections = [] self.versions = dict((v.split('\n', 1) for v in sections)) + # the path every change is written to: self.tmppath = self.version_dir / f'{name}.current' + try: + # if a file exists already, add it to the version history + filecontent = self.tmppath.read_text() + self.add_version(filecontent, get_timestamp(self.tmppath)) + except FileNotFoundError: + pass if cfgpath: cfgpaths = [cfgpath] else: @@ -764,7 +773,7 @@ class EditorMain(Main): self.cfgpath = cfgpath if cfgpath != self.tmppath: self.titlebar.mid = str(cfgpath) - timestamp = time.strftime(TIMESTAMP_FMT, time.localtime(cfgpath.stat().st_mtime)) + timestamp = get_timestamp(cfgpath) break except FileNotFoundError: pass @@ -864,6 +873,13 @@ class EditorMain(Main): def quit(self): self.save() + try: + filecontent = self.cfgpath.read_text() + except FileNotFoundError: + filecontent = '' + if filecontent == self.filecontent: + self.log.info('%s was not changed', self.cfgpath) + return True savedialog = SaveDialog(self.cfgpath) if savedialog.execute(self) == KEY.QUIT: filename = savedialog.filename @@ -912,11 +928,21 @@ class EditorMain(Main): except Exception: print(formatExtendedTraceback()) finally: - if self.pidfile: + if self.filecontent: + # add current content to the version history + try: + self.add_version(self.filecontent, time.strftime(TIMESTAMP_FMT)) + except FileNotFoundError: + pass + if self.tmppath and self.tmppath.exists(): + # no need to keep the temporary file, as it has been added to the versions + self.tmppath.unlink() + if self.pidfile and self.pidfile.exists(): self.pidfile.unlink() if __name__ == "__main__": - os.environ['FRAPPY_CONFDIR'] = 'cfg:cfg/main:cfg/stick:cfg/addons' + # os.environ['FRAPPY_CONFDIR'] = 'cfg:cfg/main:cfg/stick:cfg/addons' generalConfig.init() + os.environ['FRAPPY_CONFDIR'] EditorMain(sys.argv[1]).run()