-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathastra.py
73 lines (58 loc) · 2.16 KB
/
astra.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
"""
Models used by Astra DB when working with storage.
"""
# Copyright (c) 2023-2024. ECCO Sneaks & Data
#
# 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.
#
import re
from dataclasses import dataclass
from pydoc import locate
from typing import Optional, TypeVar, Type
from adapta.storage.models.base import DataPath, DataProtocols
TModel = TypeVar("TModel") # pylint: disable=C0103
@dataclass
class AstraPath(DataPath):
"""
Path wrapper for Astra DB.
"""
def to_uri(self) -> str:
return self.to_hdfs_path()
def base_uri(self) -> str:
raise NotImplementedError
@classmethod
def from_uri(cls, url: str) -> "DataPath":
raise NotImplementedError
keyspace: str
table: str
model_class_name: Optional[str] = None
protocol: str = DataProtocols.ASTRA.value
@classmethod
def from_hdfs_path(cls, hdfs_path: str) -> "AstraPath":
path_regex = r"^(astra)(?:\+(.+))?:\/\/(.+)@(.+)$"
match = re.match(path_regex, hdfs_path)
assert match, f"Invalid path provided, must comply with: {path_regex}"
return cls(keyspace=match.group(3), table=match.group(4), model_class_name=match.group(2))
def to_hdfs_path(self) -> str:
if not self.model_class_name:
return f"astra://{self.keyspace}@{self.table}"
return f"astra+{self.model_class_name}://{self.keyspace}@{self.table}"
def to_delta_rs_path(self) -> str:
raise NotImplementedError
def model_class(self) -> Optional[Type[TModel]]:
"""
Locates and returns model class name.
"""
if self.model_class_name:
return locate(self.model_class_name)
return None