File: OpenID.py

package info (click to toggle)
python-pyrdfa 3.5.2%2B20220621~ds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 584 kB
  • sloc: python: 5,386; makefile: 4; sh: 2
file content (64 lines) | stat: -rwxr-xr-x 2,119 bytes parent folder | download | duplicates (6)
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
# -*- coding: utf-8 -*-
"""
Simple transfomer: handle OpenID elements. Ie: an openid namespace is added and the usual
'link' elements for openid are exchanged against a namespaced version.

@summary: OpenID transformer module.
@requires: U{RDFLib package<http://rdflib.net>}
@organization: U{World Wide Web Consortium<http://www.w3.org>}
@author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
@license: This software is available for use under the
U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">}
@contact: Ivan Herman, ivan@w3.org
@var OPENID_NS: the OpenID URI used in the package
"""

"""
$Id: OpenID.py,v 1.4 2012-01-18 14:16:44 ivan Exp $
$Date: 2012-01-18 14:16:44 $
"""

OPENID_NS = "http://xmlns.openid.net/auth#"


def OpenID_transform(html, options, state) :
	"""
	Replace C{openid.XXX} type C{@rel} attribute values in C{<link>} elements by C{openid:XXX}. The openid URI is also
	added to the top level namespaces with the C{openid:} local name.

	@param html: a DOM node for the top level html element
	@param options: invocation options
	@type options: L{Options<pyRdfa.options>}
	@param state: top level execution state
	@type state: L{State<pyRdfa.state>}
	"""
	from ..host import HostLanguage
	if not( options.host_language in [ HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5 ] ) :
		return

	# the head element is necessary; to be sure, the namespaces are set
	# on that level only
	head = None
	try :
		head = html.getElementsByTagName("head")[0]
	except :
		# no head....
		return

	foundOpenId = False
	for link in html.getElementsByTagName("link") :
		if link.hasAttribute("rel") :
			rel = link.getAttribute("rel")
			newProp = ""
			for n in rel.strip().split() :
				if n.startswith("openid.") :
					newProp += " " + n.replace("openid.","openid:")
					foundOpenId = True
				else :
					newProp += " " + n
			link.setAttribute("rel",newProp.strip())

	# Add the OpenId namespace if necessary
	if foundOpenId and not head.hasAttribute("xmlns:openid") :
		head.setAttributeNS("", "xmlns:openid", OPENID_NS)