http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9552
Revision: 9552
Author: xqt
Date: 2011-09-25 17:20:50 +0000 (Sun, 25 Sep 2011)
Log Message:
-----------
minor edits for merging with trunk
Modified Paths:
--------------
branches/rewrite/pywikibot/bot.py
Modified: branches/rewrite/pywikibot/bot.py
===================================================================
--- branches/rewrite/pywikibot/bot.py 2011-09-25 15:45:24 UTC (rev 9551)
+++ branches/rewrite/pywikibot/bot.py 2011-09-25 17:20:50 UTC (rev 9552)
@@ -3,7 +3,7 @@
User-interface related functions for building bots
"""
#
-# (C) Pywikipedia bot team, 2008
+# (C) Pywikipedia bot team, 2008-2011
#
# Distributed under the terms of the MIT license.
#
@@ -252,7 +252,6 @@
text = unicode(text, 'iso8859-1')
logger.log(_level, text, extra=context, **kwargs)
-
def output(text, decoder=None, newline=True, toStdout=False, **kwargs):
"""Output a message to the user via the userinterface.
@@ -362,12 +361,13 @@
# get commandline arguments
called = sys.argv[0].strip()
if ".py" in called: # could end with .pyc, .pyw, etc. on some platforms
- called = called[ : called.rindex(".py")]
+ # clip off the '.py?' filename extension
+ called = called[:called.rindex('.py')]
return os.path.basename(called)
def _decodeArg(arg):
if sys.platform == 'win32':
- if config.console_encoding in ("cp437", 'cp850'):
+ if config.console_encoding in ('cp437', 'cp850'):
# Western Windows versions give parameters encoded as windows-1252
# even though the console encoding is cp850 or cp437.
return unicode(arg, 'windows-1252')
@@ -498,7 +498,6 @@
pywikibot.debug(u"handleArgs() completed.", _logger)
return nonGlobalArgs
-
def showHelp(name=""):
# argument, if given, is ignored
modname = calledModuleName()
@@ -508,7 +507,7 @@
except NameError:
modname = "no_module"
- globalHelp = u'''\
+ globalHelp = u'''
Global arguments available for all bots:
-dir:PATH Read the bot's configuration data from directory given by
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9551
Revision: 9551
Author: valhallasw
Date: 2011-09-25 15:45:24 +0000 (Sun, 25 Sep 2011)
Log Message:
-----------
Bugfix: make the replacement file work for windows newlines and UTF-8 BOMs
(instead of stripping the last character, strip '\uFEFF' from the left and '\r' and '\n' from the right)
Modified Paths:
--------------
trunk/pywikipedia/replace.py
Modified: trunk/pywikipedia/replace.py
===================================================================
--- trunk/pywikipedia/replace.py 2011-09-25 14:45:44 UTC (rev 9550)
+++ trunk/pywikipedia/replace.py 2011-09-25 15:45:24 UTC (rev 9551)
@@ -622,7 +622,7 @@
u"""Please enter the filename to read replacements from:""")
else:
replacefile = arg[len('-replacementfile')+1:]
- commandline_replacements.extend([x[:-1] for x in codecs.open(replacefile, 'r', 'utf-8')])
+ commandline_replacements.extend([x.lstrip(u'\uFEFF').rstrip('\r\n') for x in codecs.open(replacefile, 'r', 'utf-8')])
elif arg.startswith('-excepttitle:'):
exceptions['title'].append(arg[13:])
elif arg.startswith('-requiretitle:'):
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9550
Revision: 9550
Author: valhallasw
Date: 2011-09-25 14:45:44 +0000 (Sun, 25 Sep 2011)
Log Message:
-----------
Bugfix: use utf-8 to read in replacements from a file
Feature: show search-and-replace terms when ran with -v
Modified Paths:
--------------
trunk/pywikipedia/replace.py
Modified: trunk/pywikipedia/replace.py
===================================================================
--- trunk/pywikipedia/replace.py 2011-09-25 13:49:17 UTC (rev 9549)
+++ trunk/pywikipedia/replace.py 2011-09-25 14:45:44 UTC (rev 9550)
@@ -158,6 +158,7 @@
import editarticle
from pywikibot import i18n
import webbrowser
+import codecs
# Imports predefined replacements tasks from fixes.py
import fixes
@@ -621,7 +622,7 @@
u"""Please enter the filename to read replacements from:""")
else:
replacefile = arg[len('-replacementfile')+1:]
- commandline_replacements.extend([x[:-1] for x in open(replacefile)])
+ commandline_replacements.extend([x[:-1] for x in codecs.open(replacefile, 'r', 'utf-8')])
elif arg.startswith('-excepttitle:'):
exceptions['title'].append(arg[13:])
elif arg.startswith('-requiretitle:'):
@@ -659,6 +660,9 @@
if not genFactory.handleArg(arg):
commandline_replacements.append(arg)
+ if pywikibot.verbose:
+ pywikibot.output(u"commandline_replacements: %r" % commandline_replacements)
+
if (len(commandline_replacements) % 2):
raise pywikibot.Error, 'require even number of replacements.'
elif (len(commandline_replacements) == 2 and fix is None):
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9549
Revision: 9549
Author: valhallasw
Date: 2011-09-25 13:49:17 +0000 (Sun, 25 Sep 2011)
Log Message:
-----------
Added 'replacementfile' option, allowing replacements to be read in from a file.
Every line of the file is interpreted as if it was an extra replacement
parameter on the command line.
I.e.: file containing lines 'x' and 'y', and calling
replace.py a -replacementfile:file d
will replace 'a' with 'x' and 'y' with 'd'. This should also work with regexps, although I'm not 100% sure how they will be interpreted by open().
Modified Paths:
--------------
trunk/pywikipedia/replace.py
Modified: trunk/pywikipedia/replace.py
===================================================================
--- trunk/pywikipedia/replace.py 2011-09-25 13:33:27 UTC (rev 9548)
+++ trunk/pywikipedia/replace.py 2011-09-25 13:49:17 UTC (rev 9549)
@@ -101,6 +101,14 @@
It is possible to introduce more than one pair of old text
and replacement.
+-replacementfile Lines from the given file name(s) will be read as if they
+ were added to the command line at that point. I.e. a file
+ containing lines "a" and "b", used as
+ python replace.py -page:X -replacementfile:file c d
+ will replace 'a' with 'b' and 'c' with 'd'. However, using
+ python replace.py -page:X c -replacementfile:file d will
+ also work, and will replace 'c' with 'a' and 'b' with 'd'.
+
Examples:
If you want to change templates from the old syntax, e.g. {{msg:Stub}}, to the
@@ -607,6 +615,13 @@
u'Please enter the filename to save the titles:')
else:
filename = arg[6:]
+ elif arg.startswith('-replacementfile'):
+ if len(arg) == len('-replacementfile'):
+ replacefile = pywikibot.input(
+u"""Please enter the filename to read replacements from:""")
+ else:
+ replacefile = arg[len('-replacementfile')+1:]
+ commandline_replacements.extend([x[:-1] for x in open(replacefile)])
elif arg.startswith('-excepttitle:'):
exceptions['title'].append(arg[13:])
elif arg.startswith('-requiretitle:'):