4grab/config.py
ryuslash 8e101c92f9 Disconnected the property creation function
The function used to create a new property has been changed to be a callback function, so that later on a UI might use a dialog.
2010-03-16 23:17:46 +01:00

117 lines
4.1 KiB
Python

######################################################################
# 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
homedir = os.getenv("HOME")
if homedir is None:
homedir = os.path.dirname(sys.argv[0])
class _Configuration(object):
def __init__(self):
self.filename = os.path.join(os.path.join(homedir, ".4grab"), "config.cfg")
self.configparser = ConfigParser.RawConfigParser()
self.check()
self.configparser.read(self.filename)
def set_optioncreator(self, optioncreator):
self.optioncreator = optioncreator
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
# 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
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 get_category(self):
return self.configparser.get("settings", "categories")
def set_category(self, value):
self.configparser.set("settings", "category", 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
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 = _Configuration()
def Configuration(): return _configuration