summaryrefslogtreecommitdiff
path: root/jfr_playoff/settings.py
blob: ba03d727aa205d729bdeb5d306a902baf0acea49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import glob
import json
import readline
import sys


def complete_filename(text, state):
    return (glob.glob(text+'*')+[None])[state]


class PlayoffSettings(object):

    def __init__(self):
        self.settings = None
        self.interactive = False
        self.settings_file = None
        if len(sys.argv) > 1:
            self.settings_file = sys.argv[1].decode(
                sys.getfilesystemencoding())
        else:
            self.interactive = True

    def load(self):
        if self.settings_file is None:
            readline.set_completer_delims(' \t\n;')
            readline.parse_and_bind("tab: complete")
            readline.set_completer(complete_filename)
            self.settings_file = raw_input(
                'JSON settings file: ').decode(sys.stdin.encoding)

        if self.settings is None:
            self.settings = json.loads(
                open(unicode(self.settings_file)).read().decode('utf-8-sig'))

    def has_section(self, key):
        self.load()
        return key in self.settings

    def get(self, *keys):
        self.load()
        section = self.settings
        for key in keys:
            section = section[key]
        return section