first draft of the Xrn class, designed to replace sfa.util.namespace
[sfa.git] / sfa / util / namespace.py
1 #
2 #  WARNING - This file should be soon deprecated in favor of sfa.util.xrn
3 #
4 #
5 #  WARNING - This file should be soon deprecated in favor of sfa.util.xrn
6 #
7 #
8 #  WARNING - This file should be soon deprecated in favor of sfa.util.xrn
9 #
10 #
11 #  WARNING - This file should be soon deprecated in favor of sfa.util.xrn
12 #
13 #
14 #  WARNING - This file should be soon deprecated in favor of sfa.util.xrn
15 #
16 #
17 #  WARNING - This file should be soon deprecated in favor of sfa.util.xrn
18 #
19 import re
20 from sfa.util.faults import *
21 URN_PREFIX = "urn:publicid:IDN"
22
23 def __get_hierarchy_delim_indexes(hrn):
24     # find all non escaped '.'
25     hierarchy_delim = '([a-zA-Z0-9][\.])'
26     parts = re.findall(hierarchy_delim, hrn)
27     # list of indexes for every  hierarchy delimieter
28     indexes = []
29     for part in parts:
30         indexes.append(hrn.index(part) + 1)
31     return indexes 
32
33 def get_leaf(hrn):
34     delim_indexes = __get_hierarchy_delim_indexes(hrn)
35     if not delim_indexes:
36         return hrn
37     
38     last_delim_index = delim_indexes[-1:][0] + 1
39     return hrn[last_delim_index:] 
40
41 def get_authority(xrn):
42     hrn, type = urn_to_hrn(xrn)
43     if type and type == 'authority':
44         return hrn
45   
46     delim_indexes = __get_hierarchy_delim_indexes(hrn)
47     if not delim_indexes:
48         return ''
49     last_delim_index = delim_indexes[-1:][0] 
50     return hrn[:last_delim_index] 
51     
52 def hrn_to_pl_slicename(hrn):
53     # remove any escaped no alpah numeric characters
54     #hrn = re.sub('\\\[^a-zA-Z0-9]', '', hrn)
55     hrn = re.sub(r'\\(.)', '', hrn)
56     parts = hrn.split(".")
57     return parts[-2] + "_" + parts[-1]
58
59 # assuming hrn is the hrn of an authority, return the plc authority name
60 def hrn_to_pl_authname(hrn):
61     # remove any escaped no alpah numeric characters
62     hrn = re.sub(r'\\(.)', '', hrn)
63     parts = hrn.split(".")
64     return parts[-1]
65
66 # assuming hrn is the hrn of an authority, return the plc login_base
67 def hrn_to_pl_login_base(hrn):
68     # remove any escaped no alpah numeric characters
69     hrn = re.sub(r'\\(.)', '', hrn)
70     return hrn_to_pl_authname(hrn)
71
72 def hostname_to_hrn(auth_hrn, login_base, hostname):
73     """
74     Convert hrn to plantelab name.
75     """
76     sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
77     return sfa_hostname
78
79 def slicename_to_hrn(auth_hrn, slicename):
80     """
81     Convert hrn to planetlab name.
82     """
83     parts = slicename.split("_")
84     slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
85
86     return slice_hrn
87
88 def email_to_hrn(auth_hrn, email):
89     parts = email.split("@")
90     username = parts[0]
91     username = username.replace(".", "_").replace("+", "_") 
92     person_hrn = ".".join([auth_hrn, username])
93     
94     return person_hrn 
95
96 def urn_to_hrn(urn):
97     """
98     convert a urn to hrn
99     return a tuple (hrn, type)
100     """
101
102     # if this is already a hrn dont do anything
103     if not urn or not urn.startswith(URN_PREFIX):
104         return urn, None
105
106     name = urn[len(URN_PREFIX):]
107     urn_parts = name.split("+")
108     type = urn_parts.pop(2)
109     
110          
111     # Remove the authority name (e.g. '.sa')
112     if type == 'authority':
113         urn_parts = urn_parts[:-1]
114
115     # convert hrn_parts (list) into hrn (str) by doing the following
116     # 1. remove blank elements
117     # 2. escape all non alpha numeric chars (excluding ':')
118     # 3. replace ':' with '.'  (':' is the urn hierarchy delimiter)
119     # 4. join list elements using '.' 
120     #hrn = '.'.join([part.replace('.', '\\.').replace(':', '.') for part in hrn_parts if part]) 
121     hrn = '.'.join([re.sub(r'([^a-zA-Z0-9\:])', r'\\\1', part).replace(':', '.') for part in urn_parts if part]) 
122     
123     return str(hrn), str(type) 
124     
125     
126 def hrn_to_urn(hrn, type=None):
127     """
128     convert an hrn and type to a urn string
129     """
130     # if  this is already a urn dont do anything 
131     if not hrn or hrn.startswith(URN_PREFIX):
132         return hrn
133
134     if type == 'authority':
135         authority = hrn
136         name = 'sa'   
137     else:
138         authority = get_authority(hrn)
139         name = get_leaf(hrn)   
140   
141     # convert from hierarchy delimiter from '.' to ':'   
142     authority = re.sub(r'([a-zA-Z0-9])[\.]', r'\1:', authority) 
143     # unescape escaped characters
144     authority = re.sub(r'\\(.)', r'\1', authority)
145     
146     if type == None:
147         urn = "+".join(['',authority,name])
148     else:
149         urn = "+".join(['',authority,type,name])
150
151         
152     return URN_PREFIX + urn