[Pywikipedia-l] SVN: [4949] trunk/pywikipedia/wikipedia.py

wikipedian at svn.wikimedia.org wikipedian at svn.wikimedia.org
Wed Jan 30 12:19:31 UTC 2008


Revision: 4949
Author:   wikipedian
Date:     2008-01-30 12:19:31 +0000 (Wed, 30 Jan 2008)

Log Message:
-----------
Each time a script is called, a line is now written to the file logs/commands.log. This 
line contains a timestamp, the called module, and the parameters.

You can use this new logfile to track errors and to report bugs.

Modified Paths:
--------------
    trunk/pywikipedia/wikipedia.py

Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py	2008-01-30 10:17:03 UTC (rev 4948)
+++ trunk/pywikipedia/wikipedia.py	2008-01-30 12:19:31 UTC (rev 4949)
@@ -5182,6 +5182,17 @@
     except ValueError:
         return args[0]
 
+def decodeArg(arg):
+    if sys.platform=='win32' and config.console_encoding == 'cp850':
+        # Western Windows versions give parameters encoded as windows-1252
+        # even though the console encoding is cp850.
+        return unicode(arg, 'windows-1252')
+    else:
+        # Linux uses the same encoding for both.
+        # I don't know how non-Western Windows versions behave.
+        return unicode(arg, config.console_encoding)
+
+
 def handleArgs():
     """Handle standard command line arguments, return the rest as a list.
 
@@ -5189,7 +5200,6 @@
     global parameters such as -lang or -log. Returns a list of all arguments
     that are not global. This makes sure that global arguments are applied
     first, regardless of the order in which the arguments were given.
-    
     """
     global default_code, default_family, verbose
     # get commandline arguments
@@ -5201,14 +5211,7 @@
     moduleName = calledModuleName()
     nonGlobalArgs = []
     for arg in args[1:]:
-        if sys.platform=='win32' and config.console_encoding == 'cp850':
-            # Western Windows versions give parameters encoded as windows-1252
-            # even though the console encoding is cp850.
-            arg = unicode(arg, 'windows-1252')
-        else:
-            # Linux uses the same encoding for both.
-            # I don't know how non-Western Windows versions behave.
-            arg = unicode(arg, config.console_encoding)
+        arg = decodeArg(arg)
         if arg == '-help':
             showHelp(moduleName)
             sys.exit(0)
@@ -5464,6 +5467,26 @@
         result += diff[i]
     output(result)
 
+def writeToCommandLogFile():
+    """
+    Save the name of the called module along with all parameters to
+    logs/commands.log so that the user can look it up later to track errors
+    or report bugs.
+    """
+    # put quotation marks around all parameters
+    args = [decodeArg(sys.argv[0])] + map(lambda s: decodeArg('"%s"' % s), sys.argv[1:])
+    commandLogFilename = config.datafilepath('logs', 'commands.log')
+    try:
+        commandLogFile = codecs.open(commandLogFilename, 'a', 'utf-8')
+    except IOError:
+        commandLogFile = codecs.open(commandLogFilename, 'w', 'utf-8')
+    # add a timestamp in ISO 8601 formulation
+    isoDate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
+    commandLogFile.write(isoDate + ' ')
+    s = u' '.join(args)
+    commandLogFile.write(s + '\n')
+    commandLogFile.close()
+
 def setLogfileStatus(enabled, logname = None):
     global logfile
     if enabled:
@@ -5481,6 +5504,8 @@
 if '*' in config.log or calledModuleName() in config.log:
     setLogfileStatus(True)
 
+writeToCommandLogFile()
+
 colorTagR = re.compile('\03{.*?}', re.UNICODE)
 
 def log(text):





More information about the Pywikipedia-l mailing list