be more flexible in the accepted inputs
[sfa.git] / sfa / util / xrn.py
index 2230668..86a1f86 100644 (file)
@@ -3,6 +3,13 @@ import re
 from sfa.util.faults import *
 from sfa.util.sfalogging import sfa_logger
 
+# for convenience and smoother translation
+def get_leaf(hrn): return Xrn(hrn=hrn).get_leaf()
+def get_authority(hrn): return Xrn(hrn=hrn).get_authority_hrn()
+# these methods we should get rid of eventually
+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
+
 class Xrn:
 
     ########## basic tools on HRNs
@@ -48,8 +55,16 @@ class Xrn:
         return Xrn.urn_meaningful(urn).split('+')
 
     # provide either urn, or (hrn + type)
-    def __init__ (self, urn=None, hrn=None, type=None):
-        if urn: 
+    def __init__ (self, xrn=None, urn=None, hrn=None, type=None):
+        if xrn:
+            if xrn.startswith(Xrn.URN_PREFIX):
+                self.urn=xrn
+                self.urn_to_hrn()
+            else:
+                self.hrn=xrn
+                self.type=type
+                self.hrn_to_urn()
+        elif urn: 
             self.urn=urn
             self.urn_to_hrn()
         elif hrn and type: 
@@ -57,26 +72,28 @@ class Xrn:
             self.type=type
             self.hrn_to_urn()
         else:
-            raise SfaAPIError,"Xrn"
+            raise SfaAPIError,"Xrn.__init__"
+        if not type:
+            sfa_logger().debug("type-less Xrn's are not safe")
 
     def get_urn(self): return self.urn
     def get_hrn(self): return (self.hrn, self.type)
 
     def get_leaf(self):
-        if not self.hrn: raise SfaAPIError, "Xrn"
+        if not self.hrn: raise SfaAPIError, "Xrn.get_leaf"
         if not hasattr(self,'leaf'): 
             self.leaf=Xrn.hrn_split(self.hrn)[-1]
         return self.leaf
 
     def get_authority_hrn(self): 
-        if not self.hrn: raise SfaAPIError, "Xrn"
+        if not self.hrn: raise SfaAPIError, "Xrn.get_authority_hrn"
         # self.authority keeps a list
         if not hasattr(self,'authority'): 
             self.authority=Xrn.hrn_path_list(self.hrn)
         return '.'.join( self.authority )
     
     def get_authority_urn(self): 
-        if not self.hrn: raise SfaAPIError, "Xrn"
+        if not self.hrn: raise SfaAPIError, "Xrn.get_authority_urn"
         # self.authority keeps a list
         if not hasattr(self,'authority'): 
             self.authority=Xrn.hrn_path_list(self.hrn)
@@ -88,7 +105,7 @@ class Xrn:
         """
         
         if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
-            raise SfaAPIError, "Xrn"
+            raise SfaAPIError, "Xrn.urn_to_hrn"
 
         parts = Xrn.urn_split(self.urn)
         type=parts.pop(2)
@@ -111,7 +128,7 @@ class Xrn:
         """
 
         if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
-            raise SfaAPIError, "Xrn"
+            raise SfaAPIError, "Xrn.hrn_to_urn"
 
         if self.type == 'authority':
             self.authority = Xrn.hrn_split(self.hrn)