when computing hrns, try to use SFA code first, otherwise resort to a local version
[plcapi.git] / PLC / Namespace.py
1 #
2 # Thierry, April 2013
3
4 # This file here is the connection to the SFA code
5 # at some point in time we had duplicated this from sfa
6 # but of course both versions have entirely diverged since then
7 # in addition we really only need 2 hepler functions here, that allow to maintain 
8 # hrn for nodes and persons and that is all
9 #
10 # So in order to avoid such situations in the future, 
11 # we try to import and re-use the SFA code
12 # assumption being that if these hrn's are of importance then it makes sense to
13 # require people to have sfa-plc installed as well
14 # however we do not want this requirement to break myplc in case it is not fulfilled
15
16
17 #################### try to import from sfa
18 try:
19     from sfa.planetlab.plxrn import hostname_to_hrn
20 except:
21     hostname_to_hrn=None
22
23 try:
24     from sfa.planetlab.plxrn import email_to_hrn
25 except:
26     email_to_hrn=None
27
28 #################### if not found, bring our own local version
29 import re
30 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
31
32 if hostname_to_hrn is None:
33     def hostname_to_hrn (auth_hrn, login_base, hostname):
34         return ".".join( [ auth_hrn, login_base, escape(hostname) ] )
35
36 if email_to_hrn is None:
37     def email_to_hrn (auth_hrn, email):
38         return '.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])