http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11027
Revision: 11027
Author: yurik
Date: 2013-02-04 07:36:21 +0000 (Mon, 04 Feb 2013)
Log Message:
-----------
Applied atdt patch - script info in the user agent
http://sourceforge.net/tracker/?func=detail&aid=3603143&group_id=93…
Modified Paths:
--------------
trunk/pywikipedia/pywikibot/version.py
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/pywikibot/version.py
===================================================================
--- trunk/pywikipedia/pywikibot/version.py 2013-02-02 13:08:58 UTC (rev 11026)
+++ trunk/pywikipedia/pywikibot/version.py 2013-02-04 07:36:21 UTC (rev 11027)
@@ -10,6 +10,7 @@
__version__ = '$Id$'
import os
+import sys
import time
import urllib
@@ -134,3 +135,16 @@
exec(line)
break
return __version__
+
+## Get the tail path component and file name of the currently executing
+# script. Returns a tuple.
+#
+def get_executing_script():
+ """Get the last path component and filename of the currently
+ executing script."""
+ try:
+ script = os.path.abspath(sys.modules['__main__'].__file__)
+ except (KeyError, AttributeError):
+ script = sys.executable
+ path, file = os.path.split(script)
+ return (os.path.basename(path), file)
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2013-02-02 13:08:58 UTC (rev 11026)
+++ trunk/pywikipedia/wikipedia.py 2013-02-04 07:36:21 UTC (rev 11027)
@@ -45,7 +45,10 @@
setAction(text): Use 'text' instead of "Wikipedia python library"
in
edit summaries
setUserAgent(text): Sets the string being passed to the HTTP server as
- the User-agent: header. Defaults to 'Pywikipediabot/1.0'.
+ the User-agent: header. The default is
+ '<script>/<revision> Pywikipediabot/1.0', where
'<script>' is the tail
+ path component and file name of the currently executing script and
+ revision is the SVN revision of Pywikipediabot.
output(text): Prints the text 'text' in the encoding of the user's
console. **Use this instead of "print" statements**
@@ -161,6 +164,9 @@
WIDEBUILD = False
+# Format string for the default user agent.
+USER_AGENT_FORMAT = '{script}/r{version[rev]} Pywikipediabot/1.0'
+
SaxError = xml.sax._exceptions.SAXParseException
# Pre-compile re expressions
@@ -5208,7 +5214,10 @@
useragent = s
# Default User-agent
-setUserAgent('PythonWikipediaBot/1.0')
+setUserAgent(USER_AGENT_FORMAT.format(
+ script=('-'.join(version.get_executing_script())),
+ version=version.getversiondict()
+))
def url2link(percentname, insite, site):
"""Convert urlname of a wiki page into interwiki link format.