1b09267876
Configuration Settings can now be changed with the -e command line argument execution of 4grab has been moved to 4grab.py, but should also still work with download.py
70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
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()
|
|
if not os.path.exists(self.filename):
|
|
self.create_new()
|
|
else:
|
|
self.configparser.read(self.filename)
|
|
|
|
def create_new(self):
|
|
self.configparser.add_section("settings")
|
|
self.set_category(self.raw_input_with_default("w", "Please enter which category you would like to download from: "))
|
|
|
|
self.configparser.add_section("locations")
|
|
self.configparser.set("locations", "download", self.raw_input_with_default(os.path.join(homedir, "Pictures"), "Please enter where you would like the downloads to go: "))
|
|
|
|
self.save()
|
|
|
|
def raw_input_with_default(self, default, prompt):
|
|
inp = raw_input("%s (default=%s): " % (prompt, default))
|
|
if inp == "":
|
|
return default
|
|
return inp
|
|
|
|
def get_download_location(self):
|
|
return self.configparser.get("locations", "download")
|
|
|
|
def get_category(self):
|
|
return self.configparser.get("settings", "category")
|
|
|
|
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.mkdir(dirname)
|
|
configfile = open(self.filename, "w")
|
|
self.configparser.write(configfile)
|
|
|
|
_configuration = _Configuration()
|
|
def Configuration(): return _configuration
|