2 # WARNING - This file should be soon deprecated in favor of sfa.util.xrn
5 # WARNING - This file should be soon deprecated in favor of sfa.util.xrn
8 # WARNING - This file should be soon deprecated in favor of sfa.util.xrn
11 # WARNING - This file should be soon deprecated in favor of sfa.util.xrn
14 # WARNING - This file should be soon deprecated in favor of sfa.util.xrn
17 # WARNING - This file should be soon deprecated in favor of sfa.util.xrn
20 from sfa.util.faults import *
21 URN_PREFIX = "urn:publicid:IDN"
23 def __get_hierarchy_delim_indexes(hrn):
24 # find all non escaped '.'
25 hierarchy_delim = '([a-zA-Z0-9][\.])'
26 parts = re.findall(hierarchy_delim, hrn)
27 # list of indexes for every hierarchy delimieter
30 indexes.append(hrn.index(part) + 1)
34 delim_indexes = __get_hierarchy_delim_indexes(hrn)
38 last_delim_index = delim_indexes[-1:][0] + 1
39 return hrn[last_delim_index:]
41 def get_authority(xrn):
42 hrn, type = urn_to_hrn(xrn)
43 if type and type == 'authority':
46 delim_indexes = __get_hierarchy_delim_indexes(hrn)
49 last_delim_index = delim_indexes[-1:][0]
50 return hrn[:last_delim_index]
52 def hrn_to_pl_slicename(hrn):
53 # remove any escaped no alpah numeric characters
54 #hrn = re.sub('\\\[^a-zA-Z0-9]', '', hrn)
55 hrn = re.sub(r'\\(.)', '', hrn)
56 parts = hrn.split(".")
57 return parts[-2] + "_" + parts[-1]
59 # assuming hrn is the hrn of an authority, return the plc authority name
60 def hrn_to_pl_authname(hrn):
61 # remove any escaped no alpah numeric characters
62 hrn = re.sub(r'\\(.)', '', hrn)
63 parts = hrn.split(".")
66 # assuming hrn is the hrn of an authority, return the plc login_base
67 def hrn_to_pl_login_base(hrn):
68 # remove any escaped no alpah numeric characters
69 hrn = re.sub(r'\\(.)', '', hrn)
70 return hrn_to_pl_authname(hrn)
72 def hostname_to_hrn(auth_hrn, login_base, hostname):
74 Convert hrn to plantelab name.
76 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
79 def slicename_to_hrn(auth_hrn, slicename):
81 Convert hrn to planetlab name.
83 parts = slicename.split("_")
84 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
88 def email_to_hrn(auth_hrn, email):
89 parts = email.split("@")
91 username = username.replace(".", "_").replace("+", "_")
92 person_hrn = ".".join([auth_hrn, username])
99 return a tuple (hrn, type)
102 # if this is already a hrn dont do anything
103 if not urn or not urn.startswith(URN_PREFIX):
106 name = urn[len(URN_PREFIX):]
107 urn_parts = name.split("+")
108 type = urn_parts.pop(2)
111 # Remove the authority name (e.g. '.sa')
112 if type == 'authority':
113 urn_parts = urn_parts[:-1]
115 # convert hrn_parts (list) into hrn (str) by doing the following
116 # 1. remove blank elements
117 # 2. escape all non alpha numeric chars (excluding ':')
118 # 3. replace ':' with '.' (':' is the urn hierarchy delimiter)
119 # 4. join list elements using '.'
120 #hrn = '.'.join([part.replace('.', '\\.').replace(':', '.') for part in hrn_parts if part])
121 # hrn = '.'.join([re.sub(r'([^a-zA-Z0-9\:])', r'\\\1', part).replace(':', '.') for part in urn_parts if part])
122 hrn = '.'.join([re.sub(r'\\.', r'\\.', part).replace(':', '.') for part in urn_parts if part])
124 return str(hrn), str(type)
127 def hrn_to_urn(hrn, type=None):
129 convert an hrn and type to a urn string
131 # if this is already a urn dont do anything
132 if not hrn or hrn.startswith(URN_PREFIX):
135 if type == 'authority':
139 authority = get_authority(hrn)
142 # convert from hierarchy delimiter from '.' to ':'
143 authority = re.sub(r'([a-zA-Z0-9])[\.]', r'\1:', authority)
144 # unescape escaped characters
145 authority = re.sub(r'\\(.)', r'\1', authority)
148 urn = "+".join(['',authority,name])
150 urn = "+".join(['',authority,type,name])
153 return URN_PREFIX + urn