Revision: 4012 Author: wikipedian Date: 2007-08-09 11:24:21 +0000 (Thu, 09 Aug 2007)
Log Message: ----------- Replaced non-functional config variables always_log and never_log by an array with which one can exactly specify which scripts shall keep a logfile. The settings can be overridden by the -log or -nolog command-line arguments.
Modified Paths: -------------- trunk/pywikipedia/config.py trunk/pywikipedia/interwiki.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/config.py =================================================================== --- trunk/pywikipedia/config.py 2007-08-09 11:18:25 UTC (rev 4011) +++ trunk/pywikipedia/config.py 2007-08-09 11:24:21 UTC (rev 4012) @@ -159,11 +159,16 @@
############## LOGFILE SETTINGS ##############
-# Should all bots keep a logfile? -# TODO: Not used yet -always_log = False -# Should no bot, not even interwiki.py, keep a logfile? -never_log = False +# Defines for which scripts a logfile should be enabled. Logfiles will be +# saved in the 'logs' subdirectory. +# Example: +# log = ['interwiki', 'weblinkchecker', 'table2wiki'] +# It is also possible to enable logging for all scripts, using this line: +# log = 'all' +# Per default, logging of interwiki.py is enabled because its logfiles can +# be used to generate so-called warnfiles. +# This setting can be overridden by the -log or -nolog command-line arguments. +log = ['interwiki']
############## INTERWIKI SETTINGS ##############
Modified: trunk/pywikipedia/interwiki.py =================================================================== --- trunk/pywikipedia/interwiki.py 2007-08-09 11:18:25 UTC (rev 4011) +++ trunk/pywikipedia/interwiki.py 2007-08-09 11:24:21 UTC (rev 4012) @@ -1378,9 +1378,6 @@ # that are also used by other scripts and that determine on which pages # to work on. genFactory = pagegenerators.GeneratorFactory() - - if not config.never_log: - wikipedia.activateLog('interwiki.log')
for arg in wikipedia.handleArgs(): if arg == '-noauto':
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2007-08-09 11:18:25 UTC (rev 4011) +++ trunk/pywikipedia/wikipedia.py 2007-08-09 11:24:21 UTC (rev 4012) @@ -4132,6 +4132,18 @@ default_code = site.language default_family = site.family
+def calledModuleName(): + """ + Gets the name of the module calling this function. This is + required because the -help option loads the module's docstring + and because the module name will be used for the filename of the + log. + """ + # get commandline arguments + args = sys.argv + # TODO: check if the following line is platform-independent + return args[0][:args[0].rindex('.')] + def handleArgs(): ''' Takes the commandline arguments, converts them to Unicode, processes all @@ -4146,7 +4158,7 @@ # required because the -help option loads the module's docstring and because # the module name will be used for the filename of the log. # TODO: check if the following line is platform-independent - moduleName = args[0][:args[0].rindex('.')] + moduleName = calledModuleName() nonGlobalArgs = [] for arg in args[1:]: if sys.platform=='win32': @@ -4170,12 +4182,11 @@ elif arg.startswith('-pt:'): put_throttle.setDelay(int(arg[4:]), absolute = True) elif arg == '-log': - activateLog('%s.log' % moduleName) + setLogfileStatus(True) elif arg.startswith('-log:'): - activateLog(arg[5:]) + setLogfileStatus(True, arg[5:]) elif arg == '-nolog': - global logfile - logfile = None + setLogfileStatus(False) elif arg == '-verbose' or arg == "-v": import version output('Pywikipediabot %s' % (version.getversion())) @@ -4427,16 +4438,25 @@ dpath = normpath(dirname(path)) if not exists(dpath): makedirs(dpath) return normpath(abspath(path)) - -def activateLog(logname): + +def setLogfileStatus(enabled, logname = None): global logfile - import wikipediatools as _wt - logfn = _wt.absoluteFilename('logs', logname) - try: - logfile = codecs.open(logfn, 'a', 'utf-8') - except IOError: - logfile = codecs.open(logfn, 'w', 'utf-8') + if enabled: + if not logname: + logname = '%s.log' % calledModuleName() + import wikipediatools as _wt + logfn = _wt.absoluteFilename('logs', logname) + try: + logfile = codecs.open(logfn, 'a', 'utf-8') + except IOError: + logfile = codecs.open(logfn, 'w', 'utf-8') + else: + # disable the log file + logfile = None
+if config.log == 'all' or calledModuleName() in config.log: + setLogfileStatus(True) + output_lock = threading.Lock() input_lock = threading.Lock() output_cache = []
pywikipedia-l@lists.wikimedia.org