1# -*- coding: utf-8 -*-
2
3"""
4Common verification infrastructure for v2 streams
5"""
6
7from struct import calcsize, unpack
8
9class StreamError(Exception):
10    """Error with the stream"""
11    pass
12
13class RecordError(Exception):
14    """Error with a record in the stream"""
15    pass
16
17
18class VerifyBase(object):
19
20    def __init__(self, info, read):
21
22        self.info = info
23        self.read = read
24
25    def rdexact(self, nr_bytes):
26        """Read exactly nr_bytes from the stream"""
27        _ = self.read(nr_bytes)
28        if len(_) != nr_bytes:
29            raise IOError("Stream truncated")
30        return _
31
32    def unpack_exact(self, fmt):
33        """Unpack a struct format string from the stream"""
34        sz = calcsize(fmt)
35        return unpack(fmt, self.rdexact(sz))
36
37