ProtoGeni slice names have special characters. Filter these out using re.sub()
[sfa.git] / sfa / util / namespace.py
index 86af66f..b94eb1d 100644 (file)
@@ -1,15 +1,18 @@
-### $Id: namespace.py 15020 2009-09-14 23:11:37Z tmack $
-### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/util/namespace.py $
-
+### $Id$
+### $URL$
+import re
 from sfa.util.faults import *
-
 URN_PREFIX = "urn:publicid:IDN"
 
 def get_leaf(hrn):
     parts = hrn.split(".")
     return ".".join(parts[-1:])
 
-def get_authority(hrn):
+def get_authority(xrn):
+    hrn, type = urn_to_hrn(xrn)
+    if type and type == 'authority':
+        return hrn
+    
     parts = hrn.split(".")
     return ".".join(parts[:-1])
 
@@ -30,8 +33,8 @@ def hostname_to_hrn(auth_hrn, login_base, hostname):
     """
     Convert hrn to plantelab name.
     """
-    genihostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
-    return genihostname
+    sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
+    return sfa_hostname
 
 def slicename_to_hrn(auth_hrn, slicename):
     """
@@ -45,7 +48,7 @@ def slicename_to_hrn(auth_hrn, slicename):
 def email_to_hrn(auth_hrn, email):
     parts = email.split("@")
     username = parts[0]
-    username = username.replace(".", "_") 
+    username = username.replace(".", "_").replace("+", "_") 
     person_hrn = ".".join([auth_hrn, username])
     
     return person_hrn 
@@ -57,21 +60,28 @@ def urn_to_hrn(urn):
     """
 
     # if this is already a hrn dont do anything
-    if not urn.startswith(URN_PREFIX):
+    if not urn or not urn.startswith(URN_PREFIX):
         return urn, None
+
     name = urn[len(URN_PREFIX):]
     hrn_parts = name.split("+")
+    type = hrn_parts.pop(2)
     
-    # type is always the second to last element in the list
-    type = hrn_parts.pop(-2)
+         
+    # Remove the authority name (e.g. '.sa')
+    if type == 'authority':
+        hrn_parts = hrn_parts[:-1]
 
     # convert hrn_parts (list) into hrn (str) by doing the following    
+    # dont allow special characters (except ':') in the site login base
     # remove blank elements
     # replace ':' with '.'
     # join list elements using '.'
-    hrn = '.'.join([part.replace(':', '.') for part in hrn_parts if part]) 
-   
-    return hrn, type 
+    only_alphanum = re.compile('[^a-zA-Z0-9:]+')
+    valid_chars = lambda x: only_alphanum.sub('', x).replace(':', '.')
+    hrn = '.'.join([valid_chars(part) for part in hrn_parts if part]) 
+    
+    return str(hrn), str(type) 
     
     
 def hrn_to_urn(hrn, type=None):
@@ -79,12 +89,23 @@ def hrn_to_urn(hrn, type=None):
     convert an hrn and type to a urn string
     """
     # if  this is already a urn dont do anything 
-    if hrn.startswith(URN_PREFIX):
+    if not hrn or hrn.startswith(URN_PREFIX):
         return hrn
 
     authority = get_authority(hrn)
     name = get_leaf(hrn)
-    urn = "+".join([unicode(part).replace('.', ':') \
-                    for part in ['',authority,type,name]])
-
+     
+    if type == 'authority':
+        authority = hrn
+        name = 'sa'   
+    
+    if authority.startswith("plc"):
+        if type == None:
+            urn = "+".join(['',authority.replace('.',':'),name])
+        else:
+            urn = "+".join(['',authority.replace('.',':'),type,name])
+
+    else:
+        urn = "+".join(['',authority,type,name])
+        
     return URN_PREFIX + urn