Skip to content

Commit

Permalink
add an extra test for inner tags that should generate separate matches
Browse files Browse the repository at this point in the history
  • Loading branch information
beardypig committed Jul 31, 2018
1 parent 3768e61 commit e449aa3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/streamlink/utils/__init__.py
Expand Up @@ -143,6 +143,65 @@ def rtmpparse(url):
return tcurl, playpath


def memoize(obj):
cache = obj.cache = {}

@functools.wraps(obj)
def memoizer(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = obj(*args, **kwargs)
return cache[key]
return memoizer


def search_dict(data, key):
"""
Search for a key in a nested dict, or list of nested dicts, and return the values.
:param data: dict/list to search
:param key: key to find
:return: matches for key
"""
if isinstance(data, dict):
for dkey, value in data.items():
if dkey == key:
yield value
for result in search_dict(value, key):
yield result
elif isinstance(data, list):
for value in data:
for result in search_dict(value, key):
yield result


def load_module(name, path=None):
if is_py3:
import importlib.machinery
import importlib.util
import sys

loader_details = [(importlib.machinery.SourceFileLoader, importlib.machinery.SOURCE_SUFFIXES)]
finder = importlib.machinery.FileFinder(path, *loader_details)
spec = finder.find_spec(name)
if not spec or not spec.loader:
raise ImportError("no module named {0}".format(name))
if sys.version_info[1] > 4:
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
else:
return spec.loader.load_module(name)

else:
import imp
fd, filename, desc = imp.find_module(name, path and [path])
try:
return imp.load_module(name, fd, filename, desc)
finally:
if fd:
fd.close()

#####################################
# Deprecated functions, do not use. #
#####################################
Expand Down Expand Up @@ -246,4 +305,4 @@ def escape_librtmp(value): # pragma: no cover
"verifyjson", "absolute_url", "parse_qsd", "parse_json", "res_json",
"parse_xml", "res_xml", "rtmpparse", "prepend_www", "NamedPipe",
"escape_librtmp", "LazyFormatter", "get_filesystem_encoding",
"maybe_decode", "maybe_encode"]
"maybe_decode", "maybe_encode"]
11 changes: 11 additions & 0 deletions tests/test_plugin_utils.py
Expand Up @@ -11,6 +11,8 @@

class TestPluginUtil(unittest.TestCase):
test_html = """
<!doctype html>
<html lang="en" class="no-js">
<title>Title</title>
<meta property="og:type" content= "website" />
<meta property="og:url" content="http://test.se/"/>
Expand All @@ -19,8 +21,11 @@ class TestPluginUtil(unittest.TestCase):
<link rel="stylesheet" type="text/css" href="https://test.se/test.css">
<script>Tester.ready(function () {
alert("Hello, world!"); });</script>
<p>
<a
href="http://test.se/foo">bar</a>
</p>
</html>
"""

def test_itertags_single_text(self):
Expand Down Expand Up @@ -70,5 +75,11 @@ def test_no_end_tag(self):
"type": "text/css",
"href": "https://test.se/test.css"})

def test_tag_inner_tag(self):
links = list(itertags(self.test_html, "p"))
self.assertTrue(len(links), 1)
self.assertEqual(links[0].tag, "p")
self.assertEqual(links[0].text.strip(), '<a \nhref="http://test.se/foo">bar</a>')
self.assertEqual(links[0].attributes, {})


0 comments on commit e449aa3

Please sign in to comment.