jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1327440?usp=email )
Change subject: textlib: Buffer GetDataHTML output
......................................................................
textlib: Buffer GetDataHTML output
Collect parser output in a list and join it on access to avoid
repeatedly copying the accumulated immutable string.
Preserve writable textdata and close semantics with regression tests.
Change-Id: I1456ebbe0d8db7a079902e295e84849aa0d8cf32
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 55 insertions(+), 6 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index dc27ebd..ddd3e54 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -675,11 +675,20 @@
self.removetags: list[str] = (removetags if removetags is not None
else ['style', 'script'])
- #: The cleaned output text collected during parsing.
- self.textdata = ''
+ self._textdata: list[str] = []
self._skiptag: str | None = None
+ @property
+ def textdata(self) -> str:
+ """Return the cleaned output text collected during parsing."""
+ return ''.join(self._textdata)
+
+ @textdata.setter
+ def textdata(self, value: str) -> None:
+ """Set the cleaned output text collected during parsing."""
+ self._textdata = [value] if value else []
+
def __call__(self, text: str) -> str:
"""Feed the parser with *text* and return cleaned :attr:`textdata`.
@@ -692,7 +701,7 @@
def close(self) -> None:
"""Clean current processing and clear :attr:`textdata`."""
- self.textdata = ''
+ self._textdata.clear()
self._skiptag = None
super().close()
@@ -705,7 +714,7 @@
:param data: The text data between HTML tags.
"""
if not self._skiptag:
- self.textdata += data
+ self._textdata.append(data)
def handle_starttag(self,
tag: str,
@@ -730,7 +739,7 @@
f' {name}' if value is None else f' {name}="{value}"'
for name, value in attrs
)
- self.textdata += f'<{tag}{attr_text}>'
+ self._textdata.append(f'<{tag}{attr_text}>')
if tag in self.removetags:
self._skiptag = tag
@@ -745,7 +754,7 @@
:param tag: The name of the closing tag.
"""
if tag in self.keeptags:
- self.textdata += f'</{tag}>'
+ self._textdata.append(f'</{tag}>')
if tag in self.removetags and tag == self._skiptag:
self._skiptag = None
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index e62544e..9530379 100755
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -631,6 +631,46 @@
self.assertEndsWith(m[0], 'foo {{bar}}')
+class TestGetDataHTML(TestCase):
+
+ """Test the GetDataHTML parser."""
+
+ net = False
+
+ def test_incremental_textdata(self) -> None:
+ """Test textdata while feeding HTML in multiple fragments."""
+ parser = textlib.GetDataHTML(keeptags=['b'])
+
+ parser.feed('<div>one <b class="important">')
+ self.assertEqual(parser.textdata,
+ 'one <b class="important">')
+
+ parser.feed('two</b><!-- hidden --></div>')
+ self.assertEqual(parser.textdata,
+ 'one <b class="important">two</b>')
+
+ parser.close()
+ self.assertEqual(parser.textdata, '')
+
+ def test_textdata_assignment(self) -> None:
+ """Test assigning textdata before feeding additional HTML."""
+ parser = textlib.GetDataHTML()
+ parser.textdata = 'prefix: '
+
+ parser.feed('<i>value</i>')
+
+ self.assertEqual(parser.textdata, 'prefix: value')
+
+ def test_callable_clears_textdata(self) -> None:
+ """Test callable output remains available after automatic close."""
+ parser = textlib.GetDataHTML(keeptags=['b'])
+
+ result = parser('<div>one <b>two</b></div>')
+
+ self.assertEqual(result, 'one <b>two</b>')
+ self.assertEqual(parser.textdata, '')
+
+
class TestDisabledParts(DefaultSiteTestCase):
"""Test the removeDisabledParts function in textlib."""
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1327440?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I1456ebbe0d8db7a079902e295e84849aa0d8cf32
Gerrit-Change-Number: 1327440
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]