better support for sliver ids
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Thu, 30 Aug 2012 14:52:34 +0000 (10:52 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Thu, 30 Aug 2012 14:52:34 +0000 (10:52 -0400)
sfa/util/xrn.py

index 4a47b58..f2ab48b 100644 (file)
@@ -32,9 +32,6 @@ 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\r
 def hrn_authfor_hrn(parenthrn, hrn): return Xrn.hrn_is_auth_for_hrn(parenthrn, hrn)\r
 \r
-def urn_to_sliver_id(urn, slice_id, node_id, index=0, authority=None):\r
-    return Xrn(urn).get_sliver_id(slice_id, node_id, index, authority)\r
-\r
 class Xrn:\r
 \r
     ########## basic tools on HRNs\r
@@ -119,12 +116,15 @@ class Xrn:
     # self.type\r
     # self.path\r
     # provide either urn, or (hrn + type)\r
-    def __init__ (self, xrn, type=None):\r
+    def __init__ (self, xrn, type=None, id=None):\r
         if not xrn: xrn = ""\r
         # user has specified xrn : guess if urn or hrn\r
+        self.id = id\r
         if Xrn.is_urn(xrn):\r
             self.hrn=None\r
             self.urn=xrn\r
+            if id:\r
+                self.urn = "%s:%s" % (self.urn, str(id))\r
             self.urn_to_hrn()\r
         else:\r
             self.urn=None\r
@@ -168,19 +168,20 @@ class Xrn:
         self._normalize()\r
         return ':'.join( [Xrn.unescape(x) for x in self.authority] )\r
 \r
-    def get_sliver_id(self, slice_id, node_id=None, index=0, authority=None):\r
+    def set_authority(self, authority):\r
+        """\r
+        update the authority section of an existing urn\r
+        """\r
+        authority_hrn = self.get_authority_hrn()\r
+        if not authority_hrn.startswith(authority):\r
+            hrn = ".".join([authority,authority_hrn, self.get_leaf()])\r
+        else:\r
+            hrn = ".".join([authority_hrn, self.get_leaf()])\r
+            \r
+        self.hrn = hrn \r
+        self.hrn_to_urn()\r
         self._normalize()\r
-        urn = self.get_urn()\r
-        if authority:\r
-            authority_hrn = self.get_authority_hrn()\r
-            if not authority_hrn.startswith(authority):\r
-                hrn = ".".join([authority,authority_hrn, self.get_leaf()])\r
-            else:\r
-                hrn = ".".join([authority_hrn, self.get_leaf()])\r
-            urn = Xrn(hrn, self.get_type()).get_urn()\r
-        parts = [part for part in [urn, slice_id, node_id, index] if part is not None]\r
-        return ":".join(map(str, [parts]))\r
-\r
+        \r
     def urn_to_hrn(self):\r
         """\r
         compute tuple (hrn, type) from urn\r
@@ -210,6 +211,10 @@ class Xrn:
         hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])\r
         # dont replace ':' in the name section\r
         if name:\r
+            parts = name.split(':')\r
+            if len(parts) > 1:\r
+                self.id = ":".join(parts[1:])\r
+                name = parts[0]    \r
             hrn += '.%s' % Xrn.escape(name) \r
 \r
         self.hrn=str(hrn)\r
@@ -245,7 +250,10 @@ class Xrn:
             urn = "+".join(['',authority_string,Xrn.unescape(name)])\r
         else:\r
             urn = "+".join(['',authority_string,self.type,Xrn.unescape(name)])\r
-        \r
+\r
+        if self.id:\r
+            urn = "%s:%s" % (urn, self.id)        \r
+\r
         self.urn = Xrn.URN_PREFIX + urn\r
 \r
     def dump_string(self):\r