-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy path_run.py
More file actions
290 lines (243 loc) · 10.6 KB
/
_run.py
File metadata and controls
290 lines (243 loc) · 10.6 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
"""Defines the `source-declarative-manifest` connector, which installs alongside CDK.
This file was originally imported from the dedicated connector directory, under the
`airbyte` monorepo.
Usage:
```
pipx install airbyte-cdk
source-declarative-manifest --help
source-declarative-manifest spec
...
```
"""
from __future__ import annotations
import argparse
import json
import pkgutil
import sys
import traceback
from collections.abc import MutableMapping
from pathlib import Path
from typing import Any, cast
import orjson
import yaml
from airbyte_cdk.entrypoint import AirbyteEntrypoint, launch
from airbyte_cdk.models import (
AirbyteErrorTraceMessage,
AirbyteMessage,
AirbyteMessageSerializer,
AirbyteStateMessage,
AirbyteTraceMessage,
ConfiguredAirbyteCatalog,
ConnectorSpecificationSerializer,
TraceType,
Type,
)
from airbyte_cdk.sources.declarative.concurrent_declarative_source import (
ConcurrentDeclarativeSource,
)
from airbyte_cdk.sources.declarative.yaml_declarative_source import YamlDeclarativeSource
from airbyte_cdk.sources.source import TState
from airbyte_cdk.utils.cli_arg_parse import parse_cli_args
from airbyte_cdk.utils.datetime_helpers import ab_datetime_now
class SourceLocalYaml(YamlDeclarativeSource):
"""
Declarative source defined by a yaml file in the local filesystem
"""
def __init__(
self,
catalog: ConfiguredAirbyteCatalog | None,
config: MutableMapping[str, Any] | None,
state: TState,
**kwargs: Any,
) -> None:
"""
HACK!
Problem: YamlDeclarativeSource relies on the calling module name/path to find the yaml file.
Implication: If you call YamlDeclarativeSource directly it will look for the yaml file in the wrong place. (e.g. the airbyte-cdk package)
Solution: Subclass YamlDeclarativeSource from the same location as the manifest to load.
When can we remove this?
When the airbyte-cdk is updated to not rely on the calling module name/path to find the yaml file.
When all manifest connectors are updated to use the new airbyte-cdk.
When all manifest connectors are updated to use the source-declarative-manifest as the base image.
"""
super().__init__(
catalog=catalog,
config=config,
state=state, # type: ignore [arg-type]
path_to_yaml="manifest.yaml",
)
def _is_local_manifest_command(args: list[str]) -> bool:
# Check for a local manifest.yaml file
return Path("/airbyte/integration_code/source_declarative_manifest/manifest.yaml").exists()
def handle_command(args: list[str]) -> None:
if _is_local_manifest_command(args):
handle_local_manifest_command(args)
else:
handle_remote_manifest_command(args)
def _get_local_yaml_source(args: list[str]) -> SourceLocalYaml:
try:
parsed_args = parse_cli_args(args)
config, catalog, state = _parse_inputs_into_config_catalog_state(parsed_args)
return SourceLocalYaml(config=config, catalog=catalog, state=state)
except Exception as error:
print(
orjson.dumps(
AirbyteMessageSerializer.dump(
AirbyteMessage(
type=Type.TRACE,
trace=AirbyteTraceMessage(
type=TraceType.ERROR,
emitted_at=ab_datetime_now().to_epoch_millis(),
error=AirbyteErrorTraceMessage(
message=f"Error starting the sync. This could be due to an invalid configuration or catalog. Please contact Support for assistance. Error: {error}",
stack_trace=traceback.format_exc(),
),
),
)
)
).decode()
)
raise error
def handle_local_manifest_command(args: list[str]) -> None:
source = _get_local_yaml_source(args)
source.launch_with_cli_args(args)
def handle_remote_manifest_command(args: list[str]) -> None:
"""Overrides the spec command to return the generalized spec for the declarative manifest source.
This is different from a typical low-code, but built and published separately source built as a ManifestDeclarativeSource,
because that will have a spec method that returns the spec for that specific source. Other than spec,
the generalized connector behaves the same as any other, since the manifest is provided in the config.
"""
if args[0] == "spec":
json_spec = pkgutil.get_data(
"airbyte_cdk.cli.source_declarative_manifest",
"spec.json",
)
if json_spec is None:
raise FileNotFoundError(
"Could not find `spec.json` file for source-declarative-manifest"
)
spec_obj = json.loads(json_spec)
spec = ConnectorSpecificationSerializer.load(spec_obj)
message = AirbyteMessage(type=Type.SPEC, spec=spec)
print(AirbyteEntrypoint.airbyte_message_to_string(message))
else:
source = create_declarative_source(args)
source.launch_with_cli_args(args=args)
def create_declarative_source(
args: list[str],
) -> ConcurrentDeclarativeSource: # type: ignore [type-arg]
"""Creates the source with the injected config.
This essentially does what other low-code sources do at build time, but at runtime,
with a user-provided manifest in the config. This better reflects what happens in the
connector builder.
"""
try:
config: MutableMapping[str, Any] | None
catalog: ConfiguredAirbyteCatalog | None
state: list[AirbyteStateMessage]
parsed_args = parse_cli_args(args)
config, catalog, state = _parse_inputs_into_config_catalog_state(parsed_args)
if config is None:
raise ValueError(
"Invalid config: `__injected_declarative_manifest` should be provided at the root "
"of the config or using the --manifest-path argument."
)
# If a manifest_path is provided in the args, inject it into the config
if hasattr(parsed_args, "manifest_path") and parsed_args.manifest_path:
injected_manifest = _parse_manifest_from_file(parsed_args.manifest_path)
if injected_manifest:
config["__injected_declarative_manifest"] = injected_manifest
if "__injected_declarative_manifest" not in config:
raise ValueError(
"Invalid config: `__injected_declarative_manifest` should be provided at the root "
"of the config or using the --manifest-path argument. "
f"Config only has keys: {list(config.keys() if config else [])}"
)
if not isinstance(config["__injected_declarative_manifest"], dict):
raise ValueError(
"Invalid config: `__injected_declarative_manifest` should be a dictionary, "
f"but got type: {type(config['__injected_declarative_manifest'])}"
)
if hasattr(parsed_args, "components_path") and parsed_args.components_path:
_register_components_from_file(parsed_args.components_path)
return ConcurrentDeclarativeSource(
config=config,
catalog=catalog,
state=state,
source_config=cast(dict[str, Any], config["__injected_declarative_manifest"]),
)
except Exception as error:
print(
orjson.dumps(
AirbyteMessageSerializer.dump(
AirbyteMessage(
type=Type.TRACE,
trace=AirbyteTraceMessage(
type=TraceType.ERROR,
emitted_at=ab_datetime_now().to_epoch_millis(),
error=AirbyteErrorTraceMessage(
message=f"Error starting the sync. This could be due to an invalid configuration or catalog. Please contact Support for assistance. Error: {error}",
stack_trace=traceback.format_exc(),
),
),
)
)
).decode()
)
raise error
def _parse_inputs_into_config_catalog_state(
parsed_args: argparse.Namespace,
) -> tuple[
MutableMapping[str, Any] | None,
ConfiguredAirbyteCatalog | None,
list[AirbyteStateMessage],
]:
config = (
ConcurrentDeclarativeSource.read_config(parsed_args.config)
if hasattr(parsed_args, "config")
else None
)
catalog = (
ConcurrentDeclarativeSource.read_catalog(parsed_args.catalog)
if hasattr(parsed_args, "catalog")
else None
)
state = (
ConcurrentDeclarativeSource.read_state(parsed_args.state)
if hasattr(parsed_args, "state")
else []
)
return config, catalog, state
def _parse_manifest_from_file(filepath: str) -> dict[str, Any] | None:
"""Extract and parse a manifest file specified in the args."""
try:
with open(filepath, "r", encoding="utf-8") as manifest_file:
manifest_content = yaml.safe_load(manifest_file)
if manifest_content is None:
raise ValueError(f"Manifest file at {filepath} is empty")
if not isinstance(manifest_content, dict):
raise ValueError(f"Manifest must be a dictionary, got {type(manifest_content)}")
return manifest_content
except Exception as error:
raise ValueError(f"Failed to load manifest file from {filepath}: {error}")
def _register_components_from_file(filepath: str) -> None:
"""Load and register components from a Python file specified in the args."""
import importlib.util
import sys
components_path = Path(filepath)
module_name = "components"
sdm_module_name = "source_declarative_manifest.components"
# Create module spec
spec = importlib.util.spec_from_file_location(module_name, components_path)
if spec is None or spec.loader is None:
raise ImportError(f"Could not load module from {components_path}")
# Create module and execute code, registering the module before executing its code
# To avoid issues with dataclasses that look up the module
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
sys.modules[sdm_module_name] = module
spec.loader.exec_module(module)
def run() -> None:
args: list[str] = sys.argv[1:]
handle_command(args)