Skip to content

Commit 0078d5c

Browse files
authored
Merge pull request #2205 from sopel-irc/web.decode-cleanup
tools, tools.web: rework `web.decode()` to use modern stdlib
2 parents 4f5be5a + 546dc44 commit 0078d5c

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

docs/source/api.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ Additional API features
44
Sopel includes a number of additional functions that are useful for various
55
common IRC tasks.
66

7-
Note that ``sopel.web`` was deprecated in 6.2.0, and is not included in this
8-
documentation; it will be removed completely in Sopel 8. Plugins should use
9-
`requests <https://github.com/psf/requests>`_ directly.
10-
117
sopel.tools
128
-----------
139

sopel/tools/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from sopel import __version__
3030

3131
from ._events import events # NOQA
32-
from . import time, web # NOQA
3332

3433

3534
# Kept for backward compatibility
@@ -199,6 +198,13 @@ def deprecated_func(*args, **kwargs):
199198
return deprecated_func
200199

201200

201+
# This has to be *after* the definition of `deprecated()` or we
202+
# get "AttributeError: partially initialized module 'sopel.tools' has no
203+
# attribute 'deprecated' (most likely due to a circular import)" when trying
204+
# to use the decorator in submodules.
205+
from . import time, web # NOQA
206+
207+
202208
@deprecated('Shim for Python 2 cross-compatibility, no longer needed. '
203209
'Use built-in `input()` instead.',
204210
version='7.1',

sopel/tools/web.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
applications, APIs, or websites in your plugins.
44
55
.. versionadded:: 7.0
6-
7-
.. note::
8-
Some parts of this module will remain accessible through ``sopel.web`` as
9-
well until its final removal in Sopel 8. This is for backward
10-
compatibility only; please update old code as soon as possible.
116
"""
127
# Copyright © 2008, Sean B. Palmer, inamidst.com
138
# Copyright © 2009, Michael Yanovich <[email protected]>
@@ -18,12 +13,13 @@
1813

1914
from __future__ import generator_stop
2015

16+
import html
2117
from html.entities import name2codepoint
2218
import re
2319
import urllib
2420
from urllib.parse import urlparse, urlunparse
2521

26-
from sopel import __version__
22+
from sopel import __version__, tools
2723

2824

2925
__all__ = [
@@ -85,9 +81,19 @@
8581

8682

8783
r_entity = re.compile(r'&([^;\s]+);')
88-
"""Regular expression to match HTML entities."""
84+
"""Regular expression to match HTML entities.
85+
86+
.. deprecated:: 8.0
87+
88+
Will be removed in Sopel 9, along with :func:`entity`.
89+
"""
8990

9091

92+
@tools.deprecated(
93+
version='8.0',
94+
removed_in='9.0',
95+
reason="No longer needed now that Python 3.4+ has `html.unescape()`",
96+
)
9197
def entity(match):
9298
"""Convert an entity reference to the appropriate character.
9399
@@ -96,6 +102,12 @@ def entity(match):
96102
:return str: the Unicode character corresponding to the given ``match``
97103
string, or a fallback representation if the reference cannot be
98104
resolved to a character
105+
106+
.. deprecated:: 8.0
107+
108+
Will be removed in Sopel 9. Use :func:`decode` directly or migrate to
109+
Python's standard-library equivalent, :func:`html.unescape`.
110+
99111
"""
100112
value = match.group(1).lower()
101113
if value.startswith('#x'):
@@ -107,13 +119,20 @@ def entity(match):
107119
return '[' + value + ']'
108120

109121

110-
def decode(html):
122+
def decode(text):
111123
"""Decode HTML entities into Unicode text.
112124
113-
:param str html: the HTML page or snippet to process
114-
:return str: ``html`` with all entity references replaced
125+
:param str text: the HTML page or snippet to process
126+
:return str: ``text`` with all entity references replaced
127+
128+
.. versionchanged:: 8.0
129+
130+
Renamed ``html`` parameter to ``text``. (Python gained a standard
131+
library module named :mod:`html` in version 3.4.)
132+
115133
"""
116-
return r_entity.sub(entity, html)
134+
# TODO deprecated?
135+
return html.unescape(text)
117136

118137

119138
def quote(string, safe='/'):

0 commit comments

Comments
 (0)