1 URN_PREFIX = "urn:publicid:IDN"
5 return ".".join(parts[-1:])
7 def get_authority(hrn):
9 return ".".join(parts[:-1])
11 def hrn_to_pl_slicename(hrn):
12 parts = hrn.split(".")
13 return parts[-2] + "_" + parts[-1]
15 # assuming hrn is the hrn of an authority, return the plc authority name
16 def hrn_to_pl_authname(hrn):
17 parts = hrn.split(".")
20 # assuming hrn is the hrn of an authority, return the plc login_base
21 def hrn_to_pl_login_base(hrn):
22 return hrn_to_pl_authname(hrn)
24 def hostname_to_hrn(auth_hrn, login_base, hostname):
26 Convert hrn to planetlab name.
28 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
31 def slicename_to_hrn(auth_hrn, slicename):
33 Convert hrn to planetlab name.
35 parts = slicename.split("_")
36 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
40 def email_to_hrn(auth_hrn, email):
41 parts = email.split("@")
43 username = username.replace(".", "_")
44 person_hrn = ".".join([auth_hrn, username])
51 return a tuple (hrn, type)
54 # if this is already a hrn dont do anything
55 if not urn or not urn.startswith(URN_PREFIX):
58 name = urn[len(URN_PREFIX):]
59 hrn_parts = name.split("+")
61 # type is always the second to last element in the list
62 type = hrn_parts.pop(-2)
64 # convert hrn_parts (list) into hrn (str) by doing the following
65 # remove blank elements
66 # replace ':' with '.'
67 # join list elements using '.'
68 hrn = '.'.join([part.replace(':', '.') for part in hrn_parts if part])
70 return str(hrn), str(type)
73 def hrn_to_urn(hrn, type=None):
75 convert an hrn and type to a urn string
77 # if this is already a urn dont do anything
78 if not hrn or hrn.startswith(URN_PREFIX):
81 authority = get_authority(hrn)
83 urn = "+".join([unicode(part).replace('.', ':') \
84 for part in ['',authority,type,name]])
86 return URN_PREFIX + urn