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