4 from sfa.util.faults import *
5 URN_PREFIX = "urn:publicid:IDN"
7 def __get_hierarchy_delim_indexes(hrn):
8 # find all non escaped '.'
9 hierarchy_delim = '([a-zA-Z0-9][\.])'
10 parts = re.findall(hierarchy_delim, hrn)
11 # list of indexes for every hierarchy delimieter
14 indexes.append(hrn.index(part) + 1)
18 delim_indexes = __get_hierarchy_delim_indexes(hrn)
22 last_delim_index = delim_indexes[-1:][0] + 1
23 return hrn[last_delim_index:]
25 def get_authority(xrn):
26 hrn, type = urn_to_hrn(xrn)
27 if type and type == 'authority':
30 delim_indexes = __get_hierarchy_delim_indexes(hrn)
33 last_delim_index = delim_indexes[-1:][0]
34 return hrn[:last_delim_index]
36 def hrn_to_pl_slicename(hrn):
37 # remove any escaped no alpah numeric characters
38 #hrn = re.sub('\\\[^a-zA-Z0-9]', '', hrn)
39 hrn = re.sub(r'\\(.)', '', hrn)
40 parts = hrn.split(".")
41 return parts[-2] + "_" + parts[-1]
43 # assuming hrn is the hrn of an authority, return the plc authority name
44 def hrn_to_pl_authname(hrn):
45 # remove any escaped no alpah numeric characters
46 hrn = re.sub(r'\\(.)', '', hrn)
47 parts = hrn.split(".")
50 # assuming hrn is the hrn of an authority, return the plc login_base
51 def hrn_to_pl_login_base(hrn):
52 # remove any escaped no alpah numeric characters
53 hrn = re.sub(r'\\(.)', '', hrn)
54 return hrn_to_pl_authname(hrn)
56 def hostname_to_hrn(auth_hrn, login_base, hostname):
58 Convert hrn to plantelab name.
60 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
63 def slicename_to_hrn(auth_hrn, slicename):
65 Convert hrn to planetlab name.
67 parts = slicename.split("_")
68 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
72 def email_to_hrn(auth_hrn, email):
73 parts = email.split("@")
75 username = username.replace(".", "_").replace("+", "_")
76 person_hrn = ".".join([auth_hrn, username])
83 return a tuple (hrn, type)
86 # if this is already a hrn dont do anything
87 if not urn or not urn.startswith(URN_PREFIX):
90 name = urn[len(URN_PREFIX):]
91 urn_parts = name.split("+")
92 type = urn_parts.pop(2)
95 # Remove the authority name (e.g. '.sa')
96 if type == 'authority':
97 urn_parts = hrn_parts[:-1]
99 # convert hrn_parts (list) into hrn (str) by doing the following
100 # 1. remove blank elements
101 # 2. escape all non alpha numeric chars (excluding ':')
102 # 3. replace ':' with '.' (':' is the urn hierarchy delimiter)
103 # 4. join list elements using '.'
104 #hrn = '.'.join([part.replace('.', '\\.').replace(':', '.') for part in hrn_parts if part])
105 hrn = '.'.join([re.sub(r'([^a-zA-Z0-9\:])', r'\\\1', part).replace(':', '.') for part in urn_parts if part])
107 return str(hrn), str(type)
110 def hrn_to_urn(hrn, type=None):
112 convert an hrn and type to a urn string
114 # if this is already a urn dont do anything
115 if not hrn or hrn.startswith(URN_PREFIX):
118 if type == 'authority':
122 authority = get_authority(hrn)
125 # convert from hierarchy delimiter from '.' to ':'
126 authority = re.sub(r'([a-zA-Z0-9])[\.]', r'\1:', authority)
127 # unescape escaped characters
128 authority = re.sub(r'\\(.)', r'\1', authority)
131 urn = "+".join(['',authority,name])
133 urn = "+".join(['',authority,type,name])
136 return URN_PREFIX + urn