removed another bunch of references to geni
[sfa.git] / sfa / util / namespace.py
1 ### $Id$
2 ### $URL$
3
4 from sfa.util.faults import *
5
6 URN_PREFIX = "urn:publicid:IDN"
7
8 def get_leaf(hrn):
9     parts = hrn.split(".")
10     return ".".join(parts[-1:])
11
12 def get_authority(hrn):
13     parts = hrn.split(".")
14     return ".".join(parts[:-1])
15
16 def hrn_to_pl_slicename(hrn):
17     parts = hrn.split(".")
18     return parts[-2] + "_" + parts[-1]
19
20 # assuming hrn is the hrn of an authority, return the plc authority name
21 def hrn_to_pl_authname(hrn):
22     parts = hrn.split(".")
23     return parts[-1]
24
25 # assuming hrn is the hrn of an authority, return the plc login_base
26 def hrn_to_pl_login_base(hrn):
27     return hrn_to_pl_authname(hrn)
28
29 def hostname_to_hrn(auth_hrn, login_base, hostname):
30     """
31     Convert hrn to plantelab name.
32     """
33     sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
34     return sfa_hostname
35
36 def slicename_to_hrn(auth_hrn, slicename):
37     """
38     Convert hrn to planetlab name.
39     """
40     parts = slicename.split("_")
41     slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
42
43     return slice_hrn
44
45 def email_to_hrn(auth_hrn, email):
46     parts = email.split("@")
47     username = parts[0]
48     username = username.replace(".", "_") 
49     person_hrn = ".".join([auth_hrn, username])
50     
51     return person_hrn 
52
53 def urn_to_hrn(urn):
54     """
55     convert a urn to hrn
56     return a tuple (hrn, type)
57     """
58
59     # if this is already a hrn dont do anything
60     if not urn.startswith(URN_PREFIX):
61         return urn, None
62     name = urn[len(URN_PREFIX):]
63     hrn_parts = name.split("+")
64     
65     # type is always the second to last element in the list
66     type = hrn_parts.pop(-2)
67
68     # convert hrn_parts (list) into hrn (str) by doing the following    
69     # remove blank elements
70     # replace ':' with '.'
71     # join list elements using '.'
72     hrn = '.'.join([part.replace(':', '.') for part in hrn_parts if part]) 
73    
74     return hrn, type 
75     
76     
77 def hrn_to_urn(hrn, type=None):
78     """
79     convert an hrn and type to a urn string
80     """
81     # if  this is already a urn dont do anything 
82     if hrn.startswith(URN_PREFIX):
83         return hrn
84
85     authority = get_authority(hrn)
86     name = get_leaf(hrn)
87     urn = "+".join([unicode(part).replace('.', ':') \
88                     for part in ['',authority,type,name]])
89
90     return URN_PREFIX + urn