cleaned up imports
[sfa.git] / sfa / util / xrn.py
index a503e71..d5ad2d4 100644 (file)
@@ -4,10 +4,10 @@ from sfa.util.faults import *
 from sfa.util.sfalogging import sfa_logger
 
 # for convenience and smoother translation - we should get rid of these functions eventually 
-def get_leaf(hrn): return Xrn(xrn=hrn).get_leaf()
-def get_authority(hrn): return Xrn(xrn=hrn).get_authority_hrn()
-def urn_to_hrn(urn): xrn=Xrn(xrn=urn); return (xrn.hrn, xrn.type)
-def hrn_to_urn(hrn,type): return Xrn(hrn=hrn, type=type).urn
+def get_leaf(hrn): return Xrn(hrn).get_leaf()
+def get_authority(hrn): return Xrn(hrn).get_authority_hrn()
+def urn_to_hrn(urn): xrn=Xrn(urn); return (xrn.hrn, xrn.type)
+def hrn_to_urn(hrn,type): return Xrn(hrn, type=type).urn
 
 class Xrn:
 
@@ -60,28 +60,18 @@ class Xrn:
     # self.type
     # self.path
     # provide either urn, or (hrn + type)
-    def __init__ (self, xrn=None, urn=None, hrn=None, type=None):
+    def __init__ (self, xrn, type=None):
+        if not xrn: xrn = ""
         # user has specified xrn : guess if urn or hrn
-        if xrn is not None:
-            if xrn.startswith(Xrn.URN_PREFIX):
-                self.urn=xrn
-                self.urn_to_hrn()
-            else:
-                self.hrn=xrn
-                self.type=type
-                self.hrn_to_urn()
-        # user has specified urn, let's use it
-        elif urn is not None: 
-            self.urn=urn
+        if xrn.startswith(Xrn.URN_PREFIX):
+            self.hrn=None
+            self.urn=xrn
             self.urn_to_hrn()
-        # user has specified hrn and type
-        elif hrn is not None and type is not None: 
-            self.hrn=hrn
+        else:
+            self.urn=None
+            self.hrn=xrn
             self.type=type
             self.hrn_to_urn()
-        # what should we do ?
-        else:
-            raise SfaAPIError,"Xrn.__init__"
 # happens all the time ..
 #        if not type:
 #            sfa_logger().debug("type-less Xrn's are not safe")
@@ -123,7 +113,12 @@ class Xrn:
         parts = Xrn.urn_split(self.urn)
         type=parts.pop(2)
         # Remove the authority name (e.g. '.sa')
-        if type == 'authority': parts.pop()
+        if type == 'authority':
+            name = parts.pop()
+            # Drop the sa. This is a bad hack, but its either this
+            # or completely change how record types are generated/stored   
+            if name != 'sa':
+                type = type + "+" + name
 
         # convert parts (list) into hrn (str) by doing the following
         # 1. remove blank parts
@@ -144,9 +139,13 @@ class Xrn:
         if self.hrn.startswith(Xrn.URN_PREFIX):
             raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn
 
-        if self.type == 'authority':
+        if self.type and self.type.startswith('authority'):
             self.authority = Xrn.hrn_split(self.hrn)
-            name = 'sa'   
+            type_parts = self.type.split("+")
+            self.type = type_parts[0]
+            name = 'sa'
+            if len(type_parts) > 1:
+                name = type_parts[1]
         else:
             self.authority = Xrn.hrn_auth_list(self.hrn)
             name = Xrn.hrn_leaf(self.hrn)
@@ -154,9 +153,9 @@ class Xrn:
         authority_string = self.get_authority_urn()
 
         if self.type == None:
-            urn = "+".join(['',authority_string,name])
+            urn = "+".join(['',authority_string,Xrn.unescape(name)])
         else:
-            urn = "+".join(['',authority_string,self.type,name])
+            urn = "+".join(['',authority_string,self.type,Xrn.unescape(name)])
         
         self.urn = Xrn.URN_PREFIX + urn