Skip to content

Commit

Permalink
Initial supportd for gzip compressed BEAM files
Browse files Browse the repository at this point in the history
Fixes #19
  • Loading branch information
matwey committed Aug 5, 2024
1 parent 66181a1 commit 44fb400
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pybeam/schema/beam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@
# THE SOFTWARE.
#

from construct import Adapter, Const, FocusedSeq, GreedyRange, Int32ub, Prefixed, Terminated
from construct import this
from construct import (
Adapter,
Bytes,
Compressed,
Const,
FocusedSeq,
GreedyRange,
Int32ub,
Peek,
Prefixed,
Switch,
Terminated,)

from pybeam.schema.beam.chunks import chunk

Expand All @@ -32,11 +44,20 @@ def _decode(self, obj, context, path):
def _encode(self, obj, context, path):
return obj.items()

beam = FocusedSeq("chunks",
uncompressed_beam = FocusedSeq("chunks",
Const(b'FOR1'),
"chunks" / Prefixed(Int32ub, FocusedSeq("chunks",
Const(b'BEAM'),
"chunks" / DictAdapter(GreedyRange(chunk)),
Terminated)))

gzip_compressed_beam = Compressed(uncompressed_beam, "gzip")

beam = FocusedSeq("beam",
"magic" / Peek(Bytes(2)),
"beam" / Switch(this.magic, {
b'FO' : uncompressed_beam,
b'\x1f\x8b' : gzip_compressed_beam,
}))

__all__ = ["beam"]
3 changes: 3 additions & 0 deletions test/schema_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def test_beam2(self):
def test_beam3(self):
c = beam
self.assertRaises(TerminatedError, lambda: c.parse(b'FOR1\x00\x00\x00\x0cBEAMAtU8\x00\x00\x002'))
def test_beam_compressed1(self):
c = beam
self.assertEqual(c.parse(b'\x1f\x8b\x08\x08\x9f\xf3\xb0f\x02\xffc.beam\x00s\xf3\x0f2d```qru\xf4\x05\x00\x86\x81S6\x0c\x00\x00\x00'), {})
def test_chunk_atom(self):
c = Atom
self.assertEqual(c.parse(b'\x00\x00\x00\x00'), [])
Expand Down

0 comments on commit 44fb400

Please sign in to comment.