Ejemplo n.º 1
0
 def __init__(self, e):
     if isinstance(e, str):
         try:
             e = xml.fromstring(e)
         except xml.ParseError:
             logger.warn("Skipping malformed line: {line}".format(line=e),
                         exc_info=True)
             return
     if isinstance(e, xml.Element):
         e = etree_to_dict(e)
     if isinstance(e, dict):
         e = DictObject.objectify(e)
     assert isinstance(e, DictObject)
     assert hasattr(e.item, "type")
     self.type = ascii_integers_to_string(e.item.type)
     self.code = ascii_integers_to_string(e.item.code)
     self.length = int(e.item.length)
     if "data" in e.item:
         assert self.length > 0  # length is zero if data is undefined.
         self.data = encoded_to_str(e.item.data["#text"],
                                    e.item.data["@encoding"],
                                    as_bytes=True)
         if e.item.data["@encoding"] == "base64":
             self._data_base64 = to_unicode(e.item.data["#text"])
         else:
             self._data_base64 = None
     else:
         assert self.length == 0  # length is zero if data is undefined.
         self.data = to_binary("")
         self._data_base64 = None
Ejemplo n.º 2
0
	def __init__(self, e):
		if isinstance(e, str):
			try:
				e = xml.fromstring(e)
			except xml.ParseError:
				logger.warn("Skipping malformed line: {line}".format(line=e), exc_info=True)
				return
		if isinstance(e, xml.Element):
			e = etree_to_dict(e)
		if isinstance(e, dict):
			e = DictObject.objectify(e)
		assert isinstance(e, DictObject)
		assert hasattr(e.item, "type")
		self.type = ascii_integers_to_string(e.item.type)
		self.code = ascii_integers_to_string(e.item.code)
		self.length = int(e.item.length)
		if "data" in e.item:
			assert self.length > 0  # length is zero if data is undefined.
			self.data = encoded_to_str(e.item.data["#text"], e.item.data["@encoding"], as_bytes=True)
			if e.item.data["@encoding"] == "base64":
				self._data_base64 = to_unicode(e.item.data["#text"])
			else:
				self._data_base64 = None
		else:
			assert self.length == 0  # length is zero if data is undefined.
			self.data = to_binary("")
			self._data_base64 = None
Ejemplo n.º 3
0
def encoded_to_str(data, encoding, as_bytes=True):
    if encoding == "base64":
        bytes = decodebytes(data.encode('ascii'))
        if as_bytes:
            return bytes
        else:
            return to_unicode(bytes)
    else:
        raise AttributeError("unknown encoding format: {f}".format(f=encoding))
Ejemplo n.º 4
0
def encoded_to_str(data, encoding, as_bytes=True):
	if encoding == "base64":
		bytes = decodebytes(data.encode('ascii'))
		if as_bytes:
			return bytes
		else:
			return to_unicode(bytes)
	else:
		raise AttributeError("unknown encoding format: {f}".format(f=encoding))
Ejemplo n.º 5
0
 def data_str(self):
     return to_unicode(self.data)
Ejemplo n.º 6
0
def base64url_encode(string: bytes) -> str:
    return to_unicode(base64.urlsafe_b64encode(string)).rstrip("=")
Ejemplo n.º 7
0
	def data_str(self):
		return to_unicode(self.data)