/
types.py
251 lines (194 loc) · 6.07 KB
/
types.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# -*- coding: utf-8 -*-
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from uuid import UUID
from datetime import datetime
from typing import Any, Callable, Optional, TypedDict, Union
from rucio.db.sqla.constants import AccountType, IdentityType
class InternalType(object):
'''
Base for Internal representations of string types
'''
def __init__(self, value, vo='def', fromExternal=True):
if value is None:
self.external = None
self.internal = None
self.vo = vo
elif not isinstance(value, str):
raise TypeError('Expected value to be string type, got %s' % type(value))
elif fromExternal:
self.external = value
self.vo = vo
self.internal = self._calc_internal()
else:
self.internal = value
vo, external = self._calc_external()
self.external = external
self.vo = vo
def __repr__(self):
return self.internal
def __str__(self):
return self.external
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.internal == other.internal
return NotImplemented
def __ne__(self, other):
val = self == other
if val is NotImplemented:
return NotImplemented
return not val
def __le__(self, other):
val = self.external <= other.external
if val is NotImplemented:
return NotImplemented
return val
def __lt__(self, other):
val = self.external < other.external
if val is NotImplemented:
return NotImplemented
return val
def __hash__(self):
return hash(self.internal)
def _calc_external(self):
''' Utility to convert between internal and external representations'''
split = self.internal.split('@', 1)
if len(split) == 1: # if cannot convert, vo is '' and this is single vo
vo = 'def'
external = split[0]
else:
vo = split[1]
external = split[0]
return vo, external
def _calc_internal(self):
''' Utility to convert between internal and external representations'''
if self.vo == 'def':
return self.external
internal = '{}@{}'.format(self.external, self.vo)
return internal
class InternalAccount(InternalType):
'''
Internal representation of an account
'''
def __init__(self, account, vo='def', fromExternal=True):
super(InternalAccount, self).__init__(value=account, vo=vo, fromExternal=fromExternal)
class InternalScope(InternalType):
'''
Internal representation of a scope
'''
def __init__(self, scope, vo='def', fromExternal=True):
super(InternalScope, self).__init__(value=scope, vo=vo, fromExternal=fromExternal)
LoggerFunction = Callable[..., Any]
class RSEDomainLANDict(TypedDict):
read: Optional[int]
write: Optional[int]
delete: Optional[int]
class RSEDomainWANDict(TypedDict):
read: Optional[int]
write: Optional[int]
delete: Optional[int]
third_party_copy_read: Optional[int]
third_party_copy_write: Optional[int]
class RSEDomainsDict(TypedDict):
lan: RSEDomainLANDict
wan: RSEDomainWANDict
class RSEProtocolDict(TypedDict):
auth_token: Optional[str] # FIXME: typing.NotRequired
hostname: str
scheme: str
port: int
prefix: str
impl: str
domains: RSEDomainsDict
extended_attributes: Optional[Union[str, dict[str, Any]]]
class RSESettingsDict(TypedDict):
availability_delete: bool
availability_read: bool
availability_write: bool
credentials: Optional[dict[str, Any]]
lfn2pfn_algorithm: str
qos_class: Optional[str]
staging_area: bool
rse_type: str
sign_url: Optional[str]
read_protocol: int
write_protocol: int
delete_protocol: int
third_party_copy_read_protocol: int
third_party_copy_write_protocol: int
id: str
rse: str
volatile: bool
verify_checksum: bool
deterministic: bool
domain: list[str]
protocols: list[RSEProtocolDict]
class RSEAccountCounterDict(TypedDict):
account: InternalAccount
rse_id: UUID
class RSEAccountUsageDict(TypedDict):
rse_id: UUID
rse: str
account: InternalAccount
used_files: int
used_bytes: int
quota_bytes: int
class RSEGlobalAccountUsageDict(TypedDict):
rse_expression: str
bytes: int
files: int
bytes_limit: int
bytes_remaining: int
class RSELocalAccountUsageDict(TypedDict):
rse_id: UUID
rse: str
bytes: int
files: int
bytes_limit: int
bytes_remaining: int
class RSEResolvedGlobalAccountLimitDict(TypedDict):
resolved_rses: str
resolved_rse_ids: list[UUID]
limit: float
class TokenDict(TypedDict):
token: str
expires_at: datetime
class TokenValidationDict(TypedDict):
account: InternalAccount
identity: str
lifetime: datetime
audience: str
authz_scope: str
class AccountDict(TypedDict):
account: InternalAccount
type: AccountType
email: str
class AccountAttributesDict(TypedDict):
key: str
value: Union[bool, str]
class IdentityDict(TypedDict):
type: IdentityType
identity: str
email: str
class UsageDict(TypedDict):
bytes: int
files: int
updated_at: Optional[datetime]
class TokenOIDCAutoDict(TypedDict):
webhome: Optional[str]
token: Optional[TokenDict]
class TokenOIDCNoAutoDict(TypedDict):
fetchcode: str
class TokenOIDCPollingDict(TypedDict):
polling: bool