Track modification time of configuration file and reload it if it was externally modified before changing settings
This commit is contained in:
parent
183a8feed4
commit
07133b79e5
1 changed files with 9 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue