\r
import re\r
\r
-from sfa.util.faults import *\r
+from sfa.util.faults import SfaAPIError\r
\r
# for convenience and smoother translation - we should get rid of these functions eventually \r
def get_leaf(hrn): return Xrn(hrn).get_leaf()\r
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
# A better alternative than childHRN.startswith(parentHRN)\r
# e.g. hrn_is_auth_for_hrn('a\.b', 'a\.b.c.d') -> True,\r
# but hrn_is_auth_for_hrn('a', 'a\.b.c.d') -> False\r
- # Also hrn_is_uauth_for_hrn('a\.b.c.d', 'a\.b.c.d') -> True\r
+ # Also hrn_is_auth_for_hrn('a\.b.c.d', 'a\.b.c.d') -> True\r
@staticmethod\r
def hrn_is_auth_for_hrn(parenthrn, hrn):\r
if parenthrn == hrn:\r
return True\r
return False\r
\r
+ ########## basic tools on URNs\r
URN_PREFIX = "urn:publicid:IDN"\r
+ URN_PREFIX_lower = "urn:publicid:idn"\r
+\r
+ @staticmethod\r
+ def is_urn (text):\r
+ return text.lower().startswith(Xrn.URN_PREFIX_lower)\r
\r
- ########## basic tools on URNs\r
@staticmethod\r
def urn_full (urn):\r
- if urn.startswith(Xrn.URN_PREFIX): return urn\r
- else: return Xrn.URN_PREFIX+URN\r
+ if Xrn.is_urn(urn): return urn\r
+ else: return Xrn.URN_PREFIX+urn\r
@staticmethod\r
def urn_meaningful (urn):\r
- if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]\r
+ if Xrn.is_urn(urn): return urn[len(Xrn.URN_PREFIX):]\r
else: return urn\r
@staticmethod\r
def urn_split (urn):\r
def __init__ (self, xrn, type=None):\r
if not xrn: xrn = ""\r
# user has specified xrn : guess if urn or hrn\r
- if xrn.startswith(Xrn.URN_PREFIX):\r
+ if Xrn.is_urn(xrn):\r
self.hrn=None\r
self.urn=xrn\r
self.urn_to_hrn()\r
self.hrn=xrn\r
self.type=type\r
self.hrn_to_urn()\r
+ self._normalize()\r
# happens all the time ..\r
# if not type:\r
# debug_logger.debug("type-less Xrn's are not safe")\r
\r
+ def __repr__ (self):\r
+ result="<XRN u=%s h=%s"%(self.urn,self.hrn)\r
+ if hasattr(self,'leaf'): result += " leaf=%s"%self.leaf\r
+ if hasattr(self,'authority'): result += " auth=%s"%self.authority\r
+ result += ">"\r
+ return result\r
+\r
def get_urn(self): return self.urn\r
def get_hrn(self): return self.hrn\r
def get_type(self): return self.type\r
self._normalize()\r
return self.leaf\r
\r
- def get_authority_hrn(self): \r
+ def get_authority_hrn(self):\r
self._normalize()\r
return '.'.join( self.authority )\r
\r
def get_authority_urn(self): \r
self._normalize()\r
return ':'.join( [Xrn.unescape(x) for x in self.authority] )\r
- \r
+\r
+ def get_sliver_id(self, slice_id, node_id, index=0, authority=None):\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
+ return ":".join(map(str, [urn, slice_id, node_id, index]))\r
+\r
def urn_to_hrn(self):\r
"""\r
compute tuple (hrn, type) from urn\r
"""\r
\r
# if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):\r
- if not self.urn.startswith(Xrn.URN_PREFIX):\r
+ if not Xrn.is_urn(self.urn):\r
raise SfaAPIError, "Xrn.urn_to_hrn"\r
\r
parts = Xrn.urn_split(self.urn)\r
# or completely change how record types are generated/stored \r
if name != 'sa':\r
type = type + "+" + name\r
-\r
+ name =""\r
+ else:\r
+ name = parts.pop(len(parts)-1)\r
# convert parts (list) into hrn (str) by doing the following\r
# 1. remove blank parts\r
# 2. escape dots inside parts\r
# 3. replace ':' with '.' inside parts\r
- # 3. join parts using '.' \r
- hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part]) \r
+ # 3. join parts using '.'\r
+ hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])\r
+ # dont replace ':' in the name section\r
+ if name:\r
+ hrn += '.%s' % Xrn.escape(name) \r
\r
self.hrn=str(hrn)\r
self.type=str(type)\r
"""\r
\r
# if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):\r
- if self.hrn.startswith(Xrn.URN_PREFIX):\r
+ if Xrn.is_urn(self.hrn):\r
raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn\r
\r
if self.type and self.type.startswith('authority'):\r
- self.authority = Xrn.hrn_split(self.hrn)\r
+ self.authority = Xrn.hrn_auth_list(self.hrn)\r
+ leaf = self.get_leaf()\r
+ #if not self.authority:\r
+ # self.authority = [self.hrn]\r
type_parts = self.type.split("+")\r
self.type = type_parts[0]\r
name = 'sa'\r
if len(type_parts) > 1:\r
name = type_parts[1]\r
+ auth_parts = [part for part in [self.get_authority_urn(), leaf] if part]\r
+ authority_string = ":".join(auth_parts)\r
else:\r
self.authority = Xrn.hrn_auth_list(self.hrn)\r
name = Xrn.hrn_leaf(self.hrn)\r
-\r
- authority_string = self.get_authority_urn()\r
+ authority_string = self.get_authority_urn()\r
\r
if self.type == None:\r
urn = "+".join(['',authority_string,Xrn.unescape(name)])\r