4 from sfa.util.faults import *
5 URN_PREFIX = "urn:publicid:IDN"
9 return ".".join(parts[-1:])
11 def get_authority(xrn):
12 hrn, type = urn_to_hrn(xrn)
13 if type and type == 'authority':
16 parts = hrn.split(".")
17 return ".".join(parts[:-1])
19 def hrn_to_pl_slicename(hrn):
20 # remove any escaped no alpah numeric characters
21 #hrn = re.sub('\\\[^a-zA-Z0-9]', '', hrn)
22 # remove any escaped '.' (i.e. '\.')
23 hrn.replace('\\.', '')
24 parts = hrn.split(".")
25 return parts[-2] + "_" + parts[-1]
27 # assuming hrn is the hrn of an authority, return the plc authority name
28 def hrn_to_pl_authname(hrn):
29 # remove any escaped '.' (i.e. '\.')
30 hrn.replace('\\.', '')
31 parts = hrn.split(".")
34 # assuming hrn is the hrn of an authority, return the plc login_base
35 def hrn_to_pl_login_base(hrn):
36 # remove any escaped '.' (i.e. '\.')
37 hrn.replace('\\.', '')
38 return hrn_to_pl_authname(hrn)
40 def hostname_to_hrn(auth_hrn, login_base, hostname):
42 Convert hrn to plantelab name.
44 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
47 def slicename_to_hrn(auth_hrn, slicename):
49 Convert hrn to planetlab name.
51 parts = slicename.split("_")
52 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
56 def email_to_hrn(auth_hrn, email):
57 parts = email.split("@")
59 username = username.replace(".", "_").replace("+", "_")
60 person_hrn = ".".join([auth_hrn, username])
67 return a tuple (hrn, type)
70 # if this is already a hrn dont do anything
71 if not urn or not urn.startswith(URN_PREFIX):
74 name = urn[len(URN_PREFIX):]
75 hrn_parts = name.split("+")
76 type = hrn_parts.pop(2)
79 # Remove the authority name (e.g. '.sa')
80 if type == 'authority':
81 hrn_parts = hrn_parts[:-1]
83 # convert hrn_parts (list) into hrn (str) by doing the following
84 # 1. remove blank elements
85 # 2. escape '.' # '.' exists in protogeni object names and are not delimiters
86 # 3. replace ':' with '.' # ':' is the urn hierarchy delimiter
87 # 4. join list elements using '.'
88 hrn = '.'.join([part.replace('.', '\.').replace(':', '.') for part in hrn_parts if part])
90 return str(hrn), str(type)
93 def hrn_to_urn(hrn, type=None):
95 convert an hrn and type to a urn string
97 # if this is already a urn dont do anything
98 if not hrn or hrn.startswith(URN_PREFIX):
101 if type == 'authority':
105 authority = get_authority(hrn)
108 # We have to do the following conversion
109 # '\\.' -> '.' # where '.' belongs in the urn name
110 # '.' -> ':" # where ':' is the urn hierarchy delimiter
111 # by doing the following
112 # 1. split authority around '\\.'
113 # 2. replace '.' with ':' in all parts
114 # 3. join parts around '.'
115 parts = authority.split('\\.')
116 authority = '.'.join([part.replace('.', ':') for part in parts])
119 urn = "+".join(['',authority,name])
121 urn = "+".join(['',authority,type,name])
124 return URN_PREFIX + urn