-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.py
28 lines (24 loc) · 1.07 KB
/
utils.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
#!/usr/bin/python
ns = {'atom': 'http://www.w3.org/2005/Atom',
'espi': 'http://naesb.org/espi'}
def getEntity(source, target, accessor=None, multiple=False):
"""Extracts the named entity from the source XML tree. `accessor` is a
function of one argyment; if provided and the target entity is found, the
target will be passed into `accessor` and its result will be returned. If
`multiple` is true, the result will be all entities that match (i.e. the
function will use `finall` instead of `find`)."""
if multiple:
es = source.findall(target, ns)
if accessor:
return [ accessor(e) for e in es ]
else:
return es
else:
e = source.find(target, ns)
if e is not None and accessor is not None:
return accessor(e)
else:
return e
def getLink(source, relation, multiple=False):
"""Shorthand for pulling a link with the given "rel" attribute from the source."""
return getEntity(source, './atom:link[@rel="%s"]' % relation, lambda e: e.attrib['href'], multiple)