* Xrn object is initialized with a single non-optional xrn string. hrn/urn is detecte...
[sfa.git] / sfa / util / plxrn.py
1 # specialized Xrn class for PlanetLab
2 from sfa.util.xrn import Xrn
3
4 # temporary helper functions to use this module instead of namespace
5 def hostname_to_hrn (auth, login_base, hostname):
6     return PlXrn(auth=auth+'.'+login_base,hostname=hostname).get_hrn()
7 def slicename_to_hrn (auth_hrn, slicename):
8     return PlXrn(auth=auth_hrn,slicename=slicename).get_hrn()
9 def email_to_hrn (auth_hrn, email):
10     return PlXrn(auth=auth_hrn, email=email).get_hrn()
11 def hrn_to_pl_slicename (hrn):
12     return PlXrn(xrn=hrn,type='slice').pl_slicename()
13 def hrn_to_pl_login_base (hrn):
14     return PlXrn(xrn=hrn,type='slice').pl_login_base()
15 def hrn_to_pl_authname (hrn):
16     return PlXrn(xrn=hrn,type='any').pl_authname()
17
18
19 class PlXrn (Xrn):
20
21     @staticmethod 
22     def site_hrn (auth, login_base):
23         return '.'.join(auth,login_base)
24
25     def __init__ (self, auth=None, hostname=None, slicename=None, email=None, **kwargs):
26         #def hostname_to_hrn(auth_hrn, login_base, hostname):
27         if hostname is not None:
28             self.type='node'
29             # keep only the first part of the DNS name
30             self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
31             self.hrn_to_urn()
32         #def slicename_to_hrn(auth_hrn, slicename):
33         elif slicename is not None:
34             self.type='slice'
35             # split at the first _
36             parts = slicename.split("_",1)
37             self.hrn = ".".join([auth] + parts )
38             self.hrn_to_urn()
39         #def email_to_hrn(auth_hrn, email):
40         elif email is not None:
41             self.type='person'
42             # keep only the part before '@' and replace special chars into _
43             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
44             self.hrn_to_urn()
45         else:
46             Xrn.__init__ (self,**kwargs)
47
48     #def hrn_to_pl_slicename(hrn):
49     def pl_slicename (self):
50         self._normalize()
51         return self.authority[-1] + '_' + self.leaf
52
53     #def hrn_to_pl_authname(hrn):
54     def pl_authname (self):
55         self._normalize()
56         return self.authority[-1]
57
58     #def hrn_to_pl_login_base(hrn):
59     def pl_login_base (self):
60         self._normalize()
61         return self.authority[-1]