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 functions eventually
29 def get_leaf(hrn): return Xrn(hrn).get_leaf()
30 def get_authority(hrn): return Xrn(hrn).get_authority_hrn()
31 def urn_to_hrn(urn): xrn=Xrn(urn); return (xrn.hrn, xrn.type)
32 def hrn_to_urn(hrn,type): return Xrn(hrn, type=type).urn
33 def hrn_authfor_hrn(parenthrn, hrn): return Xrn.hrn_is_auth_for_hrn(parenthrn, hrn)
37 ########## basic tools on HRNs
38 # split a HRN-like string into pieces
39 # this is like split('.') except for escaped (backslashed) dots
40 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
43 return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
45 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
47 def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
49 # e.g. hrn_auth_list ('a\.b.c.d') -> ['a\.b', 'c']
51 def hrn_auth_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
53 # e.g. hrn_auth ('a\.b.c.d') -> 'a\.b.c'
55 def hrn_auth(hrn): return '.'.join(Xrn.hrn_auth_list(hrn))
57 # e.g. escape ('a.b') -> 'a\.b'
59 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
61 # e.g. unescape ('a\.b') -> 'a.b'
63 def unescape(token): return token.replace('\\.','.')
65 # Return the HRN authority chain from top to bottom.
66 # e.g. hrn_auth_chain('a\.b.c.d') -> ['a\.b', 'a\.b.c']
68 def hrn_auth_chain(hrn):
69 parts = Xrn.hrn_auth_list(hrn)
71 for i in range(len(parts)):
72 chain.append('.'.join(parts[:i+1]))
73 # Include the HRN itself?
77 # Is the given HRN a true authority over the namespace of the other
79 # A better alternative than childHRN.startswith(parentHRN)
80 # e.g. hrn_is_auth_for_hrn('a\.b', 'a\.b.c.d') -> True,
81 # but hrn_is_auth_for_hrn('a', 'a\.b.c.d') -> False
82 # Also hrn_is_auth_for_hrn('a\.b.c.d', 'a\.b.c.d') -> True
84 def hrn_is_auth_for_hrn(parenthrn, hrn):
87 for auth in Xrn.hrn_auth_chain(hrn):
92 ########## basic tools on URNs
93 URN_PREFIX = "urn:publicid:IDN"
94 URN_PREFIX_lower = "urn:publicid:idn"
98 return text.lower().startswith(Xrn.URN_PREFIX_lower)
102 if Xrn.is_urn(urn): return urn
103 else: return Xrn.URN_PREFIX+urn
105 def urn_meaningful (urn):
106 if Xrn.is_urn(urn): return urn[len(Xrn.URN_PREFIX):]
110 return Xrn.urn_meaningful(urn).split('+')
113 def filter_type(urns=[], type=None):
121 # If an xrn doesn't have a type its probably a hrn.
122 # We have to make some assumptions on the hrn's type
123 # based on the contents of the hrn.
124 if type == 'sliver' and '-' in xrn.leaf:
126 elif type != 'sliver' and not '-' in xrn.leaf:
128 elif (xrn.type == type):
129 # Xrn is probably a urn so we can just compare types
133 # the local fields that are kept consistent
138 # provide either urn, or (hrn + type)
139 def __init__ (self, xrn="", type=None, id=None):
141 # user has specified xrn : guess if urn or hrn
147 self.urn = "%s:%s" % (self.urn, str(id))
155 # happens all the time ..
157 # debug_logger.debug("type-less Xrn's are not safe")
160 result="<XRN u=%s h=%s"%(self.urn,self.hrn)
161 if hasattr(self,'leaf'): result += " leaf=%s"%self.leaf
162 if hasattr(self,'authority'): result += " auth=%s"%self.authority
166 def get_urn(self): return self.urn
167 def get_hrn(self): return self.hrn
168 def get_type(self): return self.type
169 def get_hrn_type(self): return (self.hrn, self.type)
171 def _normalize(self):
172 if self.hrn is None: raise SfaAPIError, "Xrn._normalize"
173 if not hasattr(self,'leaf'):
174 self.leaf=Xrn.hrn_split(self.hrn)[-1]
175 # self.authority keeps a list
176 if not hasattr(self,'authority'):
177 self.authority=Xrn.hrn_auth_list(self.hrn)
183 def get_authority_hrn(self):
185 return '.'.join( self.authority )
187 def get_authority_urn(self):
189 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
191 def set_authority(self, authority):
193 update the authority section of an existing urn
195 authority_hrn = self.get_authority_hrn()
196 if not authority_hrn.startswith(authority):
197 hrn = ".".join([authority,authority_hrn, self.get_leaf()])
199 hrn = ".".join([authority_hrn, self.get_leaf()])
205 # sliver_id_parts is list that contains the sliver's
206 # slice id and node id
207 def get_sliver_id_parts(self):
209 if self.type == 'sliver' or '-' in self.leaf:
210 sliver_id_parts = self.leaf.split('-')
211 return sliver_id_parts
214 def urn_to_hrn(self):
216 compute tuple (hrn, type) from urn
219 # if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
220 if not Xrn.is_urn(self.urn):
221 raise SfaAPIError, "Xrn.urn_to_hrn"
223 parts = Xrn.urn_split(self.urn)
225 # Remove the authority name (e.g. '.sa')
226 if type == 'authority':
228 # Drop the sa. This is a bad hack, but its either this
229 # or completely change how record types are generated/stored
231 type = type + "+" + name
234 name = parts.pop(len(parts)-1)
235 # convert parts (list) into hrn (str) by doing the following
236 # 1. remove blank parts
237 # 2. escape dots inside parts
238 # 3. replace ':' with '.' inside parts
239 # 3. join parts using '.'
240 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
241 # dont replace ':' in the name section
243 parts = name.split(':')
245 self.id = ":".join(parts[1:])
247 hrn += '.%s' % Xrn.escape(name)
252 def hrn_to_urn(self):
254 compute urn from (hrn, type)
257 # if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
258 if Xrn.is_urn(self.hrn):
259 raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn
261 if self.type and self.type.startswith('authority'):
262 self.authority = Xrn.hrn_auth_list(self.hrn)
263 leaf = self.get_leaf()
264 #if not self.authority:
265 # self.authority = [self.hrn]
266 type_parts = self.type.split("+")
267 self.type = type_parts[0]
269 if len(type_parts) > 1:
271 auth_parts = [part for part in [self.get_authority_urn(), leaf] if part]
272 authority_string = ":".join(auth_parts)
274 self.authority = Xrn.hrn_auth_list(self.hrn)
275 name = Xrn.hrn_leaf(self.hrn)
276 authority_string = self.get_authority_urn()
278 if self.type == None:
279 urn = "+".join(['',authority_string,Xrn.unescape(name)])
281 urn = "+".join(['',authority_string,self.type,Xrn.unescape(name)])
283 if hasattr(self, 'id') and self.id:
284 urn = "%s:%s" % (urn, self.id)
286 self.urn = Xrn.URN_PREFIX + urn
288 def dump_string(self):
289 result="-------------------- XRN\n"
290 result += "URN=%s\n"%self.urn
291 result += "HRN=%s\n"%self.hrn
292 result += "TYPE=%s\n"%self.type
293 result += "LEAF=%s\n"%self.get_leaf()
294 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
295 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()