jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
textlib.extract_sections: Remove footer from the last section

pywikibot.cosmetic_changes.py:
- Use list.extend instead of concatenation.
textlib_tests.py:
- Add a few tests for the bug at hand.
- Remove the `dry` attribute when inheriting from
DefaultDrySiteTestCase. The parent class sets the attributes.

Bug: T199751
Change-Id: Id67d5ab256ff6960e204fdae4c87ba7c2ff6f91e
---
M pywikibot/cosmetic_changes.py
M pywikibot/textlib.py
M tests/textlib_tests.py
3 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 6e3ff66..3130081 100755
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -702,7 +702,7 @@
- len(current_heading.lstrip('=')))
next_dep = len(next_heading) - len(next_heading.lstrip('='))
if strip_section[1].strip() or current_dep < next_dep:
- new_body = new_body + list(sections[i])
+ new_body.extend(sections[i])
return header + ''.join(new_body) + footer

def removeUselessSpaces(self, text):
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 1e29666..d63a4be 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -907,12 +907,18 @@
sections = _extract_sections(text, headings)
# Find header and footer contents
header = text[:headings[0].start] if headings else text
- last_section_contents = sections[-1].content if sections else header
cat_regex, interwiki_regex = _get_regexes(('category', 'interwiki'), site)
langlink_pattern = interwiki_regex.pattern.replace(':?', '')
+ last_section_content = sections[-1].content if sections else header
footer = re.search(
r'(%s)*\Z' % r'|'.join((langlink_pattern, cat_regex.pattern, r'\s+')),
- last_section_contents).group().strip()
+ last_section_content).group().lstrip()
+ if footer:
+ if sections:
+ sections[-1] = _Section(
+ sections[-1].title, last_section_content[:-len(footer)])
+ else:
+ header = header[:-len(footer)]
return header, sections, footer


diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 078f1d6..67db849 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -15,7 +15,7 @@

import pywikibot
import pywikibot.textlib as textlib
-from pywikibot.textlib import _MultiTemplateMatchBuilder
+from pywikibot.textlib import _MultiTemplateMatchBuilder, extract_sections

from pywikibot import config, UnknownSite
from pywikibot.site import _IWEntry
@@ -143,8 +143,6 @@

"""Test category formatting."""

- dry = True
-
catresult = ('[[Category:Cat1]]%(LS)s[[Category:Cat2]]%(LS)s'
% {'LS': config.LS})

@@ -184,8 +182,6 @@
with both a newline and an empty string as separators.
"""

- dry = True
-
old = ('[[Category:Cat1]]%(LS)s[[Category:Cat2|]]%(LS)s'
'[[Category:Cat1| ]]%(LS)s[[Category:Cat2|key]]'
% {'LS': config.LS})
@@ -1411,8 +1407,6 @@

"""Test _MultiTemplateMatchBuilder."""

- dry = True
-
@classmethod
def setUpClass(cls):
"""Cache namespace 10 (Template) case sensitivity."""
@@ -1575,6 +1569,48 @@
'{{linkfa|fr}}\n', '{{linkfa|bar}}']))


+class TestExtractSections(DefaultDrySiteTestCase):
+
+ """Test the extract_sections function."""
+
+ def test_no_sections_no_footer(self):
+ """Test for text having no sections or footer."""
+ self.assertEqual(
+ extract_sections('text', self.site),
+ ('text', [], '')
+ )
+
+ def test_no_sections_with_footer(self):
+ """Test for text having footer but no section."""
+ self.assertEqual(
+ extract_sections('text\n\n[[Category:A]]', self.site),
+ ('text\n\n', [], '[[Category:A]]')
+ )
+
+ def test_with_section_no_footer(self):
+ """Test for text having sections but no footer."""
+ self.assertEqual(
+ extract_sections(
+ 'text\n\n'
+ '==title==\n'
+ 'content',
+ self.site),
+ ('text\n\n', [('==title==', '\ncontent')], '')
+ )
+
+ def test_with_section_with_footer(self):
+ """Test for text having sections and footer."""
+ self.assertEqual(
+ extract_sections(
+ 'text\n\n'
+ '==title==\n'
+ 'content\n'
+ '[[Category:A]]\n',
+ self.site),
+ ('text\n\n', [('==title==', '\ncontent\n')], '[[Category:A]]\n')
+ )
+
+
if __name__ == '__main__': # pragma: no cover
try:
unittest.main()

To view, visit change 446301. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Id67d5ab256ff6960e204fdae4c87ba7c2ff6f91e
Gerrit-Change-Number: 446301
Gerrit-PatchSet: 2
Gerrit-Owner: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444@gmail.com>
Gerrit-Reviewer: jenkins-bot