Skip to content

Instantly share code, notes, and snippets.

@FelixWolf
Last active October 22, 2020 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FelixWolf/fa8ac80d5fa3779bf4df7107dc5069af to your computer and use it in GitHub Desktop.
Save FelixWolf/fa8ac80d5fa3779bf4df7107dc5069af to your computer and use it in GitHub Desktop.
import xml.etree.ElementTree as ET
import uuid
import datetime
import base64
def parseLLSD(element):
if element.tag == "undef":
return None
elif element.tag == "boolean":
if element.text.lower() == "true" or element.text == "1":
return True
elif element.text.lower() == "false" or element.text == "0" or element.text == "":
return False
else:
raise ValueError("Bad value for bool: {}".format(element.text))
elif element.tag == "string":
return element.text or ""
elif element.tag == "integer":
return int(element.text or 0)
elif element.tag == "real":
return float(element.text or 0.0)
elif element.tag == "uuid":
return uuid.UUID(element.text or "00000000-0000-0000-0000-000000000000")
elif element.tag == "date":
date = []
buf = ""
position = 0
for c in element.text:
if c == "-":
if position == 0 and len(buf) == 4:
date.append(int(buf))
buf = ""
elif position == 1 and len(buf) == 2:
date.append(int(buf))
buf = ""
else:
raise ValueError("Bad value for date: Unexpected \"{}\"".format(c))
position += 1
elif c.lower() == "t":
if position == 2 and len(buf) == 2:
date.append(int(buf))
buf = ""
else:
raise ValueError("Bad value for date: Unexpected \"{}\"".format(c))
position += 1
elif c == ":":
if position == 3 and len(buf) == 2:
date.append(int(buf))
buf = ""
elif position == 4 and len(buf) == 2:
date.append(int(buf))
buf = ""
else:
raise ValueError("Bad value for date: Unexpected \"{}\"".format(c))
position += 1
elif c == ".":
if position == 5 and len(buf) == 2:
date.append(int(buf))
buf = ""
else:
raise ValueError("Bad value for date: Unexpected \"{}\"".format(c))
position += 1
elif c.lower() == "z":
if position == 6:
date.append(int(buf))
buf = ""
else:
raise ValueError("Bad value for date: Unexpected \"{}\"".format(c))
position += 1
else:
buf += c
if buf != "":
raise ValueError("Bad value for date: Unemptied Buffer \"{}\"".format(buf))
elif not (position == 3 or position == 6 or position == 7):
raise ValueError("Bad value for date: Unexpected end of string")
return datetime.datetime(*date)
elif element.tag == "uri":
return element.text or ""
elif element.tag == "binary":
encoding = element.attrib.get("encoding", "base64").lower()
if encoding == "base64":
return base64.b64decode(element.text)
elif encoding == "base16":
return base64.b16decode(element.text)
elif encoding == "base85":
return base64.b85decode(element.text)
else:
raise ValueError("Bad encoding for binary: {}".format(encoding))
elif element.tag == "map":
result = {}
for i in range(0, len(element), 2):
if element[i].tag != "key":
raise ValueError("Unexpected element in map(Expected key): {}".format(element[i].tag))
result[element[i].text] = parseLLSD(element[i+1])
return result
elif element.tag == "array":
result = []
for child in element:
result.append(parseLLSD(child))
return result
else:
raise ValueError("Unexpected element {}".format(element.tag))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment