3 from sfa.util.faults import *
4 from sfa.util.sfalogging import sfa_logger
6 # for convenience and smoother translation - we should get rid of these functions eventually
7 def get_leaf(hrn): return Xrn(xrn=hrn).get_leaf()
8 def get_authority(hrn): return Xrn(xrn=hrn).get_authority_hrn()
9 def urn_to_hrn(urn): xrn=Xrn(xrn=urn); return (xrn.hrn, xrn.type)
10 def hrn_to_urn(hrn,type): return Xrn(hrn=hrn, type=type).urn
14 ########## basic tools on HRNs
15 # split a HRN-like string into pieces
16 # this is like split('.') except for escaped (backslashed) dots
17 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
20 return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
22 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
24 def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
26 # e.g. hrn_auth_list ('a\.b.c.d') -> ['a\.b', 'c']
28 def hrn_auth_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
30 # e.g. hrn_auth ('a\.b.c.d') -> 'a\.b.c'
32 def hrn_auth(hrn): return '.'.join(Xrn.hrn_auth_list(hrn))
34 # e.g. escape ('a.b') -> 'a\.b'
36 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
37 # e.g. unescape ('a\.b') -> 'a.b'
39 def unescape(token): return token.replace('\\.','.')
41 URN_PREFIX = "urn:publicid:IDN"
43 ########## basic tools on URNs
46 if urn.startswith(Xrn.URN_PREFIX): return urn
47 else: return Xrn.URN_PREFIX+URN
49 def urn_meaningful (urn):
50 if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
54 return Xrn.urn_meaningful(urn).split('+')
57 # the local fields that are kept consistent
62 # provide either urn, or (hrn + type)
63 def __init__ (self, xrn=None, urn=None, hrn=None, type=None):
64 # user has specified xrn : guess if urn or hrn
66 if xrn.startswith(Xrn.URN_PREFIX):
73 # user has specified urn, let's use it
77 # user has specified hrn and type
78 elif hrn is not None and type is not None:
84 raise SfaAPIError,"Xrn.__init__"
85 # happens all the time ..
87 # sfa_logger().debug("type-less Xrn's are not safe")
89 def get_urn(self): return self.urn
90 def get_hrn(self): return self.hrn
91 def get_type(self): return self.type
92 def get_hrn_type(self): return (self.hrn, self.type)
95 if self.hrn is None: raise SfaAPIError, "Xrn._normalize"
96 if not hasattr(self,'leaf'):
97 self.leaf=Xrn.hrn_split(self.hrn)[-1]
98 # self.authority keeps a list
99 if not hasattr(self,'authority'):
100 self.authority=Xrn.hrn_auth_list(self.hrn)
106 def get_authority_hrn(self):
108 return '.'.join( self.authority )
110 def get_authority_urn(self):
112 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
114 def urn_to_hrn(self):
116 compute tuple (hrn, type) from urn
119 # if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
120 if not self.urn.startswith(Xrn.URN_PREFIX):
121 raise SfaAPIError, "Xrn.urn_to_hrn"
123 parts = Xrn.urn_split(self.urn)
125 # Remove the authority name (e.g. '.sa')
126 if type == 'authority': parts.pop()
128 # convert parts (list) into hrn (str) by doing the following
129 # 1. remove blank parts
130 # 2. escape dots inside parts
131 # 3. replace ':' with '.' inside parts
132 # 3. join parts using '.'
133 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
138 def hrn_to_urn(self):
140 compute urn from (hrn, type)
143 # if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
144 if self.hrn.startswith(Xrn.URN_PREFIX):
145 raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn
147 if self.type == 'authority':
148 self.authority = Xrn.hrn_split(self.hrn)
151 self.authority = Xrn.hrn_auth_list(self.hrn)
152 name = Xrn.hrn_leaf(self.hrn)
154 authority_string = self.get_authority_urn()
156 if self.type == None:
157 urn = "+".join(['',authority_string,name])
159 urn = "+".join(['',authority_string,self.type,name])
161 self.urn = Xrn.URN_PREFIX + urn
163 def dump_string(self):
164 result="-------------------- XRN\n"
165 result += "URN=%s\n"%self.urn
166 result += "HRN=%s\n"%self.hrn
167 result += "TYPE=%s\n"%self.type
168 result += "LEAF=%s\n"%self.get_leaf()
169 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
170 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()