jenkins-bot has submitted this change and it was merged.
Change subject: Bug 68660: pagefromfile handles not existing file ......................................................................
Bug 68660: pagefromfile handles not existing file
Handling of not existing file added. Filename existence is checked, user can input a new filename in case or quit.
Help is shown if user quits or should IOError still occurs.
Change-Id: Ide672e331c9084c74641be7b4633ace7216ef31b --- M scripts/pagefromfile.py M tests/script_tests.py 2 files changed, 40 insertions(+), 9 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/pagefromfile.py b/scripts/pagefromfile.py index 221065e..8753db6 100644 --- a/scripts/pagefromfile.py +++ b/scripts/pagefromfile.py @@ -17,6 +17,7 @@ -start:xxx Specify the text that marks the beginning of a page -end:xxx Specify the text that marks the end of a page -file:xxx Give the filename we are getting our material from + (default: dict.txt) -include The beginning and end markers should be included in the page. -titlestart:xxx Use xxx in place of ''' for identifying the @@ -25,7 +26,7 @@ end of page title -notitle do not include the title, including titlestart, and titleend, in the page --nocontent If page has this statment it dosen't append +-nocontent If page has this statment it doesn't append (example: -nocontent:"{{infobox") -summary:xxx Use xxx as the edit summary for the upload - if a page exists, standard messages are appended @@ -49,8 +50,10 @@ __version__ = '$Id$' #
+import os import re import codecs + import pywikibot from pywikibot import config, Bot, i18n
@@ -154,9 +157,18 @@ Responsible for reading the file.
The run() method yields a (title, contents) tuple for each found page. + """ + def __init__(self, filename, pageStartMarker, pageEndMarker, titleStartMarker, titleEndMarker, include, notitle): + """Constructor. + + Check if self.file name exists. If not, ask for a new filename. + User can quit. + + """ + self.filename = filename self.pageStartMarker = pageStartMarker self.pageEndMarker = pageEndMarker @@ -166,13 +178,15 @@ self.notitle = notitle
def run(self): - pywikibot.output('Reading '%s'...' % self.filename) + """Read file and yield page title and content.""" + + pywikibot.output('\n\nReading '%s'...' % self.filename) try: f = codecs.open(self.filename, 'r', encoding=config.textfile_encoding) except IOError as err: pywikibot.output(str(err)) - return + raise IOError
text = f.read() position = 0 @@ -264,11 +278,27 @@ else: pywikibot.output(u"Disregarding unknown argument %s." % arg)
- reader = PageFromFileReader(filename, pageStartMarker, pageEndMarker, - titleStartMarker, titleEndMarker, include, - notitle) - bot = PageFromFileRobot(reader, **options) - bot.run() + failed_filename = False + while not os.path.isfile(filename): + pywikibot.output('\nFile '%s' does not exist. ' % filename) + _input = pywikibot.input( + 'Please enter the file name [q to quit]:') + if _input == 'q': + failed_filename = True + break + else: + filename = _input + + # show help text from the top of this file if reader failed + # or User quit. + if failed_filename: + pywikibot.showHelp() + else: + reader = PageFromFileReader(filename, pageStartMarker, pageEndMarker, + titleStartMarker, titleEndMarker, include, + notitle) + bot = PageFromFileRobot(reader, **options) + bot.run()
if __name__ == "__main__": main() diff --git a/tests/script_tests.py b/tests/script_tests.py index abfdcc4..b02d25c 100644 --- a/tests/script_tests.py +++ b/tests/script_tests.py @@ -66,6 +66,7 @@ 'editarticle': 'Test page\n', 'interwiki': 'Test page\n', # 'misspelling': 'q\n', # pressing 'q' doesnt work. bug 68663 + 'pagefromfile': 'q\n', 'replace': 'foo\nbar\n\n\n', # match, replacement, # Enter to begin, Enter for default summary. 'shell': '\n', # exits on end of stdin @@ -111,6 +112,7 @@ 'imageuncat': 'You have to specify the generator ', 'interwiki': 'does not exist. Skipping.', # 'Test page' does not exist 'login': 'Logged in on ', + 'pagefromfile': 'Please enter the file name', 'replace': 'Press Enter to use this default message', 'replicate_wiki': 'error: too few arguments', 'script_wui': 'Pre-loading all relevant page contents', @@ -214,7 +216,6 @@ 'data_ingestion', # bug 68611 'flickrripper', # bug 68606 (and deps) 'imagerecat', # bug 68658 - 'pagefromfile', # bug 68660 'upload', # raises custom ValueError ] or ( ((config.family != 'wikipedia' or config.mylang != 'en') and script_name == 'cfd') or
pywikibot-commits@lists.wikimedia.org