3 from sfa.util.faults import *
4 from sfa.util.sfalogging import sfa_logger
8 ########## basic tools on HRNs
9 # split a HRN-like string into pieces
10 # this is like split('.') except for escaped (backslashed) dots
11 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
14 return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
16 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
18 def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
20 # e.g. hrn_path_list ('a\.b.c.d') -> ['a\.b', 'c']
22 def hrn_path_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
24 # e.g. hrn_path ('a\.b.c.d') -> 'a\.b.c'
26 def hrn_path(hrn): return '.'.join(Xrn.hrn_path_list(hrn))
28 # e.g. escape ('a.b') -> 'a\.b'
30 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
31 # e.g. unescape ('a\.b') -> 'a.b'
33 def unescape(token): return token.replace('\\.','.')
35 URN_PREFIX = "urn:publicid:IDN"
37 ########## basic tools on URNs
40 if urn.startswith(Xrn.URN_PREFIX): return urn
41 else: return Xrn.URN_PREFIX+URN
43 def urn_meaningful (urn):
44 if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
48 return Xrn.urn_meaningful(urn).split('+')
50 # provide either urn, or (hrn + type)
51 def __init__ (self, urn=None, hrn=None, type=None):
60 raise SfaAPIError,"Xrn"
62 def get_urn(self): return self.urn
63 def get_hrn(self): return (self.hrn, self.type)
66 if not self.hrn: raise SfaAPIError, "Xrn"
67 if not hasattr(self,'leaf'):
68 self.leaf=Xrn.hrn_split(self.hrn)[-1]
71 def get_authority_hrn(self):
72 if not self.hrn: raise SfaAPIError, "Xrn"
73 # self.authority keeps a list
74 if not hasattr(self,'authority'):
75 self.authority=Xrn.hrn_path_list(self.hrn)
76 return '.'.join( self.authority )
78 def get_authority_urn(self):
79 if not self.hrn: raise SfaAPIError, "Xrn"
80 # self.authority keeps a list
81 if not hasattr(self,'authority'):
82 self.authority=Xrn.hrn_path_list(self.hrn)
83 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
87 compute tuple (hrn, type) from urn
90 if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
91 raise SfaAPIError, "Xrn"
93 parts = Xrn.urn_split(self.urn)
95 # Remove the authority name (e.g. '.sa')
96 if type == 'authority': parts.pop()
98 # convert parts (list) into hrn (str) by doing the following
99 # 1. remove blank parts
100 # 2. escape dots inside parts
101 # 3. replace ':' with '.' inside parts
102 # 3. join parts using '.'
103 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
108 def hrn_to_urn(self):
110 compute urn from (hrn, type)
113 if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
114 raise SfaAPIError, "Xrn"
116 if self.type == 'authority':
117 self.authority = Xrn.hrn_split(self.hrn)
120 self.authority = Xrn.hrn_path_list(self.hrn)
121 name = Xrn.hrn_leaf(self.hrn)
123 authority_string = self.get_authority_urn()
125 if self.type == None:
126 urn = "+".join(['',authority_string,name])
128 urn = "+".join(['',authority_string,self.type,name])
130 self.urn = Xrn.URN_PREFIX + urn
132 def dump_string(self):
133 result="-------------------- XRN\n"
134 result += "URN=%s\n"%self.urn
135 result += "HRN=%s\n"%self.hrn
136 result += "TYPE=%s\n"%self.type
137 result += "LEAF=%s\n"%self.get_leaf()
138 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
139 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()