From: Thierry Parmentelat Date: Tue, 23 Apr 2013 11:48:08 +0000 (+0200) Subject: when computing hrns, try to use SFA code first, otherwise resort to a local version X-Git-Tag: plcapi-5.2-2~2 X-Git-Url: http://git.onelab.eu/?p=plcapi.git;a=commitdiff_plain;h=4ded09f35820e9580d874157fe6f8ee46a82a402 when computing hrns, try to use SFA code first, otherwise resort to a local version --- diff --git a/PLC/Namespace.py b/PLC/Namespace.py index d0ce9b8..2e785db 100644 --- a/PLC/Namespace.py +++ b/PLC/Namespace.py @@ -1,86 +1,38 @@ -URN_PREFIX = "urn:publicid:IDN" - -def get_leaf(hrn): - parts = hrn.split(".") - return ".".join(parts[-1:]) - -def get_authority(hrn): - parts = hrn.split(".") - return ".".join(parts[:-1]) - -def hrn_to_pl_slicename(hrn): - parts = hrn.split(".") - return parts[-2] + "_" + parts[-1] - -# assuming hrn is the hrn of an authority, return the plc authority name -def hrn_to_pl_authname(hrn): - parts = hrn.split(".") - return parts[-1] - -# assuming hrn is the hrn of an authority, return the plc login_base -def hrn_to_pl_login_base(hrn): - return hrn_to_pl_authname(hrn) - -def hostname_to_hrn(auth_hrn, login_base, hostname): - """ - Convert hrn to planetlab name. - """ - sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]]) - return sfa_hostname - -def slicename_to_hrn(auth_hrn, slicename): - """ - Convert hrn to planetlab name. - """ - parts = slicename.split("_") - slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:]) - - return slice_hrn - -def email_to_hrn(auth_hrn, email): - parts = email.split("@") - username = parts[0] - username = username.replace(".", "_") - person_hrn = ".".join([auth_hrn, username]) - - return person_hrn - -def urn_to_hrn(urn): - """ - convert a urn to hrn - return a tuple (hrn, type) - """ - - # if this is already a hrn dont do anything - if not urn or not urn.startswith(URN_PREFIX): - return urn, None - - name = urn[len(URN_PREFIX):] - hrn_parts = name.split("+") - - # type is always the second to last element in the list - type = hrn_parts.pop(-2) - - # convert hrn_parts (list) into hrn (str) by doing the following - # remove blank elements - # replace ':' with '.' - # join list elements using '.' - hrn = '.'.join([part.replace(':', '.') for part in hrn_parts if part]) - - return str(hrn), str(type) - - -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 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]]) - - return URN_PREFIX + urn +# +# Thierry, April 2013 +# +# This file here is the connection to the SFA code +# at some point in time we had duplicated this from sfa +# but of course both versions have entirely diverged since then +# in addition we really only need 2 hepler functions here, that allow to maintain +# hrn for nodes and persons and that is all +# +# So in order to avoid such situations in the future, +# we try to import and re-use the SFA code +# assumption being that if these hrn's are of importance then it makes sense to +# require people to have sfa-plc installed as well +# however we do not want this requirement to break myplc in case it is not fulfilled +# + +#################### try to import from sfa +try: + from sfa.planetlab.plxrn import hostname_to_hrn +except: + hostname_to_hrn=None + +try: + from sfa.planetlab.plxrn import email_to_hrn +except: + email_to_hrn=None + +#################### if not found, bring our own local version +import re +def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token) + +if hostname_to_hrn is None: + def hostname_to_hrn (auth_hrn, login_base, hostname): + return ".".join( [ auth_hrn, login_base, escape(hostname) ] ) + +if email_to_hrn is None: + def email_to_hrn (auth_hrn, email): + return '.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])