2 from sfa.util.faults import *
3 URN_PREFIX = "urn:publicid:IDN"
7 return ".".join(parts[-1:])
9 def get_authority(xrn):
10 hrn, type = urn_to_hrn(xrn)
11 if type and type == 'authority':
14 parts = hrn.split(".")
15 return ".".join(parts[:-1])
17 def hrn_to_pl_slicename(hrn):
18 # remove any escaped no alpah numeric characters
19 #hrn = re.sub('\\\[^a-zA-Z0-9]', '', hrn)
20 # remove any escaped '.' (i.e. '\.')
21 hrn = hrn.replace('\\.', '')
22 parts = hrn.split(".")
23 return parts[-2] + "_" + parts[-1]
25 # assuming hrn is the hrn of an authority, return the plc authority name
26 def hrn_to_pl_authname(hrn):
27 # remove any escaped '.' (i.e. '\.')
28 hrn = hrn.replace('\\.', '')
29 parts = hrn.split(".")
32 # assuming hrn is the hrn of an authority, return the plc login_base
33 def hrn_to_pl_login_base(hrn):
34 # remove any escaped '.' (i.e. '\.')
35 hrn = hrn.replace('\\.', '')
36 return hrn_to_pl_authname(hrn)
38 def hostname_to_hrn(auth_hrn, login_base, hostname):
40 Convert hrn to plantelab name.
42 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
45 def slicename_to_hrn(auth_hrn, slicename):
47 Convert hrn to planetlab name.
49 parts = slicename.split("_")
50 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
54 def email_to_hrn(auth_hrn, email):
55 parts = email.split("@")
57 username = username.replace(".", "_").replace("+", "_")
58 person_hrn = ".".join([auth_hrn, username])
65 return a tuple (hrn, type)
68 # if this is already a hrn dont do anything
69 if not urn or not urn.startswith(URN_PREFIX):
72 name = urn[len(URN_PREFIX):]
73 hrn_parts = name.split("+")
74 type = hrn_parts.pop(2)
77 # Remove the authority name (e.g. '.sa')
78 if type == 'authority':
79 hrn_parts = hrn_parts[:-1]
81 # convert hrn_parts (list) into hrn (str) by doing the following
82 # 1. remove blank elements
83 # 2. escape '.' # '.' exists in protogeni object names and are not delimiters
84 # 3. replace ':' with '.' # ':' is the urn hierarchy delimiter
85 # 4. join list elements using '.'
86 hrn = '.'.join([part.replace('.', '\\.').replace(':', '.') for part in hrn_parts if part])
88 return str(hrn), str(type)
91 def hrn_to_urn(hrn, type=None):
93 convert an hrn and type to a urn string
95 # if this is already a urn dont do anything
96 if not hrn or hrn.startswith(URN_PREFIX):
99 if type == 'authority':
103 authority = get_authority(hrn)
106 # We have to do the following conversion
107 # '\\.' -> '.' # where '.' belongs in the urn name
108 # '.' -> ':" # where ':' is the urn hierarchy delimiter
109 # by doing the following
110 # 1. split authority around '\\.'
111 # 2. replace '.' with ':' in all parts
112 # 3. join parts around '.'
113 parts = authority.split('\\.')
114 authority = '.'.join([part.replace('.', ':') for part in parts])
117 urn = "+".join(['',authority,name])
119 urn = "+".join(['',authority,type,name])
122 return URN_PREFIX + urn