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).get_leaf()
8 def get_authority(hrn): return Xrn(hrn=hrn).get_authority_hrn()
9 # these methods we should get rid of eventually
10 def urn_to_hrn(urn): xrn=Xrn(xrn=urn); return (xrn.hrn, xrn.type)
11 def hrn_to_urn(hrn,type): return Xrn(hrn=hrn, type=type).urn
15 ########## basic tools on HRNs
16 # split a HRN-like string into pieces
17 # this is like split('.') except for escaped (backslashed) dots
18 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
21 return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
23 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
25 def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
27 # e.g. hrn_path_list ('a\.b.c.d') -> ['a\.b', 'c']
29 def hrn_path_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
31 # e.g. hrn_path ('a\.b.c.d') -> 'a\.b.c'
33 def hrn_path(hrn): return '.'.join(Xrn.hrn_path_list(hrn))
35 # e.g. escape ('a.b') -> 'a\.b'
37 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
38 # e.g. unescape ('a\.b') -> 'a.b'
40 def unescape(token): return token.replace('\\.','.')
42 URN_PREFIX = "urn:publicid:IDN"
44 ########## basic tools on URNs
47 if urn.startswith(Xrn.URN_PREFIX): return urn
48 else: return Xrn.URN_PREFIX+URN
50 def urn_meaningful (urn):
51 if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
55 return Xrn.urn_meaningful(urn).split('+')
57 # provide either urn, or (hrn + type)
58 def __init__ (self, xrn=None, urn=None, hrn=None, type=None):
65 raise SfaAPIError,"Xrn.__init__"
67 if xrn.startswith(Xrn.URN_PREFIX):
76 sfa_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, self.type)
82 if not self.hrn: raise SfaAPIError, "Xrn.get_leaf"
83 if not hasattr(self,'leaf'):
84 self.leaf=Xrn.hrn_split(self.hrn)[-1]
87 def get_authority_hrn(self):
88 if not self.hrn: raise SfaAPIError, "Xrn.get_authority_hrn"
89 # self.authority keeps a list
90 if not hasattr(self,'authority'):
91 self.authority=Xrn.hrn_path_list(self.hrn)
92 return '.'.join( self.authority )
94 def get_authority_urn(self):
95 if not self.hrn: raise SfaAPIError, "Xrn.get_authority_urn"
96 # self.authority keeps a list
97 if not hasattr(self,'authority'):
98 self.authority=Xrn.hrn_path_list(self.hrn)
99 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
101 def urn_to_hrn(self):
103 compute tuple (hrn, type) from urn
106 if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
107 raise SfaAPIError, "Xrn.urn_to_hrn"
109 parts = Xrn.urn_split(self.urn)
111 # Remove the authority name (e.g. '.sa')
112 if type == 'authority': parts.pop()
114 # convert parts (list) into hrn (str) by doing the following
115 # 1. remove blank parts
116 # 2. escape dots inside parts
117 # 3. replace ':' with '.' inside parts
118 # 3. join parts using '.'
119 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
124 def hrn_to_urn(self):
126 compute urn from (hrn, type)
129 if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
130 raise SfaAPIError, "Xrn.hrn_to_urn"
132 if self.type == 'authority':
133 self.authority = Xrn.hrn_split(self.hrn)
136 self.authority = Xrn.hrn_path_list(self.hrn)
137 name = Xrn.hrn_leaf(self.hrn)
139 authority_string = self.get_authority_urn()
141 if self.type == None:
142 urn = "+".join(['',authority_string,name])
144 urn = "+".join(['',authority_string,self.type,name])
146 self.urn = Xrn.URN_PREFIX + urn
148 def dump_string(self):
149 result="-------------------- XRN\n"
150 result += "URN=%s\n"%self.urn
151 result += "HRN=%s\n"%self.hrn
152 result += "TYPE=%s\n"%self.type
153 result += "LEAF=%s\n"%self.get_leaf()
154 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
155 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()