diff options
Diffstat (limited to 'config.py')
-rw-r--r-- | config.py | 68 |
1 files changed, 48 insertions, 20 deletions
@@ -24,36 +24,64 @@ 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.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 - self.save() + if changed: + 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 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") + return self.configparser.get("locations", "download_base") def get_category(self): - return self.configparser.get("settings", "category") + return self.configparser.get("settings", "categories") def set_category(self, value): self.configparser.set("settings", "category", value) @@ -81,7 +109,7 @@ class _Configuration(object): def save(self): dirname = os.path.dirname(self.filename) if not os.path.exists(dirname): - os.mkdir(dirname) + os.makedirs(dirname) configfile = open(self.filename, "w") self.configparser.write(configfile) |