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