3 from sfa.util.faults import *
5 # for convenience and smoother translation - we should get rid of these functions eventually
6 def get_leaf(hrn): return Xrn(hrn).get_leaf()
7 def get_authority(hrn): return Xrn(hrn).get_authority_hrn()
8 def urn_to_hrn(urn): xrn=Xrn(urn); return (xrn.hrn, xrn.type)
9 def hrn_to_urn(hrn,type): return Xrn(hrn, type=type).urn
11 def urn_to_sliver_id(urn, slice_id, node_id, index=0):
12 return ":".join([urn, str(slice_id), str(node_id), str(index)])
16 ########## basic tools on HRNs
17 # split a HRN-like string into pieces
18 # this is like split('.') except for escaped (backslashed) dots
19 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
22 return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
24 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
26 def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
28 # e.g. hrn_auth_list ('a\.b.c.d') -> ['a\.b', 'c']
30 def hrn_auth_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
32 # e.g. hrn_auth ('a\.b.c.d') -> 'a\.b.c'
34 def hrn_auth(hrn): return '.'.join(Xrn.hrn_auth_list(hrn))
36 # e.g. escape ('a.b') -> 'a\.b'
38 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
39 # e.g. unescape ('a\.b') -> 'a.b'
41 def unescape(token): return token.replace('\\.','.')
43 URN_PREFIX = "urn:publicid:IDN"
45 ########## basic tools on URNs
48 if urn.startswith(Xrn.URN_PREFIX): return urn
49 else: return Xrn.URN_PREFIX+URN
51 def urn_meaningful (urn):
52 if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
56 return Xrn.urn_meaningful(urn).split('+')
59 # the local fields that are kept consistent
64 # provide either urn, or (hrn + type)
65 def __init__ (self, xrn, type=None):
67 # user has specified xrn : guess if urn or hrn
68 if xrn.startswith(Xrn.URN_PREFIX):
77 # happens all the time ..
79 # debug_logger.debug("type-less Xrn's are not safe")
81 def get_urn(self): return self.urn
82 def get_hrn(self): return self.hrn
83 def get_type(self): return self.type
84 def get_hrn_type(self): return (self.hrn, self.type)
87 if self.hrn is None: raise SfaAPIError, "Xrn._normalize"
88 if not hasattr(self,'leaf'):
89 self.leaf=Xrn.hrn_split(self.hrn)[-1]
90 # self.authority keeps a list
91 if not hasattr(self,'authority'):
92 self.authority=Xrn.hrn_auth_list(self.hrn)
98 def get_authority_hrn(self):
100 return '.'.join( self.authority )
102 def get_authority_urn(self):
104 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
106 def urn_to_hrn(self):
108 compute tuple (hrn, type) from urn
111 # if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
112 if not self.urn.startswith(Xrn.URN_PREFIX):
113 raise SfaAPIError, "Xrn.urn_to_hrn"
115 parts = Xrn.urn_split(self.urn)
117 # Remove the authority name (e.g. '.sa')
118 if type == 'authority':
120 # Drop the sa. This is a bad hack, but its either this
121 # or completely change how record types are generated/stored
123 type = type + "+" + name
125 # convert parts (list) into hrn (str) by doing the following
126 # 1. remove blank parts
127 # 2. escape dots inside parts
128 # 3. replace ':' with '.' inside parts
129 # 3. join parts using '.'
130 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
135 def hrn_to_urn(self):
137 compute urn from (hrn, type)
140 # if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
141 if self.hrn.startswith(Xrn.URN_PREFIX):
142 raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn
144 if self.type and self.type.startswith('authority'):
145 self.authority = Xrn.hrn_split(self.hrn)
146 type_parts = self.type.split("+")
147 self.type = type_parts[0]
149 if len(type_parts) > 1:
152 self.authority = Xrn.hrn_auth_list(self.hrn)
153 name = Xrn.hrn_leaf(self.hrn)
155 authority_string = self.get_authority_urn()
157 if self.type == None:
158 urn = "+".join(['',authority_string,Xrn.unescape(name)])
160 urn = "+".join(['',authority_string,self.type,Xrn.unescape(name)])
162 self.urn = Xrn.URN_PREFIX + urn
164 def dump_string(self):
165 result="-------------------- XRN\n"
166 result += "URN=%s\n"%self.urn
167 result += "HRN=%s\n"%self.hrn
168 result += "TYPE=%s\n"%self.type
169 result += "LEAF=%s\n"%self.get_leaf()
170 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
171 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()