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