aboutsummaryrefslogtreecommitdiff
path: root/AndroidResR/util/ConfigLoader.py
blob: b12771fdd7cbfa42bfaa371684cb684fadf7b581 (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
import ConfigParser
from os.path import expanduser, join, isfile

__author__ = 'victor'


class ConfigLoader():
    SRCPATH = "srcpath"
    DESTPATH = "destpath"

    def __init__(self):
        self.userHome = expanduser("~")
        self.CONFIG_FILE = join(self.userHome, '.androidresr')
        self.config = ConfigParser.RawConfigParser(allow_no_value=True)

        if not isfile(self.CONFIG_FILE):
            self.initFile()

        self.load()

    def load(self):
        if not isfile(self.CONFIG_FILE):
                raise RuntimeError('No config file (', self.CONFIG_FILE, ') found')
        self.config.readfp(open(self.CONFIG_FILE))

    def set(self, key, value):
        self.config.set('general', key, value)
        self.write()

    def get(self, key):
        try:
            return self.config.get("general", key)
        except ConfigParser.NoOptionError:
            return None

    def initFile(self):
        self.config.add_section('general')
        self.write()

    def write(self):
        with open(self.CONFIG_FILE, 'w') as configfile:
            self.config.write(configfile)