diff options
author | Sergey M․ <dstftw@gmail.com> | 2015-08-01 20:22:13 +0600 |
---|---|---|
committer | Sergey M․ <dstftw@gmail.com> | 2015-08-01 20:22:13 +0600 |
commit | ee114368ad0bb9822449295910263a99f9de4e1e (patch) | |
tree | 06a14cd6cbf2939f199fb80941f74c4a0841c48e /youtube_dl | |
parent | 525a87f58ee9c4ee91b5b0384184dabf6d87eef3 (diff) |
[utils] Make value optional for find_xpath_attr
This allows selecting particular attributes by name but without specifying the value and similar to xpath syntax `[@attrib]`
Diffstat (limited to 'youtube_dl')
-rw-r--r-- | youtube_dl/utils.py | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 88f9f9070..78dc2b449 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -139,21 +139,24 @@ def write_json_file(obj, fn): if sys.version_info >= (2, 7): - def find_xpath_attr(node, xpath, key, val): + def find_xpath_attr(node, xpath, key, val=None): """ Find the xpath xpath[@key=val] """ assert re.match(r'^[a-zA-Z-]+$', key) - assert re.match(r'^[a-zA-Z0-9@\s:._-]*$', val) - expr = xpath + "[@%s='%s']" % (key, val) + if val: + assert re.match(r'^[a-zA-Z0-9@\s:._-]*$', val) + expr = xpath + ('[@%s]' % key if val is None else "[@%s='%s']" % (key, val)) return node.find(expr) else: - def find_xpath_attr(node, xpath, key, val): + def find_xpath_attr(node, xpath, key, val=None): # Here comes the crazy part: In 2.6, if the xpath is a unicode, # .//node does not match if a node is a direct child of . ! if isinstance(xpath, compat_str): xpath = xpath.encode('ascii') for f in node.findall(xpath): - if f.attrib.get(key) == val: + if key not in f.attrib: + continue + if val is None or f.attrib.get(key) == val: return f return None |