From 07133b79e5abbc8dd38d199c44be4726f9399cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 25 Nov 2014 17:04:27 +0100 Subject: [PATCH] Track modification time of configuration file and reload it if it was externally modified before changing settings --- src/octoprint/settings.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 7032b944..83e12700 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -190,6 +190,7 @@ class Settings(object): self._config = None self._dirty = False + self._mtime = None self._init_settings_dir(basedir) @@ -224,6 +225,7 @@ class Settings(object): if os.path.exists(self._configfile) and os.path.isfile(self._configfile): with open(self._configfile, "r") as f: self._config = yaml.safe_load(f) + self._mtime = self._last_modified() # changed from else to handle cases where the file exists, but is empty / 0 bytes if not self._config: self._config = {} @@ -358,6 +360,10 @@ class Settings(object): self.load() return True + def _last_modified(self): + stat = os.stat(self._configfile) + return stat.st_mtime + #~~ getter def get(self, path, asdict=False, defaults=None, merged=False): @@ -515,6 +521,9 @@ class Settings(object): if len(path) == 0: return + if self._mtime is not None and self._last_modified() != self._mtime: + self.load() + config = self._config if defaults is None: defaults = default_settings