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.
This commit is contained in:
ryuslash 2010-03-16 23:17:46 +01:00
parent caba2811b9
commit 8e101c92f9
3 changed files with 67 additions and 29 deletions

View file

@ -26,9 +26,18 @@ import config
import download import download
import progressbar import progressbar
def raw_input_with_default(default, prompt):
inp = raw_input("%s (default=%s): " % (prompt, default))
if inp == "":
return default
return inp
base_url = "http://boards.4chan.org/" base_url = "http://boards.4chan.org/"
parser = optparse.OptionParser() parser = optparse.OptionParser()
downloader = download.Downloader(progressbar.Progress) downloader = download.Downloader(progressbar.Progress)
conf = config.Configuration()
conf.set_optioncreator(raw_input_with_default)
def walk_with_wizard(baseurl): def walk_with_wizard(baseurl):
wzrd_msg = "Pilates! *SHAZAM* Here they come!" wzrd_msg = "Pilates! *SHAZAM* Here they come!"
@ -54,8 +63,8 @@ def walk_with_wizard(baseurl):
[thread]) [thread])
else: else:
inp = raw_input("Which category would you like to download? ") inp = raw_input("Which category would you like to download? ")
config.Configuration().set_category(inp) conf.set_category(inp)
baseurl = "%s%s/" % (baseurl, config.Configuration().get_category()) baseurl = "%s%s/" % (baseurl, conf.get_category())
print wzrd_msg print wzrd_msg
t = downloader.get_thread_links(baseurl) t = downloader.get_thread_links(baseurl)
@ -109,15 +118,15 @@ if options.wizard and (options.tempcat
exit(1) exit(1)
if options.confval: if options.confval:
if not config.Configuration().option_exists(options.confval[0]): if not conf.option_exists(options.confval[0]):
print ("%s: error: %s is not a " print ("%s: error: %s is not a "
"valid configuration option") % (sys.argv[0], "valid configuration option") % (sys.argv[0],
options.confval[0]) options.confval[0])
exit(1) exit(1)
print "Setting", options.confval[0], "to", options.confval[1] print "Setting", options.confval[0], "to", options.confval[1]
config.Configuration().set_option(options.confval[0], conf.set_option(options.confval[0],
options.confval[1]) options.confval[1])
config.Configuration().save() conf.save()
exit(0) exit(0)
elif options.wizard: elif options.wizard:
@ -150,9 +159,9 @@ elif options.thread:
exit(0) exit(0)
elif options.tempcat: elif options.tempcat:
config.Configuration().set_category(options.tempcat) conf.set_category(options.tempcat)
base_url = "%s%s/" % (base_url, config.Configuration().get_category()) base_url = "%s%s/" % (base_url, conf.get_category())
try: try:
t = downloader.get_thread_links(base_url) t = downloader.get_thread_links(base_url)

View file

@ -29,31 +29,59 @@ class _Configuration(object):
def __init__(self): def __init__(self):
self.filename = os.path.join(os.path.join(homedir, ".4grab"), "config.cfg") self.filename = os.path.join(os.path.join(homedir, ".4grab"), "config.cfg")
self.configparser = ConfigParser.RawConfigParser() self.configparser = ConfigParser.RawConfigParser()
if not os.path.exists(self.filename):
self.create_new() self.check()
else:
self.configparser.read(self.filename) self.configparser.read(self.filename)
def create_new(self): def set_optioncreator(self, optioncreator):
self.configparser.add_section("settings") self.optioncreator = optioncreator
self.set_category(self.raw_input_with_default("w", "Please enter which category you would like to download from: "))
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") 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: ")) # 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() self.save()
def raw_input_with_default(self, default, prompt): def create_option(self, section, name, default, message):
inp = raw_input("%s (default=%s): " % (prompt, default)) self.configparser.set(section,
if inp == "": name,
return default self.optioncreator(default,
return inp message))
def get_download_location(self): def get_download_location(self):
return self.configparser.get("locations", "download") return self.configparser.get("locations", "download_base")
def get_category(self): def get_category(self):
return self.configparser.get("settings", "category") return self.configparser.get("settings", "categories")
def set_category(self, value): def set_category(self, value):
self.configparser.set("settings", "category", value) self.configparser.set("settings", "category", value)
@ -81,7 +109,7 @@ class _Configuration(object):
def save(self): def save(self):
dirname = os.path.dirname(self.filename) dirname = os.path.dirname(self.filename)
if not os.path.exists(dirname): if not os.path.exists(dirname):
os.mkdir(dirname) os.makedirs(dirname)
configfile = open(self.filename, "w") configfile = open(self.filename, "w")
self.configparser.write(configfile) self.configparser.write(configfile)

View file

@ -25,7 +25,8 @@ import htmlparser
#import progressbar #import progressbar
import config import config
savedir = config.Configuration().get_download_location() conf = config.Configuration()
savedir = conf.get_download_location()
if not os.path.exists(savedir): if not os.path.exists(savedir):
os.makedirs(savedir) os.makedirs(savedir)
@ -127,7 +128,7 @@ class Downloader(object):
if __name__ == "__main__": if __name__ == "__main__":
# Get a file-like object for the 4chan.org w/imgboard # Get a file-like object for the 4chan.org w/imgboard
base_url = "http://boards.4chan.org/" + config.Configuration().get_category() + "/" base_url = "http://boards.4chan.org/" + conf.get_category() + "/"
# Get the hyperlinks. # Get the hyperlinks.
t = get_thread_links(base_url) t = get_thread_links(base_url)