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