2010-02-09 02:45:56 +01:00
|
|
|
######################################################################
|
|
|
|
# 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/>.
|
|
|
|
######################################################################
|
|
|
|
|
2010-01-18 20:08:38 +01:00
|
|
|
import os
|
|
|
|
import ConfigParser
|
2010-02-08 01:24:18 +01:00
|
|
|
import sys
|
|
|
|
|
2010-03-17 23:11:18 +01:00
|
|
|
# Get our reference point. preferably $HOME.
|
2010-02-08 01:24:18 +01:00
|
|
|
homedir = os.getenv("HOME")
|
|
|
|
if homedir is None:
|
|
|
|
homedir = os.path.dirname(sys.argv[0])
|
2010-03-16 23:17:46 +01:00
|
|
|
|
2010-01-18 20:08:38 +01:00
|
|
|
class _Configuration(object):
|
2010-03-17 23:11:18 +01:00
|
|
|
def __init__(self, optioncreator):
|
2010-02-08 01:24:18 +01:00
|
|
|
self.filename = os.path.join(os.path.join(homedir, ".4grab"), "config.cfg")
|
2010-01-18 20:08:38 +01:00
|
|
|
self.configparser = ConfigParser.RawConfigParser()
|
2010-03-17 23:11:18 +01:00
|
|
|
self.optioncreator = optioncreator
|
2010-03-16 23:17:46 +01:00
|
|
|
|
|
|
|
self.configparser.read(self.filename)
|
2010-03-17 23:11:18 +01:00
|
|
|
print "__init__"
|
2010-03-16 23:17:46 +01:00
|
|
|
|
|
|
|
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
|
2010-03-17 23:11:18 +01:00
|
|
|
# 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
|
2010-03-16 23:17:46 +01:00
|
|
|
# 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
|
2010-03-17 23:11:18 +01:00
|
|
|
# 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
|
2010-03-16 23:17:46 +01:00
|
|
|
if changed:
|
|
|
|
self.save()
|
2010-01-18 20:08:38 +01:00
|
|
|
|
2010-03-16 23:17:46 +01:00
|
|
|
def create_option(self, section, name, default, message):
|
|
|
|
self.configparser.set(section,
|
|
|
|
name,
|
|
|
|
self.optioncreator(default,
|
|
|
|
message))
|
2010-01-18 20:08:38 +01:00
|
|
|
|
|
|
|
def get_download_location(self):
|
2010-03-16 23:17:46 +01:00
|
|
|
return self.configparser.get("locations", "download_base")
|
2010-03-17 23:11:18 +01:00
|
|
|
def set_download_location(self, value):
|
|
|
|
self.configparser.set("locations", "download_base", value)
|
2010-01-18 20:08:38 +01:00
|
|
|
|
2010-03-17 23:11:18 +01:00
|
|
|
def get_archive_location(self):
|
|
|
|
return self.configparser.get("locations", "archive")
|
|
|
|
def set_archive_location(self, value):
|
|
|
|
self.configparser.set("locations", "archive", value)
|
2010-01-18 20:08:38 +01:00
|
|
|
|
2010-03-17 23:11:18 +01:00
|
|
|
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))
|
2010-02-09 01:32:42 +01:00
|
|
|
|
|
|
|
def option_exists(self, option):
|
2010-02-09 02:10:17 +01:00
|
|
|
sections = self.configparser.sections()
|
|
|
|
for section in sections:
|
2010-02-09 01:32:42 +01:00
|
|
|
if self.configparser.has_option(section, option):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2010-03-17 23:11:18 +01:00
|
|
|
# Should only be used by the command-line
|
2010-02-09 02:10:17 +01:00
|
|
|
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
|
|
|
|
|
2010-02-09 01:32:42 +01:00
|
|
|
def save(self):
|
|
|
|
dirname = os.path.dirname(self.filename)
|
|
|
|
if not os.path.exists(dirname):
|
2010-03-16 23:17:46 +01:00
|
|
|
os.makedirs(dirname)
|
2010-02-09 01:32:42 +01:00
|
|
|
configfile = open(self.filename, "w")
|
|
|
|
self.configparser.write(configfile)
|
|
|
|
|
2010-03-17 23:11:18 +01:00
|
|
|
_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
|