33#
44
55import sys
6- from typing import Any , Mapping
76
87from airbyte_cdk .entrypoint import AirbyteEntrypoint , launch
98from airbyte_cdk .sources .declarative .yaml_declarative_source import (
109 YamlDeclarativeSource ,
1110)
1211
13- configuration : Mapping [str , Any ] = {
14- "path_to_yaml" : "resources/manifest.yaml" ,
15- }
16-
1712
1813def debug_manifest (source : YamlDeclarativeSource , args : list [str ]) -> None :
1914 """
@@ -22,15 +17,56 @@ def debug_manifest(source: YamlDeclarativeSource, args: list[str]) -> None:
2217 launch (source , args )
2318
2419
20+ def _register_components_from_file (filepath : str ) -> None :
21+ """
22+ Dynamically load a Python file containing custom component definitions and register it
23+ under specific module names in sys.modules to ensure that these classes can be properly
24+ resolved during hydration of the manifest yaml file.
25+
26+ This is a somewhat hacky replacement for the file structure manipulation we do when building
27+ connector images to ensure the custom components can be imported.
28+ """
29+ import importlib .util
30+ import sys
31+ from pathlib import Path
32+
33+ components_path = Path (filepath )
34+ if not components_path .exists ():
35+ raise FileNotFoundError (f"Components file not found: { components_path } " )
36+
37+ module_name = "components"
38+ sdm_module_name = "source_declarative_manifest.components"
39+
40+ spec = importlib .util .spec_from_file_location (module_name , components_path )
41+ if spec is None or spec .loader is None :
42+ raise ImportError (f"Could not load module from { components_path } " )
43+
44+ # Create module and execute code
45+ module = importlib .util .module_from_spec (spec )
46+
47+ # Register then execute the module
48+ # we dual-register the module to mirror what is done elsewhere in the CDK
49+ sys .modules [module_name ] = module
50+ sys .modules [sdm_module_name ] = module
51+
52+ spec .loader .exec_module (module )
53+
54+
2555if __name__ == "__main__" :
2656 args = sys .argv [1 :]
57+ parsed_args = AirbyteEntrypoint .parse_args (args )
58+
59+ manifest_path = getattr (parsed_args , "manifest_path" , None ) or "resources/manifest.yaml"
60+ components_path = getattr (parsed_args , "components_path" , None )
61+ if components_path :
62+ _register_components_from_file (components_path )
2763 catalog_path = AirbyteEntrypoint .extract_catalog (args )
2864 config_path = AirbyteEntrypoint .extract_config (args )
2965 state_path = AirbyteEntrypoint .extract_state (args )
3066
3167 debug_manifest (
3268 YamlDeclarativeSource (
33- path_to_yaml = "resources/manifest.yaml" ,
69+ path_to_yaml = manifest_path ,
3470 catalog = YamlDeclarativeSource .read_catalog (catalog_path ) if catalog_path else None ,
3571 config = YamlDeclarativeSource .read_config (config_path ) if config_path else None ,
3672 state = YamlDeclarativeSource .read_state (state_path ) if state_path else None ,
0 commit comments