86a1f8637f5201069fd5658e4c41ef6f0ec822cd
[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 xrn:
60             if xrn.startswith(Xrn.URN_PREFIX):
61                 self.urn=xrn
62                 self.urn_to_hrn()
63             else:
64                 self.hrn=xrn
65                 self.type=type
66                 self.hrn_to_urn()
67         elif urn: 
68             self.urn=urn
69             self.urn_to_hrn()
70         elif hrn and type: 
71             self.hrn=hrn
72             self.type=type
73             self.hrn_to_urn()
74         else:
75             raise SfaAPIError,"Xrn.__init__"
76         if not type:
77             sfa_logger().debug("type-less Xrn's are not safe")
78
79     def get_urn(self): return self.urn
80     def get_hrn(self): return (self.hrn, self.type)
81
82     def get_leaf(self):
83         if not self.hrn: raise SfaAPIError, "Xrn.get_leaf"
84         if not hasattr(self,'leaf'): 
85             self.leaf=Xrn.hrn_split(self.hrn)[-1]
86         return self.leaf
87
88     def get_authority_hrn(self): 
89         if not self.hrn: raise SfaAPIError, "Xrn.get_authority_hrn"
90         # self.authority keeps a list
91         if not hasattr(self,'authority'): 
92             self.authority=Xrn.hrn_path_list(self.hrn)
93         return '.'.join( self.authority )
94     
95     def get_authority_urn(self): 
96         if not self.hrn: raise SfaAPIError, "Xrn.get_authority_urn"
97         # self.authority keeps a list
98         if not hasattr(self,'authority'): 
99             self.authority=Xrn.hrn_path_list(self.hrn)
100         return ':'.join( [Xrn.unescape(x) for x in self.authority] )
101     
102     def urn_to_hrn(self):
103         """
104         compute tuple (hrn, type) from urn
105         """
106         
107         if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
108             raise SfaAPIError, "Xrn.urn_to_hrn"
109
110         parts = Xrn.urn_split(self.urn)
111         type=parts.pop(2)
112         # Remove the authority name (e.g. '.sa')
113         if type == 'authority': parts.pop()
114
115         # convert parts (list) into hrn (str) by doing the following
116         # 1. remove blank parts
117         # 2. escape dots inside parts
118         # 3. replace ':' with '.' inside parts
119         # 3. join parts using '.' 
120         hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part]) 
121
122         self.hrn=str(hrn)
123         self.type=str(type)
124     
125     def hrn_to_urn(self):
126         """
127         compute urn from (hrn, type)
128         """
129
130         if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
131             raise SfaAPIError, "Xrn.hrn_to_urn"
132
133         if self.type == 'authority':
134             self.authority = Xrn.hrn_split(self.hrn)
135             name = 'sa'   
136         else:
137             self.authority = Xrn.hrn_path_list(self.hrn)
138             name = Xrn.hrn_leaf(self.hrn)
139
140         authority_string = self.get_authority_urn()
141
142         if self.type == None:
143             urn = "+".join(['',authority_string,name])
144         else:
145             urn = "+".join(['',authority_string,self.type,name])
146         
147         self.urn = Xrn.URN_PREFIX + urn
148
149     def dump_string(self):
150         result="-------------------- XRN\n"
151         result += "URN=%s\n"%self.urn
152         result += "HRN=%s\n"%self.hrn
153         result += "TYPE=%s\n"%self.type
154         result += "LEAF=%s\n"%self.get_leaf()
155         result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
156         result += "AUTH(urn format)=%s\n"%self.get_authority_urn()
157         return result
158