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
13 ########## basic tools on HRNs
14 # split a HRN-like string into pieces
15 # this is like split('.') except for escaped (backslashed) dots
16 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
19 return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
21 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
23 def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
25 # e.g. hrn_auth_list ('a\.b.c.d') -> ['a\.b', 'c']
27 def hrn_auth_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
29 # e.g. hrn_auth ('a\.b.c.d') -> 'a\.b.c'
31 def hrn_auth(hrn): return '.'.join(Xrn.hrn_auth_list(hrn))
33 # e.g. escape ('a.b') -> 'a\.b'
35 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
36 # e.g. unescape ('a\.b') -> 'a.b'
38 def unescape(token): return token.replace('\\.','.')
40 URN_PREFIX = "urn:publicid:IDN"
42 ########## basic tools on URNs
45 if urn.startswith(Xrn.URN_PREFIX): return urn
46 else: return Xrn.URN_PREFIX+URN
48 def urn_meaningful (urn):
49 if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
53 return Xrn.urn_meaningful(urn).split('+')
56 # the local fields that are kept consistent
61 # provide either urn, or (hrn + type)
62 def __init__ (self, xrn, type=None):
64 # user has specified xrn : guess if urn or hrn
65 if xrn.startswith(Xrn.URN_PREFIX):
74 # happens all the time ..
76 # debug_logger.debug("type-less Xrn's are not safe")
78 def get_urn(self): return self.urn
79 def get_hrn(self): return self.hrn
80 def get_type(self): return self.type
81 def get_hrn_type(self): return (self.hrn, self.type)
84 if self.hrn is None: raise SfaAPIError, "Xrn._normalize"
85 if not hasattr(self,'leaf'):
86 self.leaf=Xrn.hrn_split(self.hrn)[-1]
87 # self.authority keeps a list
88 if not hasattr(self,'authority'):
89 self.authority=Xrn.hrn_auth_list(self.hrn)
95 def get_authority_hrn(self):
97 return '.'.join( self.authority )
99 def get_authority_urn(self):
101 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
103 def urn_to_hrn(self):
105 compute tuple (hrn, type) from urn
108 # if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
109 if not self.urn.startswith(Xrn.URN_PREFIX):
110 raise SfaAPIError, "Xrn.urn_to_hrn"
112 parts = Xrn.urn_split(self.urn)
114 # Remove the authority name (e.g. '.sa')
115 if type == 'authority':
117 # Drop the sa. This is a bad hack, but its either this
118 # or completely change how record types are generated/stored
120 type = type + "+" + name
122 # convert parts (list) into hrn (str) by doing the following
123 # 1. remove blank parts
124 # 2. escape dots inside parts
125 # 3. replace ':' with '.' inside parts
126 # 3. join parts using '.'
127 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
132 def hrn_to_urn(self):
134 compute urn from (hrn, type)
137 # if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
138 if self.hrn.startswith(Xrn.URN_PREFIX):
139 raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn
141 if self.type and self.type.startswith('authority'):
142 self.authority = Xrn.hrn_split(self.hrn)
143 type_parts = self.type.split("+")
144 self.type = type_parts[0]
146 if len(type_parts) > 1:
149 self.authority = Xrn.hrn_auth_list(self.hrn)
150 name = Xrn.hrn_leaf(self.hrn)
152 authority_string = self.get_authority_urn()
154 if self.type == None:
155 urn = "+".join(['',authority_string,Xrn.unescape(name)])
157 urn = "+".join(['',authority_string,self.type,Xrn.unescape(name)])
159 self.urn = Xrn.URN_PREFIX + urn
161 def dump_string(self):
162 result="-------------------- XRN\n"
163 result += "URN=%s\n"%self.urn
164 result += "HRN=%s\n"%self.hrn
165 result += "TYPE=%s\n"%self.type
166 result += "LEAF=%s\n"%self.get_leaf()
167 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
168 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()