working on the command-line arguments here

This commit is contained in:
ryuslash 2010-02-09 01:32:42 +01:00
parent 53c7802ae2
commit 32c4471273
3 changed files with 37 additions and 8 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
*~ *~
*.pyc *.pyc
#*#
.*

17
4grab.py Normal file
View file

@ -0,0 +1,17 @@
import optparse
import sys
import config
parser = optparse.OptionParser()
parser.add_option("-e", nargs=2, dest="confval", metavar="VALUE")
(options, args) = parser.parse_args()
if options.confval:
if not config.Configuration().option_exists(options.confval[0]):
print "%s: error: %s is not a valid configuration option" % (sys.argv[0], options.confval[0])
exit(1)
print "Setting", options.confval[0], "to", options.confval[1]
config.Configuration().

View file

@ -12,18 +12,12 @@ class _Configuration(object):
def create_new(self): def create_new(self):
self.configparser.add_section("settings") self.configparser.add_section("settings")
self.configparser.set("settings", "category", self.raw_input_with_default("w", "Please enter which category you would like to download from: ")) 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.add_section("locations")
self.configparser.set("locations", "download", self.raw_input_with_default(os.path.join(os.getenv("HOME"), "Pictures"), "Please enter where you would like the downloads to go: ")) self.configparser.set("locations", "download", self.raw_input_with_default(os.path.join(os.getenv("HOME"), "Pictures"), "Please enter where you would like the downloads to go: "))
#with open(self.filename) as configfile: self.save()
# self.configparser.write(configfile)
dirname = os.path.dirname(self.filename)
if not os.path.exists(dirname):
os.mkdir(dirname)
configfile = open(self.filename, "w")
self.configparser.write(configfile)
def raw_input_with_default(self, default, prompt): def raw_input_with_default(self, default, prompt):
inp = raw_input("%s (default=%s): " % (prompt, default)) inp = raw_input("%s (default=%s): " % (prompt, default))
@ -37,5 +31,21 @@ class _Configuration(object):
def get_category(self): def get_category(self):
return self.configparser.get("settings", "category") return self.configparser.get("settings", "category")
def set_category(self, value):
self.configparser.set("settings", "category", value)
def option_exists(self, option):
for section in self.configparser.sections():
if self.configparser.has_option(section, option):
return True
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() _configuration = _Configuration()
def Configuration(): return _configuration def Configuration(): return _configuration