2 from sfa.util.faults import *
3 URN_PREFIX = "urn:publicid:IDN"
5 def __get_hierarchy_delim_indexes(hrn):
6 # find all non escaped '.'
7 hierarchy_delim = '([a-zA-Z0-9][\.])'
8 parts = re.findall(hierarchy_delim, hrn)
9 # list of indexes for every hierarchy delimieter
12 indexes.append(hrn.index(part) + 1)
16 delim_indexes = __get_hierarchy_delim_indexes(hrn)
20 last_delim_index = delim_indexes[-1:][0] + 1
21 return hrn[last_delim_index:]
23 def get_authority(xrn):
24 hrn, type = urn_to_hrn(xrn)
25 if type and type == 'authority':
28 delim_indexes = __get_hierarchy_delim_indexes(hrn)
31 last_delim_index = delim_indexes[-1:][0]
32 return hrn[:last_delim_index]
34 def hrn_to_pl_slicename(hrn):
35 # remove any escaped no alpah numeric characters
36 #hrn = re.sub('\\\[^a-zA-Z0-9]', '', hrn)
37 hrn = re.sub(r'\\(.)', '', hrn)
38 parts = hrn.split(".")
39 return parts[-2] + "_" + parts[-1]
41 # assuming hrn is the hrn of an authority, return the plc authority name
42 def hrn_to_pl_authname(hrn):
43 # remove any escaped no alpah numeric characters
44 hrn = re.sub(r'\\(.)', '', hrn)
45 parts = hrn.split(".")
48 # assuming hrn is the hrn of an authority, return the plc login_base
49 def hrn_to_pl_login_base(hrn):
50 # remove any escaped no alpah numeric characters
51 hrn = re.sub(r'\\(.)', '', hrn)
52 return hrn_to_pl_authname(hrn)
54 def hostname_to_hrn(auth_hrn, login_base, hostname):
56 Convert hrn to plantelab name.
58 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
61 def slicename_to_hrn(auth_hrn, slicename):
63 Convert hrn to planetlab name.
65 parts = slicename.split("_")
66 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
70 def email_to_hrn(auth_hrn, email):
71 parts = email.split("@")
73 username = username.replace(".", "_").replace("+", "_")
74 person_hrn = ".".join([auth_hrn, username])
81 return a tuple (hrn, type)
84 # if this is already a hrn dont do anything
85 if not urn or not urn.startswith(URN_PREFIX):
88 name = urn[len(URN_PREFIX):]
89 urn_parts = name.split("+")
90 type = urn_parts.pop(2)
93 # Remove the authority name (e.g. '.sa')
94 if type == 'authority':
95 urn_parts = urn_parts[:-1]
97 # convert hrn_parts (list) into hrn (str) by doing the following
98 # 1. remove blank elements
99 # 2. escape all non alpha numeric chars (excluding ':')
100 # 3. replace ':' with '.' (':' is the urn hierarchy delimiter)
101 # 4. join list elements using '.'
102 #hrn = '.'.join([part.replace('.', '\\.').replace(':', '.') for part in hrn_parts if part])
103 hrn = '.'.join([re.sub(r'([^a-zA-Z0-9\:])', r'\\\1', part).replace(':', '.') for part in urn_parts if part])
105 return str(hrn), str(type)
108 def hrn_to_urn(hrn, type=None):
110 convert an hrn and type to a urn string
112 # if this is already a urn dont do anything
113 if not hrn or hrn.startswith(URN_PREFIX):
116 if type == 'authority':
120 authority = get_authority(hrn)
123 # convert from hierarchy delimiter from '.' to ':'
124 authority = re.sub(r'([a-zA-Z0-9])[\.]', r'\1:', authority)
125 # unescape escaped characters
126 authority = re.sub(r'\\(.)', r'\1', authority)
129 urn = "+".join(['',authority,name])
131 urn = "+".join(['',authority,type,name])
134 return URN_PREFIX + urn