summaryrefslogtreecommitdiffstats
path: root/config.py
blob: 66f3297fe0dc9cf786646c59392ab002b249ca7b (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
######################################################################
# Copyright 2009, 2010 ryuslash
#
# This file is part of 4grab.
#
# 4grab is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 4grab is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with 4grab.  If not, see <http://www.gnu.org/licenses/>.
######################################################################

import os
import ConfigParser
import sys

# Get our reference point. preferably $HOME.
homedir = os.getenv("HOME")
if homedir is None:
    homedir = os.path.dirname(sys.argv[0])
    
class _Configuration(object):
    def __init__(self, optioncreator):
        self.filename = os.path.join(os.path.join(homedir, ".4grab"), "config.cfg")
        self.configparser = ConfigParser.RawConfigParser()
        self.optioncreator = optioncreator
        
        self.configparser.read(self.filename)
        print "__init__"

    def check(self):
        changed = False
        # read if it exists
        if os.path.exists(self.filename):
                self.configparser.read(self.filename)
        # locations
        if not self.configparser.has_section("locations"):
            self.configparser.add_section("locations")
        # locations/download_base
        if not self.configparser.has_option("locations", "download_base"):
            self.create_option("locations",
                               "download_base",
                               os.path.join(homedir,
                                            "Pictures"),
                               "Please enter where "
                               "you would like the "
                               "downloads to go: ")
            changed = True
        # locations/archive
        if not self.configparser.has_option("locations", "archive"):
            self.create_option("locations",
                               "archive",
                               os.path.join(self.configparser.get("locations",
                                                                  "download_base"),
                                            ".arch"),
                               "Please enter where in {download_base} you "
                               "would like to store archived images (used for "
                               "checking what to download): ")
            changed = True
        # settings
        if not self.configparser.has_section("settings"):
            self.configparser.add_section("settings")
        # settings/categories
        if not self.configparser.has_option("settings", "categories"):
            self.create_option("settings",
                               "categories",
                               "w",
                               "Please enter which "
                               "category you would like "
                               "to download from: ")
            changed = True
        # settings/resolutions
        if not self.configparser.has_option("settings", "resolutions"):
            self.create_option("settings",
                               "resolutions",
                               "1600x1050,1900x1200,1900x1080",
                               "Please enter your preferred "
                               "resolutions (* for all)")
            changed = True
        # save
        if changed:
            self.save()

    def create_option(self, section, name, default, message):
        self.configparser.set(section,
                              name,
                              self.optioncreator(default,
                                                 message))

    def get_download_location(self):
        return self.configparser.get("locations", "download_base")
    def set_download_location(self, value):
        self.configparser.set("locations", "download_base", value)

    def get_archive_location(self):
        return self.configparser.get("locations", "archive")
    def set_archive_location(self, value):
        self.configparser.set("locations", "archive", value)

    def get_categories(self):
        return self.configparser.get("settings", "categories").split(',')
    def set_categories(self, value = []):
        self.configparser.set("settings", "category", ','.join(value))

    def get_resolutions(self):
        return self.configparser.get("settings", "resolutions").split(',')
    def set_resolutions(self, value = []):
        self.configparser.set("settings", "resolutions", ','.join(value))

    def option_exists(self, option):
        sections = self.configparser.sections()
        for section in sections:
            if self.configparser.has_option(section, option):
                return True
        return False

    # Should only be used by the command-line
    def set_option(self, option, value):
        sec = None
        sections = self.configparser.sections()
        for section in sections:
            if self.configparser.has_option(section, option):
                sec = section
        
        if not sec is None:
            self.configparser.set(sec, option, value)
            return True
        else:
            return False

    def save(self):
        dirname = os.path.dirname(self.filename)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        configfile = open(self.filename, "w")
        self.configparser.write(configfile)

_configuration = None
_optioncreator = None        
def Configuration():
    global _optioncreator
    global _configuration

    if _optioncreator is None:
        raise ValueError("optioncreator must be set")
    if _configuration is None:
        _configuration = _Configuration(_optioncreator)
        _configuration.check()
    return _configuration