remove refrences to legacy methods
[sfa.git] / sfa / util / xrn.py
1 import re
2
3 from sfa.util.faults import *
4 from sfa.util.sfalogging import sfa_logger
5
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
12
13 class Xrn:
14
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']
19     @staticmethod
20     def hrn_split(hrn):
21         return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
22
23     # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
24     @staticmethod
25     def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
26
27     # e.g. hrn_path_list ('a\.b.c.d') -> ['a\.b', 'c']
28     @staticmethod
29     def hrn_path_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
30     
31     # e.g. hrn_path ('a\.b.c.d') -> 'a\.b.c'
32     @staticmethod
33     def hrn_path(hrn): return '.'.join(Xrn.hrn_path_list(hrn))
34     
35     # e.g. escape ('a.b') -> 'a\.b'
36     @staticmethod
37     def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
38     # e.g. unescape ('a\.b') -> 'a.b'
39     @staticmethod
40     def unescape(token): return token.replace('\\.','.')
41         
42     URN_PREFIX = "urn:publicid:IDN"
43
44     ########## basic tools on URNs
45     @staticmethod
46     def urn_full (urn):
47         if urn.startswith(Xrn.URN_PREFIX): return urn
48         else: return Xrn.URN_PREFIX+URN
49     @staticmethod
50     def urn_meaningful (urn):
51         if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
52         else: return urn
53     @staticmethod
54     def urn_split (urn):
55         return Xrn.urn_meaningful(urn).split('+')
56
57     # provide either urn, or (hrn + type)
58     def __init__ (self, xrn=None, urn=None, hrn=None, type=None):
59         if not xrn:
60             if urn:
61                 xrn = urn
62             elif hrn:
63                 xrn = hrn
64             else:
65                 raise SfaAPIError,"Xrn.__init__"
66
67         if xrn.startswith(Xrn.URN_PREFIX):
68             self.urn=xrn
69             self.urn_to_hrn()
70         else:
71             self.hrn=xrn
72             self.type=type
73             self.hrn_to_urn()
74
75         if not type:
76             sfa_logger().debug("type-less Xrn's are not safe")
77
78     def get_urn(self): return self.urn
79     def get_hrn(self): return (self.hrn, self.type)
80
81     def get_leaf(self):
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]
85         return self.leaf
86
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 )
93     
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] )
100     
101     def urn_to_hrn(self):
102         """
103         compute tuple (hrn, type) from urn
104         """
105         
106         if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
107             raise SfaAPIError, "Xrn.urn_to_hrn"
108
109         parts = Xrn.urn_split(self.urn)
110         type=parts.pop(2)
111         # Remove the authority name (e.g. '.sa')
112         if type == 'authority': parts.pop()
113
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]) 
120
121         self.hrn=str(hrn)
122         self.type=str(type)
123     
124     def hrn_to_urn(self):
125         """
126         compute urn from (hrn, type)
127         """
128
129         if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
130             raise SfaAPIError, "Xrn.hrn_to_urn"
131
132         if self.type == 'authority':
133             self.authority = Xrn.hrn_split(self.hrn)
134             name = 'sa'   
135         else:
136             self.authority = Xrn.hrn_path_list(self.hrn)
137             name = Xrn.hrn_leaf(self.hrn)
138
139         authority_string = self.get_authority_urn()
140
141         if self.type == None:
142             urn = "+".join(['',authority_string,name])
143         else:
144             urn = "+".join(['',authority_string,self.type,name])
145         
146         self.urn = Xrn.URN_PREFIX + urn
147
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()
156         return result
157