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 parts = hrn.split(".")
21 return parts[-2] + "_" + parts[-1]
23 # assuming hrn is the hrn of an authority, return the plc authority name
24 def hrn_to_pl_authname(hrn):
25 parts = hrn.split(".")
28 # assuming hrn is the hrn of an authority, return the plc login_base
29 def hrn_to_pl_login_base(hrn):
30 return hrn_to_pl_authname(hrn)
32 def hostname_to_hrn(auth_hrn, login_base, hostname):
34 Convert hrn to plantelab name.
36 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
39 def slicename_to_hrn(auth_hrn, slicename):
41 Convert hrn to planetlab name.
43 parts = slicename.split("_")
44 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
48 def email_to_hrn(auth_hrn, email):
49 parts = email.split("@")
51 username = username.replace(".", "_").replace("+", "_")
52 person_hrn = ".".join([auth_hrn, username])
59 return a tuple (hrn, type)
60 Warning: urn_to_hrn() replces some special characters that
61 are not replaced in hrn_to_urn(). Due to this,
62 urn_to_hrn() -> hrn_to_urn() may not return the original urn
65 # if this is already a hrn dont do anything
66 if not urn or not urn.startswith(URN_PREFIX):
69 name = urn[len(URN_PREFIX):]
70 hrn_parts = name.split("+")
71 type = hrn_parts.pop(2)
74 # Remove the authority name (e.g. '.sa')
75 if type == 'authority':
76 hrn_parts = hrn_parts[:-1]
78 # convert hrn_parts (list) into hrn (str) by doing the following
79 # dont allow special characters (except ':') in the site login base
80 # remove blank elements
81 # replace ':' with '.'
82 # join list elements using '.'
83 only_alphanum = re.compile('[^a-zA-Z0-9:]+')
84 valid_chars = lambda x: only_alphanum.sub('', x).replace(':', '.')
85 hrn = '.'.join([valid_chars(part) for part in hrn_parts if part])
87 return str(hrn), str(type)
90 def hrn_to_urn(hrn, type=None):
92 convert an hrn and type to a urn string
94 # if this is already a urn dont do anything
95 if not hrn or hrn.startswith(URN_PREFIX):
98 authority = get_authority(hrn)
101 if type == 'authority':
105 if authority.startswith("plc"):
107 urn = "+".join(['',authority.replace('.',':'),name])
109 urn = "+".join(['',authority.replace('.',':'),type,name])
112 urn = "+".join(['',authority,type,name])
114 return URN_PREFIX + urn