http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10036
Revision: 10036 Author: valhallasw Date: 2012-03-21 21:31:05 +0000 (Wed, 21 Mar 2012) Log Message: ----------- New -overwrite=[yes|no] option for djvutext
Patch by Lars Aronsson aronsson@users.sourceforge.net Reference: pywikipediabot-Patches-3502676
Modified Paths: -------------- trunk/pywikipedia/djvutext.py
Modified: trunk/pywikipedia/djvutext.py =================================================================== --- trunk/pywikipedia/djvutext.py 2012-03-21 17:48:34 UTC (rev 10035) +++ trunk/pywikipedia/djvutext.py 2012-03-21 21:31:05 UTC (rev 10036) @@ -10,6 +10,9 @@ what would have been changed. -ask Ask for confirmation before uploading each page. (Default: ask when overwriting pages) + -overwrite:no When asking for confirmation, the answer is no. + -overwrite:yes When asking for confirmation, the answer is yes. + (Default: ask for the answer) -djvu:... Filename of the djvu file -index:... Name of the index page (Default: the djvu filename) @@ -37,7 +40,7 @@
class DjVuTextBot:
- def __init__(self, djvu, index, pages, ask=False, debug=False): + def __init__(self, djvu, index, pages, ask=False, overwrite='ask', debug=False): """ Constructor. Parameters: djvu : filename @@ -49,6 +52,7 @@ self.pages = pages self.dry = debug self.ask = ask + self.overwrite = overwrite
def NoOfImages(self): cmd = u"djvused -e 'n' "%s"" % (self.djvu) @@ -154,8 +158,15 @@ pywikibot.inputChoice(u'Dry mode... Press enter to continue', [], [], 'dummy') return - if ask: - choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No'], ['y', 'N'], 'N') + if ask: # True either when the -ask flag is used or if the page exists + if self.overwrite == 'n': + choice = 'n' + pywikibot.output(u"You did not accept these changes") + elif self.overwrite == 'y': + choice = 'y' + pywikibot.output(u"You accepted these changes") + else: + choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No'], ['y', 'N'], 'N') else: choice = 'y' if choice == 'y': @@ -179,6 +190,7 @@ # what would have been changed. dry = False ask = False + overwrite = 'ask'
# Parse command line arguments for arg in pywikibot.handleArgs(): @@ -186,6 +198,11 @@ dry = True elif arg.startswith("-ask"): ask = True + elif arg.startswith("-overwrite:"): + overwrite = arg[11:12] + if overwrite != 'y' and overwrite != 'n': + pywikibot.output(u"Unknown argument %s; will ask before overwriting" % arg) + overwrite = 'ask' elif arg.startswith("-djvu:"): djvu = arg[6:] elif arg.startswith("-index:"): @@ -219,7 +236,7 @@ raise pywikibot.NoPage(u"Page '%s' does not exist" % index) pywikibot.output(u"uploading text from %s to %s" % (djvu, index_page.title(asLink=True)) ) - bot = DjVuTextBot(djvu, index, pages, ask, dry) + bot = DjVuTextBot(djvu, index, pages, ask, overwrite, dry) if not bot.has_text(): raise ValueError("No text layer in djvu file") bot.run()
pywikipedia-svn@lists.wikimedia.org