-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.py
85 lines (64 loc) · 2.32 KB
/
misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Misc functions that provide commonly-used functionalities."""
import re
import logging
LOG = logging.getLogger(__name__)
def get_xpath_attrib(node, xpath, attrib, default=None):
"""Returns the value of an attribute of the first node selected by an XPath
query.
If multiple nodes that match the XPath are found then a warning will be
printed.
Arguments:
node: node to perform the XPath query on.
xpath: XPath query.
attrib: attribute to look up.
default: value to return if no node is found or the attribute is missing.
"""
nodes = node.xpath(xpath)
if isinstance(nodes, list):
if nodes:
if len(nodes) > 1:
LOG.warning('More than one node matched xpath %s', xpath)
return nodes[0].get(attrib, default)
return default
return nodes.get(attrib, default)
def get_xpath_attribs(node, xpath, default=None):
"""Returns the attributes dict of the first node selected by an XPath
query.
If multiple nodes that match the XPath are found then a warning will be
printed.
Arguments:
node: node to perform the XPath query on.
xpath: XPath query.
default: value to return if no node is found or the attribute is missing.
"""
nodes = node.xpath(xpath)
if isinstance(nodes, list):
if nodes:
if len(nodes) > 1:
LOG.warning('More than one node matched xpath %s', xpath)
return nodes[0].attrib
return default
return nodes.attrib
def find_nodes_with_tag(root_node, xpath, tag):
"""Performs an XPath query on a node and yields nodes that have a 'tags'
attribute that contains the given tag.
Arguments:
root_node: node to perform the XPath query on.
xpath: XPath query.
tag: requested tag.
"""
# regex that matches the tag in a tags string
tag_re = re.compile(r'\b{}\b'.format(tag))
for node in root_node.xpath(xpath):
if tag_re.search(node.get('tags', '')):
yield node
def get_path_in_ext(path, ext_name):
"""Transform a game path relative to an extension root into a game path
relative to the game root.
Arguments:
path: game path relative to the extension path
ext_name: extension name
"""
if ext_name:
return "/extensions/{}/{}".format(ext_name, path)
return path