From 99b87aeffb9412ad7a0cc1f795fd6af89cf4eb76 Mon Sep 17 00:00:00 2001 From: ryuslash Date: Wed, 21 Apr 2010 15:40:20 +0200 Subject: Log function works, need to start using it now --- 4grab.py | 13 +++++++++++-- util.py | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/4grab.py b/4grab.py index 367cc04..c728dfc 100755 --- a/4grab.py +++ b/4grab.py @@ -25,6 +25,7 @@ import os import config from util import raw_input_with_default +import util import download import progressbar @@ -110,6 +111,12 @@ under certain conditions.""") dest="sort", help="Sort downloaded images, most handy if you've used " "older versions which didn't sort yet") + parser.add_option("-l", + "--loglevel", + nargs=1, + dest="loglevel", + metavar="LEVEL", + help="Changes the default log level to LEVEL") (options, args) = parser.parse_args() if options.confval and (options.tempcat @@ -175,8 +182,9 @@ under certain conditions.""") elif options.tempcat: conf.set_categories([options.tempcat]) -#base_url = "%s%s/" % (base_url, conf.get_categories()) - + elif options.loglevel is not None: + util.loglevel = util.LogType.from_int(options.loglevel) + if __name__ == "__main__": conf = config.Configuration() sort = sorter.Sorter() @@ -196,3 +204,4 @@ if __name__ == "__main__": except KeyboardInterrupt: print print "So you don't want these images? Fine! I'll stop then." + util.log(util.LogType.Err, "Quit on user request") diff --git a/util.py b/util.py index 69a24e5..d7cce7f 100644 --- a/util.py +++ b/util.py @@ -1,13 +1,40 @@ import os import sys -homedir = os.getenv("HOME") -if homedir is None: - homedir = os.path.dirname(sys.argv[0]) -confdir = os.path.join(homedir, ".4grab") +class LogType: + Non = 0 + Err = 1 + Warn = 2 + Msg = 3 + @staticmethod + def from_int(lloglevel): + iloglevel = int(lloglevel) + if iloglevel == 0: + return LogType.Non + if iloglevel == 1: + return LogType.Err + if iloglevel == 2: + return LogType.Warn + if iloglevel == 3: + return LogType.Msg + +loglevel = LogType.Non def raw_input_with_default(default, prompt): inp = raw_input("%s (default=%s): " % (prompt, default)) if inp == "": return default return inp + +def log(logtype, message, data = None): + global loglevel + print logtype, loglevel, loglevel >= logtype + if loglevel >= logtype: + print message + if not data is None: + print data + +homedir = os.getenv("HOME") +if homedir is None: + homedir = os.path.dirname(sys.argv[0]) +confdir = os.path.join(homedir, ".4grab") -- cgit v1.2.3-54-g00ecf