3 from sfa.util.faults import *
4 from sfa.util.sfalogging import sfa_logger
6 # for convenience and smoother translation
7 def get_leaf(hrn): return Xrn(hrn=hrn,type='any').get_leaf()
8 def get_authority(hrn): return Xrn(hrn=hrn,type='any').get_authority_hrn()
9 def urn_to_hrn(urn): xrn=Xrn(urn=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_path_list ('a\.b.c.d') -> ['a\.b', 'c']
28 def hrn_path_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
30 # e.g. hrn_path ('a\.b.c.d') -> 'a\.b.c'
32 def hrn_path(hrn): return '.'.join(Xrn.hrn_path_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('+')
56 # provide either urn, or (hrn + type)
57 def __init__ (self, urn=None, hrn=None, type=None):
66 raise SfaAPIError,"Xrn.__init__"
68 def get_urn(self): return self.urn
69 def get_hrn(self): return (self.hrn, self.type)
72 if not self.hrn: raise SfaAPIError, "Xrn.get_leaf"
73 if not hasattr(self,'leaf'):
74 self.leaf=Xrn.hrn_split(self.hrn)[-1]
77 def get_authority_hrn(self):
78 if not self.hrn: raise SfaAPIError, "Xrn.get_authority_hrn"
79 # self.authority keeps a list
80 if not hasattr(self,'authority'):
81 self.authority=Xrn.hrn_path_list(self.hrn)
82 return '.'.join( self.authority )
84 def get_authority_urn(self):
85 if not self.hrn: raise SfaAPIError, "Xrn.get_authority_urn"
86 # self.authority keeps a list
87 if not hasattr(self,'authority'):
88 self.authority=Xrn.hrn_path_list(self.hrn)
89 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
93 compute tuple (hrn, type) from urn
96 if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
97 raise SfaAPIError, "Xrn.urn_to_hrn"
99 parts = Xrn.urn_split(self.urn)
101 # Remove the authority name (e.g. '.sa')
102 if type == 'authority': parts.pop()
104 # convert parts (list) into hrn (str) by doing the following
105 # 1. remove blank parts
106 # 2. escape dots inside parts
107 # 3. replace ':' with '.' inside parts
108 # 3. join parts using '.'
109 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
114 def hrn_to_urn(self):
116 compute urn from (hrn, type)
119 if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
120 raise SfaAPIError, "Xrn.hrn_to_urn"
122 if self.type == 'authority':
123 self.authority = Xrn.hrn_split(self.hrn)
126 self.authority = Xrn.hrn_path_list(self.hrn)
127 name = Xrn.hrn_leaf(self.hrn)
129 authority_string = self.get_authority_urn()
131 if self.type == None:
132 urn = "+".join(['',authority_string,name])
134 urn = "+".join(['',authority_string,self.type,name])
136 self.urn = Xrn.URN_PREFIX + urn
138 def dump_string(self):
139 result="-------------------- XRN\n"
140 result += "URN=%s\n"%self.urn
141 result += "HRN=%s\n"%self.hrn
142 result += "TYPE=%s\n"%self.type
143 result += "LEAF=%s\n"%self.get_leaf()
144 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
145 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()