4 from sfa.util.faults import *
6 URN_PREFIX = "urn:publicid:IDN"
10 return ".".join(parts[-1:])
12 def get_authority(hrn):
13 parts = hrn.split(".")
14 return ".".join(parts[:-1])
16 def hrn_to_pl_slicename(hrn):
17 parts = hrn.split(".")
18 return parts[-2] + "_" + parts[-1]
20 # assuming hrn is the hrn of an authority, return the plc authority name
21 def hrn_to_pl_authname(hrn):
22 parts = hrn.split(".")
25 # assuming hrn is the hrn of an authority, return the plc login_base
26 def hrn_to_pl_login_base(hrn):
27 return hrn_to_pl_authname(hrn)
29 def hostname_to_hrn(auth_hrn, login_base, hostname):
31 Convert hrn to plantelab name.
33 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
36 def slicename_to_hrn(auth_hrn, slicename):
38 Convert hrn to planetlab name.
40 parts = slicename.split("_")
41 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
45 def email_to_hrn(auth_hrn, email):
46 parts = email.split("@")
48 username = username.replace(".", "_")
49 person_hrn = ".".join([auth_hrn, username])
56 return a tuple (hrn, type)
59 # if this is already a hrn dont do anything
60 if not urn or not urn.startswith(URN_PREFIX):
63 name = urn[len(URN_PREFIX):]
64 hrn_parts = name.split("+")
66 # type is always the second to last element in the list
67 type = hrn_parts.pop(-2)
69 # convert hrn_parts (list) into hrn (str) by doing the following
70 # remove blank elements
71 # replace ':' with '.'
72 # join list elements using '.'
73 hrn = '.'.join([part.replace(':', '.') for part in hrn_parts if part])
75 return str(hrn), str(type)
78 def hrn_to_urn(hrn, type=None):
80 convert an hrn and type to a urn string
82 # if this is already a urn dont do anything
83 if not hrn or hrn.startswith(URN_PREFIX):
86 authority = get_authority(hrn)
88 urn = "+".join([unicode(part).replace('.', ':') \
89 for part in ['',authority,type,name]])
91 return URN_PREFIX + urn