first draft of the Xrn class, designed to replace sfa.util.namespace
[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 class Xrn:
7
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']
12     @staticmethod
13     def hrn_split(hrn):
14         return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
15
16     # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
17     @staticmethod
18     def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
19
20     # e.g. hrn_path_list ('a\.b.c.d') -> ['a\.b', 'c']
21     @staticmethod
22     def hrn_path_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
23     
24     # e.g. hrn_path ('a\.b.c.d') -> 'a\.b.c'
25     @staticmethod
26     def hrn_path(hrn): return '.'.join(Xrn.hrn_path_list(hrn))
27     
28     # e.g. escape ('a.b') -> 'a\.b'
29     @staticmethod
30     def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
31     # e.g. unescape ('a\.b') -> 'a.b'
32     @staticmethod
33     def unescape(token): return token.replace('\\.','.')
34         
35     URN_PREFIX = "urn:publicid:IDN"
36
37     ########## basic tools on URNs
38     @staticmethod
39     def urn_full (urn):
40         if urn.startswith(Xrn.URN_PREFIX): return urn
41         else: return Xrn.URN_PREFIX+URN
42     @staticmethod
43     def urn_meaningful (urn):
44         if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
45         else: return urn
46     @staticmethod
47     def urn_split (urn):
48         return Xrn.urn_meaningful(urn).split('+')
49
50     # provide either urn, or (hrn + type)
51     def __init__ (self, urn=None, hrn=None, type=None):
52         if urn: 
53             self.urn=urn
54             self.urn_to_hrn()
55         elif hrn and type: 
56             self.hrn=hrn
57             self.type=type
58             self.hrn_to_urn()
59         else:
60             raise SfaAPIError,"Xrn"
61
62     def get_urn(self): return self.urn
63     def get_hrn(self): return (self.hrn, self.type)
64
65     def get_leaf(self):
66         if not self.hrn: raise SfaAPIError, "Xrn"
67         if not hasattr(self,'leaf'): 
68             self.leaf=Xrn.hrn_split(self.hrn)[-1]
69         return self.leaf
70
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 )
77     
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] )
84     
85     def urn_to_hrn(self):
86         """
87         compute tuple (hrn, type) from urn
88         """
89         
90         if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
91             raise SfaAPIError, "Xrn"
92
93         parts = Xrn.urn_split(self.urn)
94         type=parts.pop(2)
95         # Remove the authority name (e.g. '.sa')
96         if type == 'authority': parts.pop()
97
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]) 
104
105         self.hrn=str(hrn)
106         self.type=str(type)
107     
108     def hrn_to_urn(self):
109         """
110         compute urn from (hrn, type)
111         """
112
113         if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
114             raise SfaAPIError, "Xrn"
115
116         if self.type == 'authority':
117             self.authority = Xrn.hrn_split(self.hrn)
118             name = 'sa'   
119         else:
120             self.authority = Xrn.hrn_path_list(self.hrn)
121             name = Xrn.hrn_leaf(self.hrn)
122
123         authority_string = self.get_authority_urn()
124
125         if self.type == None:
126             urn = "+".join(['',authority_string,name])
127         else:
128             urn = "+".join(['',authority_string,self.type,name])
129         
130         self.urn = Xrn.URN_PREFIX + urn
131
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()
140         return result
141