jenkins-bot has submitted this change and it was merged.
Change subject: pagefromfile.py: pep257 and optimisation
......................................................................
pagefromfile.py: pep257 and optimisation
Close open hanging file.
Use bot.userPut() instead of page.save().
Used PageSaveRelatedError hierarchy.
Change-Id: Ic2da03cac8e3939549fa6bd074539213ee594907
---
M scripts/pagefromfile.py
M tox.ini
2 files changed, 25 insertions(+), 17 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
XZise: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/scripts/pagefromfile.py b/scripts/pagefromfile.py
index 98df996..dc50d89 100644
--- a/scripts/pagefromfile.py
+++ b/scripts/pagefromfile.py
@@ -1,6 +1,8 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
+Bot to upload pages from a file.
+
This bot takes its input from a file that contains a number of
pages to be put on the wiki. The pages should all have the same
begin and end text (which may not overlap).
@@ -65,18 +67,23 @@
"""No title found."""
def __init__(self, offset):
+ """Constructor."""
self.offset = offset
class PageFromFileRobot(Bot):
"""
- Responsible for writing pages to the wiki, with the titles and contents
- given by a PageFromFileReader.
+ Responsible for writing pages to the wiki.
+
+ Titles and contents are given by a PageFromFileReader.
+
"""
def __init__(self, reader, **kwargs):
+ """Constructor."""
self.availableOptions.update({
+ 'always': True,
'force': False,
'append': None,
'summary': None,
@@ -90,10 +97,12 @@
self.reader = reader
def run(self):
+ """Start file processing and upload content."""
for title, contents in self.reader.run():
self.save(title, contents)
def save(self, title, contents):
+ """Upload page content."""
mysite = pywikibot.Site()
page = pywikibot.Page(mysite, title)
@@ -145,17 +154,12 @@
if self.getOption('autosummary'):
comment = ''
config.default_edit_summary = ''
- try:
- page.text = contents
- page.save(comment, minor=self.getOption('minor'))
- except pywikibot.LockedPage:
- pywikibot.output(u"Page %s is locked; skipping." % title)
- except pywikibot.EditConflict:
- pywikibot.output(u'Skipping %s because of edit conflict' % title)
- except pywikibot.SpamfilterError as error:
- pywikibot.output(
- u'Cannot change %s because of spam blacklist entry %s'
- % (title, error.url))
+
+ self.userPut(page, page.text, contents,
+ comment=comment,
+ minor=self.getOption('minor'),
+ show_diff=False,
+ ignore_save_related_errors=True)
class PageFromFileReader:
@@ -170,11 +174,11 @@
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
@@ -187,13 +191,14 @@
"""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)
+ with codecs.open(self.filename, 'r',
+ encoding=config.textfile_encoding) as f:
+ text = f.read()
+
except IOError as err:
pywikibot.output(str(err))
raise IOError
- text = f.read()
position = 0
length = 0
while True:
@@ -214,6 +219,7 @@
yield title, contents
def findpage(self, text):
+ """Find page to work on."""
pageR = re.compile(re.escape(self.pageStartMarker) + "(.*?)" +
re.escape(self.pageEndMarker), re.DOTALL)
titleR = re.compile(re.escape(self.titleStartMarker) + "(.*?)" +
@@ -236,6 +242,7 @@
def main():
+ """Main function."""
# Adapt these to the file you are using. 'pageStartMarker' and
# 'pageEndMarker' are the beginning and end of each entry. Take text that
# should be included and does not occur elsewhere in the text.
diff --git a/tox.ini b/tox.ini
index e002106..f722b37 100644
--- a/tox.ini
+++ b/tox.ini
@@ -58,6 +58,7 @@
./scripts/harvest_template.py \
./scripts/illustrate_wikidata.py \
./scripts/newitem.py \
+ ./scripts/pagefromfile.py \
./tests/aspects.py \
./tests/deprecation_tests.py \
./tests/api_tests.py \
--
To view, visit
https://gerrit.wikimedia.org/r/166663
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ic2da03cac8e3939549fa6bd074539213ee594907
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>