merged namespace
[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_leaf(hrn):
8     parts = hrn.split(".")
9     return ".".join(parts[-1:])
10
11 def get_authority(xrn):
12     hrn, type = urn_to_hrn(xrn)
13     if type and type == 'authority':
14         return hrn
15     
16     parts = hrn.split(".")
17     return ".".join(parts[:-1])
18
19 def hrn_to_pl_slicename(hrn):
20     # remove any escaped no alpah numeric characters
21     #hrn = re.sub('\\\[^a-zA-Z0-9]', '', hrn)
22     # remove any escaped '.' (i.e. '\.')
23     hrn = hrn.replace('\\.', '')
24     parts = hrn.split(".")
25     return parts[-2] + "_" + parts[-1]
26
27 # assuming hrn is the hrn of an authority, return the plc authority name
28 def hrn_to_pl_authname(hrn):
29     # remove any escaped '.' (i.e. '\.')
30     hrn = hrn.replace('\\.', '')
31     parts = hrn.split(".")
32     return parts[-1]
33
34 # assuming hrn is the hrn of an authority, return the plc login_base
35 def hrn_to_pl_login_base(hrn):
36     # remove any escaped '.' (i.e. '\.')
37     hrn = hrn.replace('\\.', '')
38     return hrn_to_pl_authname(hrn)
39
40 def hostname_to_hrn(auth_hrn, login_base, hostname):
41     """
42     Convert hrn to plantelab name.
43     """
44     sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
45     return sfa_hostname
46
47 def slicename_to_hrn(auth_hrn, slicename):
48     """
49     Convert hrn to planetlab name.
50     """
51     parts = slicename.split("_")
52     slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
53
54     return slice_hrn
55
56 def email_to_hrn(auth_hrn, email):
57     parts = email.split("@")
58     username = parts[0]
59     username = username.replace(".", "_").replace("+", "_") 
60     person_hrn = ".".join([auth_hrn, username])
61     
62     return person_hrn 
63
64 def urn_to_hrn(urn):
65     """
66     convert a urn to hrn
67     return a tuple (hrn, type)
68     """
69
70     # if this is already a hrn dont do anything
71     if not urn or not urn.startswith(URN_PREFIX):
72         return urn, None
73
74     name = urn[len(URN_PREFIX):]
75     hrn_parts = name.split("+")
76     type = hrn_parts.pop(2)
77     
78          
79     # Remove the authority name (e.g. '.sa')
80     if type == 'authority':
81         hrn_parts = hrn_parts[:-1]
82
83     # convert hrn_parts (list) into hrn (str) by doing the following
84     # 1. remove blank elements
85     # 2. escape '.'            # '.' exists in protogeni object names and are not delimiters
86     # 3. replace ':' with '.'  # ':' is the urn hierarchy delimiter
87     # 4. join list elements using '.' 
88     hrn = '.'.join([part.replace('.', '\\.').replace(':', '.') for part in hrn_parts if part]) 
89     
90     return str(hrn), str(type) 
91     
92     
93 def hrn_to_urn(hrn, type=None):
94     """
95     convert an hrn and type to a urn string
96     """
97     # if  this is already a urn dont do anything 
98     if not hrn or hrn.startswith(URN_PREFIX):
99         return hrn
100
101     if type == 'authority':
102         authority = hrn
103         name = 'sa'   
104     else:
105         authority = get_authority(hrn)
106         name = get_leaf(hrn)   
107    
108     # We have to do the following conversion
109     # '\\.'  -> '.'    # where '.' belongs in the urn name
110     # '.'    -> ':"    # where ':' is the urn hierarchy delimiter
111     # by doing the following
112     # 1. split authority around '\\.'
113     # 2. replace '.' with ':' in all parts
114     # 3. join parts around '.'  
115     parts = authority.split('\\.')
116     authority = '.'.join([part.replace('.', ':') for part in parts])
117     
118     if type == None:
119         urn = "+".join(['',authority,name])
120     else:
121         urn = "+".join(['',authority,type,name])
122
123         
124     return URN_PREFIX + urn