From bbc0d27a0bf7d4592e83c834863fba6eabff0b12 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 26 Jan 2018 23:37:43 +0100 Subject: Ability to merge config from remote URLs Closes #2 --- jfr_playoff/settings.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'jfr_playoff') diff --git a/jfr_playoff/settings.py b/jfr_playoff/settings.py index ba03d72..a40fd9b 100644 --- a/jfr_playoff/settings.py +++ b/jfr_playoff/settings.py @@ -2,6 +2,7 @@ import glob import json import readline import sys +import urllib def complete_filename(text, state): @@ -20,6 +21,21 @@ class PlayoffSettings(object): else: self.interactive = True + def __merge_config(self, base_config, + new_config=None, remote_url=None, + overwrite=True): + try: + remote_config = new_config if new_config is not None else \ + json.loads(urllib.urlopen( + remote_url).read().decode('utf-8-sig')) + for key, value in remote_config.iteritems(): + if (key not in base_config) or overwrite: + base_config[key] = value + except e: + print 'WARNING: unable to fetch remote config %s: %s' % ( + url, str(e)) + return base_config + def load(self): if self.settings_file is None: readline.set_completer_delims(' \t\n;') @@ -31,6 +47,14 @@ class PlayoffSettings(object): if self.settings is None: self.settings = json.loads( open(unicode(self.settings_file)).read().decode('utf-8-sig')) + if self.has_section('remotes'): + remote_config = {} + for remote in self.get('remotes'): + remote_config = self.__merge_config( + remote_config, remote_url=remote) + self.settings = self.__merge_config( + self.settings, new_config=remote_config, + overwrite=False) def has_section(self, key): self.load() -- cgit v1.2.3