1 # ----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
22 # ----------------------------------------------------------------------
26 from sfa.util.faults import SfaAPIError
28 # for convenience and smoother translation - we should get rid of these
29 # functions eventually
33 return Xrn(hrn).get_leaf()
36 def get_authority(hrn):
37 return Xrn(hrn).get_authority_hrn()
42 return (xrn.hrn, xrn.type)
45 def hrn_to_urn(hrn, type): return Xrn(hrn, type=type).urn
48 def hrn_authfor_hrn(parenthrn, hrn):
49 return Xrn.hrn_is_auth_for_hrn(parenthrn, hrn)
55 # split a HRN-like string into pieces
56 # this is like split('.') except for escaped (backslashed) dots
57 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
60 return [x.replace('--sep--', '\\.')
61 for x in hrn.replace('\\.', '--sep--').split('.')]
63 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
66 return Xrn.hrn_split(hrn)[-1]
68 # e.g. hrn_auth_list ('a\.b.c.d') -> ['a\.b', 'c']
70 def hrn_auth_list(hrn):
71 return Xrn.hrn_split(hrn)[0:-1]
73 # e.g. hrn_auth ('a\.b.c.d') -> 'a\.b.c'
76 return '.'.join(Xrn.hrn_auth_list(hrn))
78 # e.g. escape ('a.b') -> 'a\.b'
81 return re.sub(r'([^\\])\.', r'\1\.', token)
83 # e.g. unescape ('a\.b') -> 'a.b'
86 return token.replace('\\.', '.')
88 # Return the HRN authority chain from top to bottom.
89 # e.g. hrn_auth_chain('a\.b.c.d') -> ['a\.b', 'a\.b.c']
91 def hrn_auth_chain(hrn):
92 parts = Xrn.hrn_auth_list(hrn)
94 for i in range(len(parts)):
95 chain.append('.'.join(parts[:i + 1]))
96 # Include the HRN itself?
100 # Is the given HRN a true authority over the namespace of the other
102 # A better alternative than childHRN.startswith(parentHRN)
103 # e.g. hrn_is_auth_for_hrn('a\.b', 'a\.b.c.d') -> True,
104 # but hrn_is_auth_for_hrn('a', 'a\.b.c.d') -> False
105 # Also hrn_is_auth_for_hrn('a\.b.c.d', 'a\.b.c.d') -> True
107 def hrn_is_auth_for_hrn(parenthrn, hrn):
110 for auth in Xrn.hrn_auth_chain(hrn):
111 if parenthrn == auth:
115 # basic tools on URNs
116 URN_PREFIX = "urn:publicid:IDN"
117 URN_PREFIX_lower = "urn:publicid:idn"
121 return text.lower().startswith(Xrn.URN_PREFIX_lower)
128 return Xrn.URN_PREFIX + urn
131 def urn_meaningful(urn):
133 return urn[len(Xrn.URN_PREFIX):]
139 return Xrn.urn_meaningful(urn).split('+')
142 def filter_type(urns=None, type=None):
151 if (xrn.type == type):
152 # Xrn is probably a urn so we can just compare types
156 # the local fields that are kept consistent
161 # provide either urn, or (hrn + type)
163 def __init__(self, xrn="", type=None, id=None):
166 # user has specified xrn : guess if urn or hrn
172 self.urn = "%s:%s" % (self.urn, str(id))
180 # happens all the time ..
182 # debug_logger.debug("type-less Xrn's are not safe")
185 result = "<XRN u=%s h=%s" % (self.urn, self.hrn)
186 if hasattr(self, 'leaf'):
187 result += " leaf=%s" % self.leaf
188 if hasattr(self, 'authority'):
189 result += " auth=%s" % self.authority
193 def get_urn(self): return self.urn
195 def get_hrn(self): return self.hrn
197 def get_type(self): return self.type
199 def get_hrn_type(self): return (self.hrn, self.type)
201 def _normalize(self):
203 raise SfaAPIError("Xrn._normalize")
204 if not hasattr(self, 'leaf'):
205 self.leaf = Xrn.hrn_split(self.hrn)[-1]
206 # self.authority keeps a list
207 if not hasattr(self, 'authority'):
208 self.authority = Xrn.hrn_auth_list(self.hrn)
214 def get_authority_hrn(self):
216 return '.'.join(self.authority)
218 def get_authority_urn(self):
220 return ':'.join([Xrn.unescape(x) for x in self.authority])
222 def set_authority(self, authority):
224 update the authority section of an existing urn
226 authority_hrn = self.get_authority_hrn()
227 if not authority_hrn.startswith(authority):
228 hrn = ".".join([authority, authority_hrn, self.get_leaf()])
230 hrn = ".".join([authority_hrn, self.get_leaf()])
236 # sliver_id_parts is list that contains the sliver's
237 # slice id and node id
238 def get_sliver_id_parts(self):
240 if self.type == 'sliver' or '-' in self.leaf:
241 sliver_id_parts = self.leaf.split('-')
242 return sliver_id_parts
244 def urn_to_hrn(self):
246 compute tuple (hrn, type) from urn
249 # if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
250 if not Xrn.is_urn(self.urn):
251 raise SfaAPIError("Xrn.urn_to_hrn")
253 parts = Xrn.urn_split(self.urn)
255 # Remove the authority name (e.g. '.sa')
256 if type == 'authority':
258 # Drop the sa. This is a bad hack, but its either this
259 # or completely change how record types are generated/stored
261 type = type + "+" + name
264 name = parts.pop(len(parts) - 1)
265 # convert parts (list) into hrn (str) by doing the following
266 # 1. remove blank parts
267 # 2. escape dots inside parts
268 # 3. replace ':' with '.' inside parts
269 # 3. join parts using '.'
270 hrn = '.'.join([Xrn.escape(part).replace(':', '.')
271 for part in parts if part])
272 # dont replace ':' in the name section
274 parts = name.split(':')
276 self.id = ":".join(parts[1:])
278 hrn += '.%s' % Xrn.escape(name)
281 self.type = str(type)
283 def hrn_to_urn(self):
285 compute urn from (hrn, type)
288 # if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
289 if Xrn.is_urn(self.hrn):
290 raise SfaAPIError("Xrn.hrn_to_urn, hrn=%s" % self.hrn)
292 if self.type and self.type.startswith('authority'):
293 self.authority = Xrn.hrn_auth_list(self.hrn)
294 leaf = self.get_leaf()
295 # if not self.authority:
296 # self.authority = [self.hrn]
297 type_parts = self.type.split("+")
298 self.type = type_parts[0]
300 if len(type_parts) > 1:
302 auth_parts = [part for part in [
303 self.get_authority_urn(), leaf] if part]
304 authority_string = ":".join(auth_parts)
306 self.authority = Xrn.hrn_auth_list(self.hrn)
307 name = Xrn.hrn_leaf(self.hrn)
308 authority_string = self.get_authority_urn()
310 if self.type is None:
311 urn = "+".join(['', authority_string, Xrn.unescape(name)])
313 urn = "+".join(['', authority_string,
314 self.type, Xrn.unescape(name)])
316 if hasattr(self, 'id') and self.id:
317 urn = "%s:%s" % (urn, self.id)
319 self.urn = Xrn.URN_PREFIX + urn
321 def dump_string(self):
322 result = "-------------------- XRN\n"
323 result += "URN=%s\n" % self.urn
324 result += "HRN=%s\n" % self.hrn
325 result += "TYPE=%s\n" % self.type
326 result += "LEAF=%s\n" % self.get_leaf()
327 result += "AUTH(hrn format)=%s\n" % self.get_authority_hrn()
328 result += "AUTH(urn format)=%s\n" % self.get_authority_urn()