Exemplo n.º 1
0
 def to_bytes(self):
     """
     >>> XmlnsAttributeRecord('name', 'value').to_bytes()
     b'\\t\\x04name\\x05value'
     """
     bt = struct.pack(b"<B", self.type)
     bt += Utf8String(self.name).to_bytes()
     bt += Utf8String(self.value).to_bytes()
     return bytes(bt)
Exemplo n.º 2
0
 def parse(cls, fp):
     r"""
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\x04name\x05value')
     >>> str(XmlnsAttributeRecord.parse(fp))
     'xmlns:name="value"'
     """
     name = Utf8String.parse(fp).value
     value = Utf8String.parse(fp).value
     return cls(name, value)
Exemplo n.º 3
0
    def to_bytes(self):
        """
        >>> AttributeRecord('x', 'test', TrueTextRecord()).to_bytes()
        b'\\x05\\x01x\\x04test\\x86'
        """
        bt = super(AttributeRecord, self).to_bytes()
        bt += Utf8String(self.prefix).to_bytes()
        bt += Utf8String(self.name).to_bytes()
        bt += self.value.to_bytes()

        return bytes(bt)
Exemplo n.º 4
0
 def parse(cls, fp):
     r"""
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\x01x\x08Envelope')
     >>> er = ElementRecord.parse(fp)
     >>> str(er)
     '<x:Envelope>'
     """
     prefix = Utf8String.parse(fp).value
     name = Utf8String.parse(fp).value
     return cls(prefix, name)
Exemplo n.º 5
0
    def to_bytes(self):
        """
        >>> CommentRecord('test').to_bytes()
        b'\\x02\\x04test'
        """
        string = Utf8String(self.comment)

        return bytes(super(CommentRecord, self).to_bytes() + string.to_bytes())
Exemplo n.º 6
0
 def to_bytes(self):
     """
     >>> ShortXmlnsAttributeRecord('test').to_bytes()
     b'\\x08\\x04test'
     """
     bt = struct.pack(b"<B", self.type)
     bt += Utf8String(self.value).to_bytes()
     return bytes(bt)
Exemplo n.º 7
0
    def parse(cls, fp):
        """
        >>> from io import BytesIO
        >>> fp = BytesIO(b'\\x01x\\x04test\\x86')
        >>> ar = AttributeRecord.parse(fp)
        >>> str(ar.prefix)
        'x'
        >>> str(ar.name)
        'test'
        >>> ar.value
        <TrueTextRecord(type=0x86)>
        """
        prefix = Utf8String.parse(fp).value
        name = Utf8String.parse(fp).value
        type = struct.unpack(b"<B", fp.read(1))[0]
        value = Record.records[type].parse(fp)

        return cls(prefix, name, value)
Exemplo n.º 8
0
 def to_bytes(self):
     """
     >>> ElementRecord('x', 'Envelope').to_bytes()
     b'A\\x01x\\x08Envelope'
     """
     pref = Utf8String(self.prefix)
     data = super(ElementRecord, self).to_bytes()
     type = data[0:1]
     return bytes(type + pref.to_bytes() + data[1:])
Exemplo n.º 9
0
 def to_bytes(self):
     r"""
     >>> PrefixAttributeARecord('name', TrueTextRecord()).to_bytes()
     b'&\x04name\x86'
     """
     string = Utf8String(self.name)
     return bytes(
         struct.pack(b"<B", self.type) + string.to_bytes() +
         self.value.to_bytes())
Exemplo n.º 10
0
 def parse(cls, fp):
     r"""
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\x08Envelope')
     >>> ser = ShortElementRecord.parse(fp)
     >>> str(ser)
     '<Envelope>'
     """
     name = Utf8String.parse(fp).value
     return cls(name)
Exemplo n.º 11
0
 def parse(cls, fp):
     r"""
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\x04test')
     >>> pear = PrefixElementARecord.parse(fp)
     >>> str(pear)
     '<a:test>'
     """
     name = Utf8String.parse(fp).value
     return cls(name)
Exemplo n.º 12
0
    def to_bytes(self):
        """
        >>> DictionaryXmlnsAttributeRecord('a', 6).to_bytes()
        b'\\x0b\\x01\x61\\x06'
        """
        bt = struct.pack(b"<B", self.type)
        bt += Utf8String(self.prefix).to_bytes()
        bt += MultiByteInt31(self.index).to_bytes()

        return bytes(bt)
Exemplo n.º 13
0
 def parse(cls, fp):
     """
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\\x04test')
     >>> sxar = ShortXmlnsAttributeRecord.parse(fp)
     >>> str(sxar)
     'xmlns="test"'
     """
     value = Utf8String.parse(fp).value
     return cls(value)
Exemplo n.º 14
0
    def to_bytes(self):
        """
        >>> ShortAttributeRecord('test', TrueTextRecord()).to_bytes()
        b'\\x04\\x04test\\x86'
        """
        bt = super(ShortAttributeRecord, self).to_bytes()
        bt += Utf8String(self.name).to_bytes()
        bt += self.value.to_bytes()

        return bytes(bt)
Exemplo n.º 15
0
 def parse(cls, fp):
     r"""
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\x01x\x02')
     >>> sder = DictionaryElementRecord.parse(fp)
     >>> str(sder)
     '<x:Envelope>'
     """
     prefix = Utf8String.parse(fp).value
     index = MultiByteInt31.parse(fp).value
     return cls(prefix, index)
Exemplo n.º 16
0
    def to_bytes(self):
        """
        >>> DictionaryAttributeRecord('x', 2, TrueTextRecord()).to_bytes()
        b'\\x07\\x01x\\x02\\x86'
        """
        bt = super(DictionaryAttributeRecord, self).to_bytes()
        bt += Utf8String(self.prefix).to_bytes()
        bt += MultiByteInt31(self.index).to_bytes()
        bt += self.value.to_bytes()

        return bytes(bt)
Exemplo n.º 17
0
    def to_bytes(self):
        r"""
        >>> PrefixElementARecord('test').to_bytes()
        b'^\x04test'
        """
        string = Utf8String(self.name)

        bt = struct.pack(b"<B", self.type) + string.to_bytes()

        for attr in self.attributes:
            bt += attr.to_bytes()
        return bytes(bt)
Exemplo n.º 18
0
 def parse(cls, fp):
     r"""
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\x04name\x86')
     >>> paar = PrefixAttributeARecord.parse(fp)
     >>> str(paar)
     'a:name="true"'
     """
     name = Utf8String.parse(fp).value
     type = struct.unpack(b"<B", fp.read(1))[0]
     value = Record.records[type].parse(fp)
     return cls(name, value)
Exemplo n.º 19
0
    def to_bytes(self):
        """
        >>> ShortElementRecord('Envelope').to_bytes()
        b'@\\x08Envelope'
        """
        string = Utf8String(self.name)

        bt = super(ShortElementRecord, self).to_bytes() + string.to_bytes()

        for attr in self.attributes:
            bt += attr.to_bytes()
        return bytes(bt)
Exemplo n.º 20
0
    def to_bytes(self):
        """
        >>> DictionaryElementRecord('x', 2).to_bytes()
        b'C\\x01x\\x02'
        """
        pref = Utf8String(self.prefix)
        string = MultiByteInt31(self.index)

        bt = (super(DictionaryElementRecord, self).to_bytes() +
              pref.to_bytes() + string.to_bytes())

        for attr in self.attributes:
            bt += attr.to_bytes()
        return bytes(bt)
Exemplo n.º 21
0
 def parse(cls, fp):
     """
     >>> from io import BytesIO
     >>> fp = BytesIO(b'\\x01a\\x06')
     >>> dxar = DictionaryXmlnsAttributeRecord.parse(fp)
     >>> str(dxar.prefix)
     'a'
     >>> dxar.index
     6
     >>> str(dxar)
     'xmlns:a="http://www.w3.org/2005/08/addressing"'
     """
     prefix = Utf8String.parse(fp).value
     index = MultiByteInt31.parse(fp).value
     return cls(prefix, index)
Exemplo n.º 22
0
    def parse(cls, fp):
        """
        >>> from io import BytesIO
        >>> fp = BytesIO(b'\\x01x\\x02\\x86')
        >>> dar = DictionaryAttributeRecord.parse(fp)
        >>> str(dar.prefix)
        'x'
        >>> dar.index
        2
        >>> str(dar.value)
        'true'
        """
        prefix = Utf8String.parse(fp).value
        index = MultiByteInt31.parse(fp).value
        type = struct.unpack(b"<B", fp.read(1))[0]
        value = Record.records[type].parse(fp)

        return cls(prefix, index, value)
Exemplo n.º 23
0
def build_dictionary(fp, key):
    size = MultiByteInt31.parse(fp).value
    print("Dictionary table: {} bytes".format(size))
    table_data = fp.read(size)
    table = BytesIO(table_data)

    idx = 1
    while table.tell() < size:
        string = Utf8String.parse(table)
        assert idx not in dictionary_cache[key]
        dictionary_cache[key][idx] = string.value
        idx += 2
    dictionary.clear()
    dictionary.update(old_dictionary)
    dictionary.update(dictionary_cache[key])

    for idx, value in dictionary_cache[key].items():
        print('{}: {}'.format(idx, value))
    return dictionary_cache[key]
Exemplo n.º 24
0
def build_dictionary(fp, key):
    size = MultiByteInt31.parse(fp).value
    print("Dictionary table: {} bytes".format(size))
    table_data = fp.read(size)
    table = BytesIO(table_data)

    idx = 1
    while table.tell() < size:
        string = Utf8String.parse(table)
        assert idx not in dictionary_cache[key]
        dictionary_cache[key][idx] = string.value
        idx += 2
    dictionary.clear()
    dictionary.update(old_dictionary)
    dictionary.update(dictionary_cache[key])

    for idx, value in dictionary_cache[key].items():
        print('{}: {}'.format(idx, value))
    return dictionary_cache[key]
Exemplo n.º 25
0
 def parse(cls, fp):
     data = Utf8String.parse(fp).value
     return cls(data)