File: setting.py

package info (click to toggle)
python-b2sdk 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,020 kB
  • sloc: python: 30,902; sh: 13; makefile: 8
file content (214 lines) | stat: -rw-r--r-- 7,344 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
######################################################################
#
# File: b2sdk/_internal/replication/setting.py
#
# Copyright 2022 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

import re
from builtins import classmethod
from dataclasses import dataclass, field
from typing import ClassVar


@dataclass
class ReplicationRule:
    """
    Hold information about replication rule: destination bucket, priority,
    prefix and rule name.
    """

    DEFAULT_PRIORITY: ClassVar[int] = 128

    destination_bucket_id: str
    name: str
    file_name_prefix: str = ''
    is_enabled: bool = True
    priority: int = DEFAULT_PRIORITY
    include_existing_files: bool = False

    REPLICATION_RULE_REGEX: ClassVar = re.compile(r'^[a-zA-Z0-9_\-]{1,64}$')
    MIN_PRIORITY: ClassVar[int] = 1
    MAX_PRIORITY: ClassVar[int] = 2**31 - 1

    def __post_init__(self):
        if not self.destination_bucket_id:
            raise ValueError('destination_bucket_id is required')

        if not self.REPLICATION_RULE_REGEX.match(self.name):
            raise ValueError('replication rule name is invalid')

        if not (self.MIN_PRIORITY <= self.priority <= self.MAX_PRIORITY):
            raise ValueError(
                'priority should be within [%d, %d] interval'
                % (
                    self.MIN_PRIORITY,
                    self.MAX_PRIORITY,
                )
            )

    def as_dict(self) -> dict:
        return {
            'destinationBucketId': self.destination_bucket_id,
            'fileNamePrefix': self.file_name_prefix,
            'includeExistingFiles': self.include_existing_files,
            'isEnabled': self.is_enabled,
            'priority': self.priority,
            'replicationRuleName': self.name,
        }

    @classmethod
    def from_dict(cls, value_dict: dict) -> ReplicationRule:
        kwargs = {}
        for field_, protocolField in (
            ('destination_bucket_id', 'destinationBucketId'),
            ('name', 'replicationRuleName'),
            ('file_name_prefix', 'fileNamePrefix'),
            ('include_existing_files', 'includeExistingFiles'),
            ('is_enabled', 'isEnabled'),
            ('priority', 'priority'),
        ):
            value = value_dict.get(
                protocolField
            )  # refactor to := when dropping Python 3.7, maybe even dict expression
            if value is not None:
                kwargs[field_] = value
        return cls(**kwargs)


@dataclass
class ReplicationConfiguration:
    """
    Hold information about bucket replication configuration
    """

    # configuration as source:
    rules: list[ReplicationRule] = field(default_factory=list)
    source_key_id: str | None = None
    # configuration as destination:
    source_to_destination_key_mapping: dict[str, str] = field(default_factory=dict)

    def __post_init__(self):
        if self.rules and not self.source_key_id:
            raise ValueError('source_key_id must not be empty')

        for source, destination in self.source_to_destination_key_mapping.items():
            if not source or not destination:
                raise ValueError(
                    f'source_to_destination_key_mapping must not contain \
                     empty keys or values: ({source}, {destination})'
                )

    @property
    def is_source(self) -> bool:
        return bool(self.source_key_id)

    def get_source_configuration_as_dict(self) -> dict:
        return {
            'rules': self.rules,
            'source_key_id': self.source_key_id,
        }

    @property
    def is_destination(self) -> bool:
        return bool(self.source_to_destination_key_mapping)

    def get_destination_configuration_as_dict(self) -> dict:
        return {
            'source_to_destination_key_mapping': self.source_to_destination_key_mapping,
        }

    def as_dict(self) -> dict:
        """
        Represent the setting as a dict, for example:

        .. code-block:: python

            {
                "asReplicationSource": {
                    "replicationRules": [
                        {
                            "destinationBucketId": "c5f35d53a90a7ea284fb0719",
                            "fileNamePrefix": "",
                            "includeExistingFiles": True,
                            "isEnabled": true,
                            "priority": 1,
                            "replicationRuleName": "replication-us-west"
                        },
                        {
                            "destinationBucketId": "55f34d53a96a7ea284fb0719",
                            "fileNamePrefix": "",
                            "includeExistingFiles": True,
                            "isEnabled": true,
                            "priority": 2,
                            "replicationRuleName": "replication-us-west-2"
                        }
                    ],
                    "sourceApplicationKeyId": "10053d55ae26b790000000006"
                },
                "asReplicationDestination": {
                    "sourceToDestinationKeyMapping": {
                        "10053d55ae26b790000000045": "10053d55ae26b790000000004",
                        "10053d55ae26b790000000046": "10053d55ae26b790030000004"
                    }
                }
            }

        """

        result = {
            'asReplicationSource': {
                'replicationRules': [rule.as_dict() for rule in self.rules],
                'sourceApplicationKeyId': self.source_key_id,
            }
            if self.is_source
            else None,
            'asReplicationDestination': {
                'sourceToDestinationKeyMapping': self.source_to_destination_key_mapping,
            }
            if self.is_destination
            else None,
        }

        return result

    serialize_to_json_for_request = as_dict

    @classmethod
    def from_dict(cls, value_dict: dict) -> ReplicationConfiguration:
        source_dict = value_dict.get('asReplicationSource') or {}
        destination_dict = value_dict.get('asReplicationDestination') or {}

        return cls(
            rules=[
                ReplicationRule.from_dict(rule_dict)
                for rule_dict in source_dict.get('replicationRules', [])
            ],
            source_key_id=source_dict.get('sourceApplicationKeyId'),
            source_to_destination_key_mapping=destination_dict.get('sourceToDestinationKeyMapping')
            or {},
        )


@dataclass
class ReplicationConfigurationFactory:
    is_client_authorized_to_read: bool
    value: ReplicationConfiguration | None

    @classmethod
    def from_bucket_dict(cls, bucket_dict: dict) -> ReplicationConfigurationFactory:
        """
        Returns ReplicationConfigurationFactory for the given bucket dict
        retrieved from the api.
        """
        replication_dict = bucket_dict.get('replicationConfiguration') or {}
        value_dict = replication_dict.get('value') or {}

        return cls(
            is_client_authorized_to_read=replication_dict.get('isClientAuthorizedToRead', True),
            value=ReplicationConfiguration.from_dict(value_dict),
        )