diff options
author | emkael <emkael@tlen.pl> | 2017-07-31 20:04:02 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2017-07-31 20:04:02 +0200 |
commit | 46b24f530191dd443a4edb451b5f5573a875a5ca (patch) | |
tree | 454f1cbeebd2fce87c145ec55709b4027725ca20 | |
parent | 13d8dbb4135f290a80577f6fca09d9488e861a18 (diff) |
Factoring out settings loading mechanism
-rw-r--r-- | playoff.py | 21 | ||||
-rw-r--r-- | playoff/settings.py | 31 |
2 files changed, 35 insertions, 17 deletions
@@ -1,25 +1,12 @@ -import glob, json, os, readline, shutil, socket, sys +import os, shutil, socket from datetime import datetime from urlparse import urljoin from playoff import sql as p_sql from playoff import template as p_temp -def complete_filename(text, state): - return (glob.glob(text+'*')+[None])[state] - -if len(sys.argv) > 1: - settings_file = sys.argv[1] -else: - readline.set_completer_delims(' \t\n;') - readline.parse_and_bind("tab: complete") - readline.set_completer(complete_filename) - settings_file = raw_input('JSON settings file: ') - -if not os.path.exists(settings_file): - print 'Settings file "%s" not found' % settings_file - sys.exit(1) - -settings = json.load(open(settings_file)) +from playoff.settings import PlayoffSettings
+s = PlayoffSettings()
+settings = s.get()
teams = settings['teams'] leaderboard = [None] * len(teams) diff --git a/playoff/settings.py b/playoff/settings.py new file mode 100644 index 0000000..dd7399d --- /dev/null +++ b/playoff/settings.py @@ -0,0 +1,31 @@ +import glob, json, os, readline, sys + +def complete_filename(text, state): + return (glob.glob(text+'*')+[None])[state] + +class PlayoffSettings: + + def __init__(self): + self.interactive = False + if len(sys.argv) > 1: + settings_file = sys.argv[1] + else: + self.interactive = True + readline.set_completer_delims(' \t\n;') + readline.parse_and_bind("tab: complete") + readline.set_completer(complete_filename) + settings_file = raw_input('JSON settings file: ') + + if not os.path.exists(settings_file): + raise IOError('Settings file %s not found' % settings_file) + + self.settings = json.load(open(settings_file)) + + def has_section(self, key): + return key in self.settings + + def get(self, *keys): + section = self.settings + for key in keys: + section = section[key] + return section |