Skip to content

Commit

Permalink
Generate ipv6 uri based on java format
Browse files Browse the repository at this point in the history
  • Loading branch information
juztas committed Oct 9, 2023
1 parent 22e6383 commit e7a3479
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/python/SiteRMLibs/ipaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ def replaceSpecialSymbols(valIn):
return valIn


def _ipv6InJavaFormat(ipinput):
"""Return IPv6 address in Java expected output format.
2001:0DB8:0000:0D30:0000:0000:0000:0000 becomes
2001:DB8:0:CD30:0:0:0:0
"""
out = []
for item in ipinput.split(':'):
tmpval = str(item)
tmparr = []
stoploop = False
for i in range(len(tmpval)):
if not stoploop:
if tmpval[i] != '0':
tmparr.append(tmpval[i])
stoploop = True
continue
tmparr.append(tmpval[i])
if not tmparr:
tmparr.append('0')
out.append("".join(tmparr))
return ":".join(out)


def validMRMLName(valIn):
"""Generate valid MRML Name for ipv6 value"""
# In case of IPv6, it does allow multiple ways to list IP address, like:
Expand All @@ -109,10 +132,10 @@ def validMRMLName(valIn):
# Because of this - we always use a short version
if ipVersion(valIn) == 6:
tmpspl = valIn.split('/')
longip = ip_address(tmpspl[0]).exploded
longip = _ipv6InJavaFormat(ip_address(tmpspl[0]).exploded)
if len(tmpspl) == 2:
return f"{longip}_{tmpspl[1]}".replace('0000', '0').replace(':', '_')
return f"{longip}".replace('0000', '0').replace(':', '_')
return f"{longip}_{tmpspl[1]}".replace(':', '_')
return f"{longip}".replace(':', '_')
return replaceSpecialSymbols(valIn)


Expand Down

0 comments on commit e7a3479

Please sign in to comment.