python-suitcase icon indicating copy to clipboard operation
python-suitcase copied to clipboard

Describing a fixed length field

Open ocurero opened this issue 9 years ago • 3 comments
trafficstars

Hi,

How do I describe a fixed length character field that is always 8 bytes long?

Payload() works if the field is variable length but in my case I have a lot of fields that are fixed (padded with blanks if necessary).

Thanks!

ocurero avatar May 19 '16 09:05 ocurero

I think UBInt8Sequence should work for this. It appears that it doesn't show up in the docs as it is generated by a factory (https://digidotcom.github.io/python-suitcase/latest/_modules/suitcase/fields.html#BaseFixedByteSequence). It expects a sequence of bytes (e.g. [1, 2, 3, 4, 5, 6, 7, 8]), so some conversion will be required if you have an actual bytes payload.

posborne avatar May 19 '16 21:05 posborne

Thanks Paul!

It works, but something is still wrong. If I try the way you say:

IRM_RACF_GRNAME = BaseFixedByteSequence(lambda x: str(x) + 's', 8)

I get the following error:

  File "./testproto.py", line 153, in client
    printb(a.pack())
  File "/home/oscar/env/lib/python2.7/site-packages/suitcase/structure.py", line 303, in pack
    return self._packer.pack()
  File "/home/oscar/env/lib/python2.7/site-packages/suitcase/structure.py", line 28, in pack
    self.write(sio)
  File "/home/oscar/env/lib/python2.7/site-packages/suitcase/structure.py", line 41, in write
    field.pack(stream)
  File "/home/oscar/env/lib/python2.7/site-packages/suitcase/fields.py", line 889, in pack
    raise SuitcasePackStructException(e)
suitcase.exceptions.SuitcasePackStructException: pack requires exactly 1 arguments

To make it work I need to change the way self._value is passed to pack:

Original version not working (line 887 in fields.py): stream.write(struct.pack(self.format, *self._value))

Modified version working (same line 887 in fields.py): stream.write(struct.pack(self.format, self._value))

If the fix is ok, BaseVariableByteSequence has the same problem.

ocurero avatar May 20 '16 06:05 ocurero

ocurero's suggestion is a nice workaround, but it would be great if you could implement a FixedByteSequence(number_of_bytes) field in the next version of suitcase.

v3gard avatar Oct 05 '16 13:10 v3gard