-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathyaml_declarative_source.py
More file actions
68 lines (57 loc) · 2.75 KB
/
yaml_declarative_source.py
File metadata and controls
68 lines (57 loc) · 2.75 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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import pkgutil
from typing import Any, List, Mapping, Optional
import yaml
from airbyte_cdk.models import AirbyteStateMessage, ConfiguredAirbyteCatalog
from airbyte_cdk.sources.declarative.concurrent_declarative_source import (
ConcurrentDeclarativeSource,
)
from airbyte_cdk.sources.types import ConnectionDefinition
class YamlDeclarativeSource(ConcurrentDeclarativeSource[List[AirbyteStateMessage]]):
"""Declarative source defined by a yaml file"""
def __init__(
self,
path_to_yaml: str,
debug: bool = False,
catalog: Optional[ConfiguredAirbyteCatalog] = None,
config: Optional[Mapping[str, Any]] = None,
state: Optional[List[AirbyteStateMessage]] = None,
) -> None:
"""
:param path_to_yaml: Path to the yaml file describing the source
"""
self._path_to_yaml = path_to_yaml
source_config = self._read_and_parse_yaml_file(path_to_yaml)
super().__init__(
catalog=catalog or ConfiguredAirbyteCatalog(streams=[]),
config=config or {},
state=state or [],
source_config=source_config,
)
def _read_and_parse_yaml_file(self, path_to_yaml_file: str) -> ConnectionDefinition:
try:
# For testing purposes, we want to allow to just pass a file
with open(path_to_yaml_file, "r") as f:
return yaml.safe_load(f) # type: ignore # we assume the yaml represents a ConnectionDefinition
except FileNotFoundError:
# Running inside the container, the working directory during an operation is not structured the same as the static files
package = self.__class__.__module__.split(".")[0]
yaml_config = pkgutil.get_data(package, path_to_yaml_file)
if yaml_config:
decoded_yaml = yaml_config.decode()
return self._parse(decoded_yaml)
return {}
def _emit_manifest_debug_message(self, extra_args: dict[str, Any]) -> None:
extra_args["path_to_yaml"] = self._path_to_yaml
self.logger.debug("declarative source created from parsed YAML manifest", extra=extra_args)
@staticmethod
def _parse(connection_definition_str: str) -> ConnectionDefinition:
"""
Parses a yaml file into a manifest. Component references still exist in the manifest which will be
resolved during the creating of the DeclarativeSource.
:param connection_definition_str: yaml string to parse
:return: The ConnectionDefinition parsed from connection_definition_str
"""
return yaml.safe_load(connection_definition_str) # type: ignore # yaml.safe_load doesn't return a type but know it is a Mapping