summaryrefslogtreecommitdiff
path: root/obnamlib/bag.py
blob: 87bec27786ccb34d8884472c61b2a70595c667e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright 2015-2016  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import obnamlib


class Bag(object):

    def __init__(self):
        self._bag_id = None
        self._blobs = []
        self._blobs_bytes = 0

    def get_id(self):
        return self._bag_id

    def set_id(self, bag_id):
        assert isinstance(bag_id, (int, long, str))
        self._bag_id = bag_id

    def append(self, blob):
        if self.get_id() is None:
            raise BagIdNotSetError()
        self._blobs.append(blob)
        self._blobs_bytes += len(blob)
        return obnamlib.make_object_id(self.get_id(), len(self) - 1)

    def __len__(self):
        return len(self._blobs)

    def get_bytes(self):
        return self._blobs_bytes

    def __getitem__(self, index):
        return self._blobs[index]


class BagIdNotSetError(obnamlib.ObnamError):

    msg = 'Bag id not set: cannot append a blob (programming error)'


def make_object_id(bag_id, object_index):
    if isinstance(bag_id, str):
        assert object_index == 0
        return '%s.%d' % (bag_id, object_index)
    return '%016x.%d' % (bag_id, object_index)


def parse_object_id(object_id):
    parts = object_id.split('.', 1)
    return int(parts[0], 16), int(parts[1])