unstructured-ingest 0.2.0__py3-none-any.whl → 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of unstructured-ingest might be problematic. Click here for more details.
- test/integration/connectors/sql/test_singlestore.py +156 -0
- test/integration/connectors/test_confluence.py +113 -0
- test/integration/connectors/test_kafka.py +67 -0
- test/integration/connectors/test_onedrive.py +112 -0
- test/integration/connectors/test_qdrant.py +137 -0
- test/integration/connectors/test_s3.py +1 -1
- test/integration/connectors/utils/docker.py +2 -1
- test/integration/connectors/utils/docker_compose.py +23 -8
- test/integration/connectors/utils/validation.py +73 -22
- unstructured_ingest/__version__.py +1 -1
- unstructured_ingest/connector/kafka.py +0 -1
- unstructured_ingest/interfaces.py +7 -7
- unstructured_ingest/v2/interfaces/file_data.py +1 -0
- unstructured_ingest/v2/processes/chunker.py +2 -2
- unstructured_ingest/v2/processes/connectors/__init__.py +15 -7
- unstructured_ingest/v2/processes/connectors/astradb.py +278 -55
- unstructured_ingest/v2/processes/connectors/confluence.py +195 -0
- unstructured_ingest/v2/processes/connectors/databricks/volumes.py +5 -5
- unstructured_ingest/v2/processes/connectors/fsspec/fsspec.py +2 -10
- unstructured_ingest/v2/processes/connectors/gitlab.py +267 -0
- unstructured_ingest/v2/processes/connectors/kafka/__init__.py +13 -0
- unstructured_ingest/v2/processes/connectors/kafka/cloud.py +82 -0
- unstructured_ingest/v2/processes/connectors/kafka/kafka.py +196 -0
- unstructured_ingest/v2/processes/connectors/kafka/local.py +75 -0
- unstructured_ingest/v2/processes/connectors/onedrive.py +163 -2
- unstructured_ingest/v2/processes/connectors/qdrant/__init__.py +16 -0
- unstructured_ingest/v2/processes/connectors/qdrant/cloud.py +59 -0
- unstructured_ingest/v2/processes/connectors/qdrant/local.py +58 -0
- unstructured_ingest/v2/processes/connectors/qdrant/qdrant.py +168 -0
- unstructured_ingest/v2/processes/connectors/qdrant/server.py +60 -0
- unstructured_ingest/v2/processes/connectors/sql/__init__.py +5 -0
- unstructured_ingest/v2/processes/connectors/sql/postgres.py +1 -20
- unstructured_ingest/v2/processes/connectors/sql/singlestore.py +168 -0
- unstructured_ingest/v2/processes/connectors/sql/snowflake.py +5 -5
- unstructured_ingest/v2/processes/connectors/sql/sql.py +15 -6
- unstructured_ingest/v2/processes/partitioner.py +14 -3
- unstructured_ingest/v2/unstructured_api.py +25 -11
- {unstructured_ingest-0.2.0.dist-info → unstructured_ingest-0.2.2.dist-info}/METADATA +17 -17
- {unstructured_ingest-0.2.0.dist-info → unstructured_ingest-0.2.2.dist-info}/RECORD +43 -27
- unstructured_ingest/v2/processes/connectors/singlestore.py +0 -156
- {unstructured_ingest-0.2.0.dist-info → unstructured_ingest-0.2.2.dist-info}/LICENSE.md +0 -0
- {unstructured_ingest-0.2.0.dist-info → unstructured_ingest-0.2.2.dist-info}/WHEEL +0 -0
- {unstructured_ingest-0.2.0.dist-info → unstructured_ingest-0.2.2.dist-info}/entry_points.txt +0 -0
- {unstructured_ingest-0.2.0.dist-info → unstructured_ingest-0.2.2.dist-info}/top_level.txt +0 -0
|
@@ -1,31 +1,50 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
import csv
|
|
3
|
+
import hashlib
|
|
1
4
|
import json
|
|
5
|
+
import sys
|
|
2
6
|
from dataclasses import dataclass, field
|
|
3
7
|
from pathlib import Path
|
|
4
|
-
from
|
|
8
|
+
from time import time
|
|
9
|
+
from typing import TYPE_CHECKING, Any, Generator, Optional
|
|
5
10
|
|
|
6
11
|
from pydantic import Field, Secret
|
|
7
12
|
|
|
8
13
|
from unstructured_ingest import __name__ as integration_name
|
|
9
14
|
from unstructured_ingest.__version__ import __version__ as integration_version
|
|
10
|
-
from unstructured_ingest.error import
|
|
15
|
+
from unstructured_ingest.error import (
|
|
16
|
+
DestinationConnectionError,
|
|
17
|
+
SourceConnectionError,
|
|
18
|
+
SourceConnectionNetworkError,
|
|
19
|
+
)
|
|
11
20
|
from unstructured_ingest.utils.data_prep import batch_generator
|
|
12
21
|
from unstructured_ingest.utils.dep_check import requires_dependencies
|
|
13
22
|
from unstructured_ingest.v2.interfaces import (
|
|
14
23
|
AccessConfig,
|
|
15
24
|
ConnectionConfig,
|
|
25
|
+
Downloader,
|
|
26
|
+
DownloaderConfig,
|
|
27
|
+
DownloadResponse,
|
|
16
28
|
FileData,
|
|
29
|
+
FileDataSourceMetadata,
|
|
30
|
+
Indexer,
|
|
31
|
+
IndexerConfig,
|
|
17
32
|
Uploader,
|
|
18
33
|
UploaderConfig,
|
|
19
34
|
UploadStager,
|
|
20
35
|
UploadStagerConfig,
|
|
36
|
+
download_responses,
|
|
21
37
|
)
|
|
22
38
|
from unstructured_ingest.v2.logger import logger
|
|
23
39
|
from unstructured_ingest.v2.processes.connector_registry import (
|
|
24
40
|
DestinationRegistryEntry,
|
|
41
|
+
SourceRegistryEntry,
|
|
25
42
|
)
|
|
26
43
|
|
|
27
44
|
if TYPE_CHECKING:
|
|
45
|
+
from astrapy import AsyncCollection as AstraDBAsyncCollection
|
|
28
46
|
from astrapy import Collection as AstraDBCollection
|
|
47
|
+
from astrapy import DataAPIClient as AstraDBClient
|
|
29
48
|
|
|
30
49
|
|
|
31
50
|
CONNECTOR_TYPE = "astradb"
|
|
@@ -37,14 +56,253 @@ class AstraDBAccessConfig(AccessConfig):
|
|
|
37
56
|
|
|
38
57
|
|
|
39
58
|
class AstraDBConnectionConfig(ConnectionConfig):
|
|
40
|
-
|
|
59
|
+
connector_type: str = Field(default=CONNECTOR_TYPE, init=False)
|
|
41
60
|
access_config: Secret[AstraDBAccessConfig]
|
|
42
61
|
|
|
62
|
+
@requires_dependencies(["astrapy"], extras="astradb")
|
|
63
|
+
def get_client(self) -> "AstraDBClient":
|
|
64
|
+
from astrapy import DataAPIClient as AstraDBClient
|
|
65
|
+
|
|
66
|
+
# Create a client object to interact with the Astra DB
|
|
67
|
+
# caller_name/version for Astra DB tracking
|
|
68
|
+
return AstraDBClient(
|
|
69
|
+
caller_name=integration_name,
|
|
70
|
+
caller_version=integration_version,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def get_astra_collection(
|
|
75
|
+
connection_config: AstraDBConnectionConfig,
|
|
76
|
+
collection_name: str,
|
|
77
|
+
keyspace: str,
|
|
78
|
+
) -> "AstraDBCollection":
|
|
79
|
+
# Build the Astra DB object.
|
|
80
|
+
access_configs = connection_config.access_config.get_secret_value()
|
|
81
|
+
|
|
82
|
+
# Create a client object to interact with the Astra DB
|
|
83
|
+
# caller_name/version for Astra DB tracking
|
|
84
|
+
client = connection_config.get_client()
|
|
85
|
+
|
|
86
|
+
# Get the database object
|
|
87
|
+
astra_db = client.get_database(
|
|
88
|
+
api_endpoint=access_configs.api_endpoint,
|
|
89
|
+
token=access_configs.token,
|
|
90
|
+
keyspace=keyspace,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
# Connect to the collection
|
|
94
|
+
astra_db_collection = astra_db.get_collection(name=collection_name)
|
|
95
|
+
return astra_db_collection
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def get_async_astra_collection(
|
|
99
|
+
connection_config: AstraDBConnectionConfig,
|
|
100
|
+
collection_name: str,
|
|
101
|
+
keyspace: str,
|
|
102
|
+
) -> "AstraDBAsyncCollection":
|
|
103
|
+
# Build the Astra DB object.
|
|
104
|
+
access_configs = connection_config.access_config.get_secret_value()
|
|
105
|
+
|
|
106
|
+
# Create a client object to interact with the Astra DB
|
|
107
|
+
client = connection_config.get_client()
|
|
108
|
+
|
|
109
|
+
# Get the async database object
|
|
110
|
+
async_astra_db = client.get_async_database(
|
|
111
|
+
api_endpoint=access_configs.api_endpoint,
|
|
112
|
+
token=access_configs.token,
|
|
113
|
+
keyspace=keyspace,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
# Get async collection from AsyncDatabase
|
|
117
|
+
async_astra_db_collection = await async_astra_db.get_collection(name=collection_name)
|
|
118
|
+
return async_astra_db_collection
|
|
119
|
+
|
|
43
120
|
|
|
44
121
|
class AstraDBUploadStagerConfig(UploadStagerConfig):
|
|
45
122
|
pass
|
|
46
123
|
|
|
47
124
|
|
|
125
|
+
class AstraDBIndexerConfig(IndexerConfig):
|
|
126
|
+
collection_name: str = Field(
|
|
127
|
+
description="The name of the Astra DB collection. "
|
|
128
|
+
"Note that the collection name must only include letters, "
|
|
129
|
+
"numbers, and underscores."
|
|
130
|
+
)
|
|
131
|
+
keyspace: Optional[str] = Field(default=None, description="The Astra DB connection keyspace.")
|
|
132
|
+
namespace: Optional[str] = Field(
|
|
133
|
+
default=None,
|
|
134
|
+
description="The Astra DB connection namespace.",
|
|
135
|
+
deprecated="Please use 'keyspace' instead.",
|
|
136
|
+
)
|
|
137
|
+
batch_size: int = Field(default=20, description="Number of records per batch")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class AstraDBDownloaderConfig(DownloaderConfig):
|
|
141
|
+
fields: list[str] = field(default_factory=list)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class AstraDBUploaderConfig(UploaderConfig):
|
|
145
|
+
collection_name: str = Field(
|
|
146
|
+
description="The name of the Astra DB collection. "
|
|
147
|
+
"Note that the collection name must only include letters, "
|
|
148
|
+
"numbers, and underscores."
|
|
149
|
+
)
|
|
150
|
+
embedding_dimension: int = Field(
|
|
151
|
+
default=384, description="The dimensionality of the embeddings"
|
|
152
|
+
)
|
|
153
|
+
keyspace: Optional[str] = Field(default=None, description="The Astra DB connection keyspace.")
|
|
154
|
+
namespace: Optional[str] = Field(
|
|
155
|
+
default=None,
|
|
156
|
+
description="The Astra DB connection namespace.",
|
|
157
|
+
deprecated="Please use 'keyspace' instead.",
|
|
158
|
+
)
|
|
159
|
+
requested_indexing_policy: Optional[dict[str, Any]] = Field(
|
|
160
|
+
default=None,
|
|
161
|
+
description="The indexing policy to use for the collection.",
|
|
162
|
+
examples=['{"deny": ["metadata"]}'],
|
|
163
|
+
)
|
|
164
|
+
batch_size: int = Field(default=20, description="Number of records per batch")
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@dataclass
|
|
168
|
+
class AstraDBIndexer(Indexer):
|
|
169
|
+
connection_config: AstraDBConnectionConfig
|
|
170
|
+
index_config: AstraDBIndexerConfig
|
|
171
|
+
|
|
172
|
+
def get_collection(self) -> "AstraDBCollection":
|
|
173
|
+
return get_astra_collection(
|
|
174
|
+
connection_config=self.connection_config,
|
|
175
|
+
collection_name=self.index_config.collection_name,
|
|
176
|
+
keyspace=self.index_config.keyspace or self.index_config.namespace,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
def precheck(self) -> None:
|
|
180
|
+
try:
|
|
181
|
+
self.get_collection()
|
|
182
|
+
except Exception as e:
|
|
183
|
+
logger.error(f"Failed to validate connection {e}", exc_info=True)
|
|
184
|
+
raise SourceConnectionError(f"failed to validate connection: {e}")
|
|
185
|
+
|
|
186
|
+
def _get_doc_ids(self) -> set[str]:
|
|
187
|
+
"""Fetches all document ids in an index"""
|
|
188
|
+
# Initialize set of ids
|
|
189
|
+
ids = set()
|
|
190
|
+
|
|
191
|
+
# Get the collection
|
|
192
|
+
collection = self.get_collection()
|
|
193
|
+
|
|
194
|
+
# Perform the find operation to get all items
|
|
195
|
+
astra_db_docs_cursor = collection.find({}, projection={"_id": True})
|
|
196
|
+
|
|
197
|
+
# Iterate over the cursor
|
|
198
|
+
astra_db_docs = []
|
|
199
|
+
for result in astra_db_docs_cursor:
|
|
200
|
+
astra_db_docs.append(result)
|
|
201
|
+
|
|
202
|
+
# Create file data for each astra record
|
|
203
|
+
for astra_record in astra_db_docs:
|
|
204
|
+
ids.add(astra_record["_id"])
|
|
205
|
+
|
|
206
|
+
return ids
|
|
207
|
+
|
|
208
|
+
def run(self, **kwargs: Any) -> Generator[FileData, None, None]:
|
|
209
|
+
all_ids = self._get_doc_ids()
|
|
210
|
+
ids = list(all_ids)
|
|
211
|
+
id_batches = batch_generator(ids, self.index_config.batch_size)
|
|
212
|
+
|
|
213
|
+
for batch in id_batches:
|
|
214
|
+
# Make sure the hash is always a positive number to create identified
|
|
215
|
+
identified = str(hash(batch) + sys.maxsize + 1)
|
|
216
|
+
fd = FileData(
|
|
217
|
+
identifier=identified,
|
|
218
|
+
connector_type=CONNECTOR_TYPE,
|
|
219
|
+
doc_type="batch",
|
|
220
|
+
metadata=FileDataSourceMetadata(
|
|
221
|
+
date_processed=str(time()),
|
|
222
|
+
),
|
|
223
|
+
additional_metadata={
|
|
224
|
+
"ids": list(batch),
|
|
225
|
+
"collection_name": self.index_config.collection_name,
|
|
226
|
+
"keyspace": self.index_config.keyspace or self.index_config.namespace,
|
|
227
|
+
},
|
|
228
|
+
)
|
|
229
|
+
yield fd
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
@dataclass
|
|
233
|
+
class AstraDBDownloader(Downloader):
|
|
234
|
+
connection_config: AstraDBConnectionConfig
|
|
235
|
+
download_config: AstraDBDownloaderConfig
|
|
236
|
+
connector_type: str = CONNECTOR_TYPE
|
|
237
|
+
|
|
238
|
+
def is_async(self) -> bool:
|
|
239
|
+
return True
|
|
240
|
+
|
|
241
|
+
def get_identifier(self, record_id: str) -> str:
|
|
242
|
+
f = f"{record_id}"
|
|
243
|
+
if self.download_config.fields:
|
|
244
|
+
f = "{}-{}".format(
|
|
245
|
+
f,
|
|
246
|
+
hashlib.sha256(",".join(self.download_config.fields).encode()).hexdigest()[:8],
|
|
247
|
+
)
|
|
248
|
+
return f
|
|
249
|
+
|
|
250
|
+
def write_astra_result_to_csv(self, astra_result: dict, download_path: str) -> None:
|
|
251
|
+
with open(download_path, "w", encoding="utf8") as f:
|
|
252
|
+
writer = csv.writer(f)
|
|
253
|
+
writer.writerow(astra_result.keys())
|
|
254
|
+
writer.writerow(astra_result.values())
|
|
255
|
+
|
|
256
|
+
def generate_download_response(self, result: dict, file_data: FileData) -> DownloadResponse:
|
|
257
|
+
record_id = result["_id"]
|
|
258
|
+
filename_id = self.get_identifier(record_id=record_id)
|
|
259
|
+
filename = f"{filename_id}.csv" # csv to preserve column info
|
|
260
|
+
download_path = self.download_dir / Path(filename)
|
|
261
|
+
logger.debug(f"Downloading results from record {record_id} as csv to {download_path}")
|
|
262
|
+
download_path.parent.mkdir(parents=True, exist_ok=True)
|
|
263
|
+
try:
|
|
264
|
+
self.write_astra_result_to_csv(astra_result=result, download_path=download_path)
|
|
265
|
+
except Exception as e:
|
|
266
|
+
logger.error(
|
|
267
|
+
f"failed to download from record {record_id} to {download_path}: {e}",
|
|
268
|
+
exc_info=True,
|
|
269
|
+
)
|
|
270
|
+
raise SourceConnectionNetworkError(f"failed to download file {file_data.identifier}")
|
|
271
|
+
|
|
272
|
+
# modify input file_data for download_response
|
|
273
|
+
copied_file_data = copy.deepcopy(file_data)
|
|
274
|
+
copied_file_data.identifier = filename
|
|
275
|
+
copied_file_data.doc_type = "file"
|
|
276
|
+
copied_file_data.metadata.date_processed = str(time())
|
|
277
|
+
copied_file_data.metadata.record_locator = {"document_id": record_id}
|
|
278
|
+
copied_file_data.additional_metadata.pop("ids", None)
|
|
279
|
+
return super().generate_download_response(
|
|
280
|
+
file_data=copied_file_data, download_path=download_path
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
def run(self, file_data: FileData, **kwargs: Any) -> download_responses:
|
|
284
|
+
raise NotImplementedError("Use astradb run_async instead")
|
|
285
|
+
|
|
286
|
+
async def run_async(self, file_data: FileData, **kwargs: Any) -> download_responses:
|
|
287
|
+
# Get metadata from file_data
|
|
288
|
+
ids: list[str] = file_data.additional_metadata["ids"]
|
|
289
|
+
collection_name: str = file_data.additional_metadata["collection_name"]
|
|
290
|
+
keyspace: str = file_data.additional_metadata["keyspace"]
|
|
291
|
+
|
|
292
|
+
# Retrieve results from async collection
|
|
293
|
+
download_responses = []
|
|
294
|
+
async_astra_collection = await get_async_astra_collection(
|
|
295
|
+
connection_config=self.connection_config,
|
|
296
|
+
collection_name=collection_name,
|
|
297
|
+
keyspace=keyspace,
|
|
298
|
+
)
|
|
299
|
+
async for result in async_astra_collection.find({"_id": {"$in": ids}}):
|
|
300
|
+
download_responses.append(
|
|
301
|
+
self.generate_download_response(result=result, file_data=file_data)
|
|
302
|
+
)
|
|
303
|
+
return download_responses
|
|
304
|
+
|
|
305
|
+
|
|
48
306
|
@dataclass
|
|
49
307
|
class AstraDBUploadStager(UploadStager):
|
|
50
308
|
upload_stager_config: AstraDBUploadStagerConfig = field(
|
|
@@ -77,29 +335,6 @@ class AstraDBUploadStager(UploadStager):
|
|
|
77
335
|
return output_path
|
|
78
336
|
|
|
79
337
|
|
|
80
|
-
class AstraDBUploaderConfig(UploaderConfig):
|
|
81
|
-
collection_name: str = Field(
|
|
82
|
-
description="The name of the Astra DB collection. "
|
|
83
|
-
"Note that the collection name must only include letters, "
|
|
84
|
-
"numbers, and underscores."
|
|
85
|
-
)
|
|
86
|
-
embedding_dimension: int = Field(
|
|
87
|
-
default=384, description="The dimensionality of the embeddings"
|
|
88
|
-
)
|
|
89
|
-
keyspace: Optional[str] = Field(default=None, description="The Astra DB connection keyspace.")
|
|
90
|
-
namespace: Optional[str] = Field(
|
|
91
|
-
default=None,
|
|
92
|
-
description="The Astra DB connection namespace.",
|
|
93
|
-
deprecated="Please use 'keyspace' instead.",
|
|
94
|
-
)
|
|
95
|
-
requested_indexing_policy: Optional[dict[str, Any]] = Field(
|
|
96
|
-
default=None,
|
|
97
|
-
description="The indexing policy to use for the collection.",
|
|
98
|
-
examples=['{"deny": ["metadata"]}'],
|
|
99
|
-
)
|
|
100
|
-
batch_size: int = Field(default=20, description="Number of records per batch")
|
|
101
|
-
|
|
102
|
-
|
|
103
338
|
@dataclass
|
|
104
339
|
class AstraDBUploader(Uploader):
|
|
105
340
|
connection_config: AstraDBConnectionConfig
|
|
@@ -108,43 +343,23 @@ class AstraDBUploader(Uploader):
|
|
|
108
343
|
|
|
109
344
|
def precheck(self) -> None:
|
|
110
345
|
try:
|
|
111
|
-
|
|
346
|
+
get_astra_collection(
|
|
347
|
+
connection_config=self.connection_config,
|
|
348
|
+
collection_name=self.upload_config.collection_name,
|
|
349
|
+
keyspace=self.upload_config.keyspace or self.upload_config.namespace,
|
|
350
|
+
)
|
|
112
351
|
except Exception as e:
|
|
113
352
|
logger.error(f"Failed to validate connection {e}", exc_info=True)
|
|
114
353
|
raise DestinationConnectionError(f"failed to validate connection: {e}")
|
|
115
354
|
|
|
116
355
|
@requires_dependencies(["astrapy"], extras="astradb")
|
|
117
356
|
def get_collection(self) -> "AstraDBCollection":
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
# Get the collection_name
|
|
124
|
-
collection_name = self.upload_config.collection_name
|
|
125
|
-
|
|
126
|
-
# Build the Astra DB object.
|
|
127
|
-
access_configs = self.connection_config.access_config.get_secret_value()
|
|
128
|
-
|
|
129
|
-
# Create a client object to interact with the Astra DB
|
|
130
|
-
# caller_name/version for Astra DB tracking
|
|
131
|
-
my_client = AstraDBClient(
|
|
132
|
-
caller_name=integration_name,
|
|
133
|
-
caller_version=integration_version,
|
|
134
|
-
)
|
|
135
|
-
|
|
136
|
-
# Get the database object
|
|
137
|
-
astra_db = my_client.get_database(
|
|
138
|
-
api_endpoint=access_configs.api_endpoint,
|
|
139
|
-
token=access_configs.token,
|
|
140
|
-
keyspace=keyspace_param,
|
|
357
|
+
return get_astra_collection(
|
|
358
|
+
connection_config=self.connection_config,
|
|
359
|
+
collection_name=self.upload_config.collection_name,
|
|
360
|
+
keyspace=self.upload_config.keyspace or self.upload_config.namespace,
|
|
141
361
|
)
|
|
142
362
|
|
|
143
|
-
# Connect to the newly created collection
|
|
144
|
-
astra_db_collection = astra_db.get_collection(name=collection_name)
|
|
145
|
-
|
|
146
|
-
return astra_db_collection
|
|
147
|
-
|
|
148
363
|
def run(self, path: Path, file_data: FileData, **kwargs: Any) -> None:
|
|
149
364
|
with path.open("r") as file:
|
|
150
365
|
elements_dict = json.load(file)
|
|
@@ -160,6 +375,14 @@ class AstraDBUploader(Uploader):
|
|
|
160
375
|
collection.insert_many(chunk)
|
|
161
376
|
|
|
162
377
|
|
|
378
|
+
astra_db_source_entry = SourceRegistryEntry(
|
|
379
|
+
indexer=AstraDBIndexer,
|
|
380
|
+
indexer_config=AstraDBIndexerConfig,
|
|
381
|
+
downloader=AstraDBDownloader,
|
|
382
|
+
downloader_config=AstraDBDownloaderConfig,
|
|
383
|
+
connection_config=AstraDBConnectionConfig,
|
|
384
|
+
)
|
|
385
|
+
|
|
163
386
|
astra_db_destination_entry = DestinationRegistryEntry(
|
|
164
387
|
connection_config=AstraDBConnectionConfig,
|
|
165
388
|
upload_stager_config=AstraDBUploadStagerConfig,
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import TYPE_CHECKING, Generator, List, Optional
|
|
4
|
+
|
|
5
|
+
from pydantic import Field, Secret
|
|
6
|
+
|
|
7
|
+
from unstructured_ingest.error import SourceConnectionError
|
|
8
|
+
from unstructured_ingest.utils.dep_check import requires_dependencies
|
|
9
|
+
from unstructured_ingest.v2.interfaces import (
|
|
10
|
+
AccessConfig,
|
|
11
|
+
ConnectionConfig,
|
|
12
|
+
Downloader,
|
|
13
|
+
DownloaderConfig,
|
|
14
|
+
FileData,
|
|
15
|
+
FileDataSourceMetadata,
|
|
16
|
+
Indexer,
|
|
17
|
+
IndexerConfig,
|
|
18
|
+
SourceIdentifiers,
|
|
19
|
+
download_responses,
|
|
20
|
+
)
|
|
21
|
+
from unstructured_ingest.v2.logger import logger
|
|
22
|
+
from unstructured_ingest.v2.processes.connector_registry import (
|
|
23
|
+
SourceRegistryEntry,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from atlassian import Confluence
|
|
28
|
+
|
|
29
|
+
CONNECTOR_TYPE = "confluence"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ConfluenceAccessConfig(AccessConfig):
|
|
33
|
+
api_token: str = Field(description="Confluence API token")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ConfluenceConnectionConfig(ConnectionConfig):
|
|
37
|
+
url: str = Field(description="URL of the Confluence instance")
|
|
38
|
+
user_email: str = Field(description="User email for authentication")
|
|
39
|
+
access_config: Secret[ConfluenceAccessConfig] = Field(
|
|
40
|
+
description="Access configuration for Confluence"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
@requires_dependencies(["atlassian"], extras="confluence")
|
|
44
|
+
def get_client(self) -> "Confluence":
|
|
45
|
+
from atlassian import Confluence
|
|
46
|
+
|
|
47
|
+
access_configs = self.access_config.get_secret_value()
|
|
48
|
+
return Confluence(
|
|
49
|
+
url=self.url,
|
|
50
|
+
username=self.user_email,
|
|
51
|
+
password=access_configs.api_token,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class ConfluenceIndexerConfig(IndexerConfig):
|
|
56
|
+
max_num_of_spaces: int = Field(500, description="Maximum number of spaces to index")
|
|
57
|
+
max_num_of_docs_from_each_space: int = Field(
|
|
58
|
+
100, description="Maximum number of documents to fetch from each space"
|
|
59
|
+
)
|
|
60
|
+
spaces: Optional[List[str]] = Field(None, description="List of specific space keys to index")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass
|
|
64
|
+
class ConfluenceIndexer(Indexer):
|
|
65
|
+
connection_config: ConfluenceConnectionConfig
|
|
66
|
+
index_config: ConfluenceIndexerConfig
|
|
67
|
+
connector_type: str = CONNECTOR_TYPE
|
|
68
|
+
|
|
69
|
+
def precheck(self) -> bool:
|
|
70
|
+
try:
|
|
71
|
+
|
|
72
|
+
# Attempt to retrieve a list of spaces with limit=1.
|
|
73
|
+
# This should only succeed if all creds are valid
|
|
74
|
+
client = self.connection_config.get_client()
|
|
75
|
+
client.get_all_spaces(limit=1)
|
|
76
|
+
logger.info("Connection to Confluence successful.")
|
|
77
|
+
return True
|
|
78
|
+
except Exception as e:
|
|
79
|
+
logger.error(f"Failed to connect to Confluence: {e}", exc_info=True)
|
|
80
|
+
raise SourceConnectionError(f"Failed to connect to Confluence: {e}")
|
|
81
|
+
|
|
82
|
+
def _get_space_ids(self) -> List[str]:
|
|
83
|
+
spaces = self.index_config.spaces
|
|
84
|
+
if spaces:
|
|
85
|
+
return spaces
|
|
86
|
+
else:
|
|
87
|
+
client = self.connection_config.get_client()
|
|
88
|
+
all_spaces = client.get_all_spaces(limit=self.index_config.max_num_of_spaces)
|
|
89
|
+
space_ids = [space["key"] for space in all_spaces["results"]]
|
|
90
|
+
return space_ids
|
|
91
|
+
|
|
92
|
+
def _get_docs_ids_within_one_space(self, space_id: str) -> List[dict]:
|
|
93
|
+
client = self.connection_config.get_client()
|
|
94
|
+
pages = client.get_all_pages_from_space(
|
|
95
|
+
space=space_id,
|
|
96
|
+
start=0,
|
|
97
|
+
limit=self.index_config.max_num_of_docs_from_each_space,
|
|
98
|
+
expand=None,
|
|
99
|
+
content_type="page",
|
|
100
|
+
status=None,
|
|
101
|
+
)
|
|
102
|
+
doc_ids = [{"space_id": space_id, "doc_id": page["id"]} for page in pages]
|
|
103
|
+
return doc_ids
|
|
104
|
+
|
|
105
|
+
def run(self) -> Generator[FileData, None, None]:
|
|
106
|
+
from time import time
|
|
107
|
+
|
|
108
|
+
space_ids = self._get_space_ids()
|
|
109
|
+
for space_id in space_ids:
|
|
110
|
+
doc_ids = self._get_docs_ids_within_one_space(space_id)
|
|
111
|
+
for doc in doc_ids:
|
|
112
|
+
doc_id = doc["doc_id"]
|
|
113
|
+
# Build metadata
|
|
114
|
+
metadata = FileDataSourceMetadata(
|
|
115
|
+
date_processed=str(time()),
|
|
116
|
+
url=f"{self.connection_config.url}/pages/{doc_id}",
|
|
117
|
+
record_locator={
|
|
118
|
+
"space_id": space_id,
|
|
119
|
+
"document_id": doc_id,
|
|
120
|
+
},
|
|
121
|
+
)
|
|
122
|
+
additional_metadata = {
|
|
123
|
+
"space_id": space_id,
|
|
124
|
+
"document_id": doc_id,
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
# Construct relative path and filename
|
|
128
|
+
filename = f"{doc_id}.html"
|
|
129
|
+
relative_path = str(Path(space_id) / filename)
|
|
130
|
+
|
|
131
|
+
source_identifiers = SourceIdentifiers(
|
|
132
|
+
filename=filename,
|
|
133
|
+
fullpath=relative_path,
|
|
134
|
+
rel_path=relative_path,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
file_data = FileData(
|
|
138
|
+
identifier=doc_id,
|
|
139
|
+
connector_type=self.connector_type,
|
|
140
|
+
metadata=metadata,
|
|
141
|
+
additional_metadata=additional_metadata,
|
|
142
|
+
source_identifiers=source_identifiers,
|
|
143
|
+
)
|
|
144
|
+
yield file_data
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class ConfluenceDownloaderConfig(DownloaderConfig):
|
|
148
|
+
pass
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
@dataclass
|
|
152
|
+
class ConfluenceDownloader(Downloader):
|
|
153
|
+
connection_config: ConfluenceConnectionConfig
|
|
154
|
+
download_config: ConfluenceDownloaderConfig = field(default_factory=ConfluenceDownloaderConfig)
|
|
155
|
+
connector_type: str = CONNECTOR_TYPE
|
|
156
|
+
|
|
157
|
+
def run(self, file_data: FileData, **kwargs) -> download_responses:
|
|
158
|
+
doc_id = file_data.identifier
|
|
159
|
+
try:
|
|
160
|
+
client = self.connection_config.get_client()
|
|
161
|
+
page = client.get_page_by_id(
|
|
162
|
+
page_id=doc_id,
|
|
163
|
+
expand="history.lastUpdated,version,body.view",
|
|
164
|
+
)
|
|
165
|
+
except Exception as e:
|
|
166
|
+
logger.error(f"Failed to retrieve page with ID {doc_id}: {e}", exc_info=True)
|
|
167
|
+
raise SourceConnectionError(f"Failed to retrieve page with ID {doc_id}: {e}")
|
|
168
|
+
|
|
169
|
+
if not page:
|
|
170
|
+
raise ValueError(f"Page with ID {doc_id} does not exist.")
|
|
171
|
+
|
|
172
|
+
content = page["body"]["view"]["value"]
|
|
173
|
+
|
|
174
|
+
filepath = file_data.source_identifiers.relative_path
|
|
175
|
+
download_path = Path(self.download_dir) / filepath
|
|
176
|
+
download_path.parent.mkdir(parents=True, exist_ok=True)
|
|
177
|
+
with open(download_path, "w", encoding="utf8") as f:
|
|
178
|
+
f.write(content)
|
|
179
|
+
|
|
180
|
+
# Update file_data with metadata
|
|
181
|
+
file_data.metadata.date_created = page["history"]["createdDate"]
|
|
182
|
+
file_data.metadata.date_modified = page["version"]["when"]
|
|
183
|
+
file_data.metadata.version = str(page["version"]["number"])
|
|
184
|
+
file_data.display_name = page["title"]
|
|
185
|
+
|
|
186
|
+
return self.generate_download_response(file_data=file_data, download_path=download_path)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
confluence_source_entry = SourceRegistryEntry(
|
|
190
|
+
connection_config=ConfluenceConnectionConfig,
|
|
191
|
+
indexer_config=ConfluenceIndexerConfig,
|
|
192
|
+
indexer=ConfluenceIndexer,
|
|
193
|
+
downloader_config=ConfluenceDownloaderConfig,
|
|
194
|
+
downloader=ConfluenceDownloader,
|
|
195
|
+
)
|
|
@@ -148,9 +148,7 @@ class DatabricksVolumesDownloader(Downloader, ABC):
|
|
|
148
148
|
|
|
149
149
|
|
|
150
150
|
class DatabricksVolumesUploaderConfig(UploaderConfig, DatabricksPathMixin):
|
|
151
|
-
|
|
152
|
-
default=False, description="If true, an existing file will be overwritten."
|
|
153
|
-
)
|
|
151
|
+
pass
|
|
154
152
|
|
|
155
153
|
|
|
156
154
|
@dataclass
|
|
@@ -166,10 +164,12 @@ class DatabricksVolumesUploader(Uploader, ABC):
|
|
|
166
164
|
raise DestinationConnectionError(f"failed to validate connection: {e}")
|
|
167
165
|
|
|
168
166
|
def run(self, path: Path, file_data: FileData, **kwargs: Any) -> None:
|
|
169
|
-
output_path = os.path.join(
|
|
167
|
+
output_path = os.path.join(
|
|
168
|
+
self.upload_config.path, f"{file_data.source_identifiers.filename}.json"
|
|
169
|
+
)
|
|
170
170
|
with open(path, "rb") as elements_file:
|
|
171
171
|
self.connection_config.get_client().files.upload(
|
|
172
172
|
file_path=output_path,
|
|
173
173
|
contents=elements_file,
|
|
174
|
-
overwrite=
|
|
174
|
+
overwrite=True,
|
|
175
175
|
)
|
|
@@ -176,6 +176,7 @@ class FsspecIndexer(Indexer):
|
|
|
176
176
|
),
|
|
177
177
|
metadata=self.get_metadata(file_data=file_data),
|
|
178
178
|
additional_metadata=additional_metadata,
|
|
179
|
+
display_name=file_path,
|
|
179
180
|
)
|
|
180
181
|
|
|
181
182
|
|
|
@@ -230,9 +231,7 @@ class FsspecDownloader(Downloader):
|
|
|
230
231
|
|
|
231
232
|
|
|
232
233
|
class FsspecUploaderConfig(FileConfig, UploaderConfig):
|
|
233
|
-
|
|
234
|
-
default=False, description="If true, an existing file will be overwritten."
|
|
235
|
-
)
|
|
234
|
+
pass
|
|
236
235
|
|
|
237
236
|
|
|
238
237
|
FsspecUploaderConfigT = TypeVar("FsspecUploaderConfigT", bound=FsspecUploaderConfig)
|
|
@@ -287,9 +286,6 @@ class FsspecUploader(Uploader):
|
|
|
287
286
|
def run(self, path: Path, file_data: FileData, **kwargs: Any) -> None:
|
|
288
287
|
path_str = str(path.resolve())
|
|
289
288
|
upload_path = self.get_upload_path(file_data=file_data)
|
|
290
|
-
if self.fs.exists(path=str(upload_path)) and not self.upload_config.overwrite:
|
|
291
|
-
logger.debug(f"skipping upload of {path} to {upload_path}, file already exists")
|
|
292
|
-
return
|
|
293
289
|
logger.debug(f"writing local file {path_str} to {upload_path}")
|
|
294
290
|
self.fs.upload(lpath=path_str, rpath=str(upload_path))
|
|
295
291
|
|
|
@@ -297,9 +293,5 @@ class FsspecUploader(Uploader):
|
|
|
297
293
|
upload_path = self.get_upload_path(file_data=file_data)
|
|
298
294
|
path_str = str(path.resolve())
|
|
299
295
|
# Odd that fsspec doesn't run exists() as async even when client support async
|
|
300
|
-
already_exists = self.fs.exists(path=str(upload_path))
|
|
301
|
-
if already_exists and not self.upload_config.overwrite:
|
|
302
|
-
logger.debug(f"skipping upload of {path} to {upload_path}, file already exists")
|
|
303
|
-
return
|
|
304
296
|
logger.debug(f"writing local file {path_str} to {upload_path}")
|
|
305
297
|
self.fs.upload(lpath=path_str, rpath=str(upload_path))
|