1010from types import ModuleType
1111from typing import Any , Dict , Iterator , List , Mapping , MutableMapping , Optional , Set
1212
13+ import orjson
1314import yaml
1415from jsonschema .exceptions import ValidationError
1516from jsonschema .validators import validate
1617from packaging .version import InvalidVersion , Version
1718
19+ from airbyte_cdk .config_observation import create_connector_config_control_message
1820from airbyte_cdk .connector_builder .models import (
1921 LogMessage as ConnectorBuilderLogMessage ,
2022)
2931 ConnectorSpecification ,
3032 FailureType ,
3133)
34+ from airbyte_cdk .models .airbyte_protocol_serializers import AirbyteMessageSerializer
3235from airbyte_cdk .sources .declarative .checks import COMPONENTS_CHECKER_TYPE_MAPPING
3336from airbyte_cdk .sources .declarative .checks .connection_checker import ConnectionChecker
3437from airbyte_cdk .sources .declarative .declarative_source import DeclarativeSource
@@ -100,6 +103,7 @@ def __init__(
100103 component_factory : Optional [ModelToComponentFactory ] = None ,
101104 migrate_manifest : Optional [bool ] = False ,
102105 normalize_manifest : Optional [bool ] = False ,
106+ config_path : Optional [str ] = None ,
103107 ) -> None :
104108 """
105109 Args:
@@ -109,6 +113,7 @@ def __init__(
109113 emit_connector_builder_messages: True if messages should be emitted to the connector builder.
110114 component_factory: optional factory if ModelToComponentFactory's default behavior needs to be tweaked.
111115 normalize_manifest: Optional flag to indicate if the manifest should be normalized.
116+ config_path: Optional path to the config file.
112117 """
113118 self .logger = logging .getLogger (f"airbyte.{ self .name } " )
114119 self ._should_normalize = normalize_manifest
@@ -131,7 +136,6 @@ def __init__(
131136 self ._slice_logger : SliceLogger = (
132137 AlwaysLogSliceLogger () if emit_connector_builder_messages else DebugSliceLogger ()
133138 )
134- self ._config = config or {}
135139
136140 # resolve all components in the manifest
137141 self ._source_config = self ._pre_process_manifest (dict (source_config ))
@@ -140,11 +144,33 @@ def __init__(
140144 # apply additional post-processing to the manifest
141145 self ._post_process_manifest ()
142146
147+ self ._config : Mapping [str , Any ]
143148 self ._spec_component : Optional [Spec ] = None
144149 if spec := self ._source_config .get ("spec" ):
145150 if "type" not in spec :
146151 spec ["type" ] = "Spec"
147152 self ._spec_component = self ._constructor .create_component (SpecModel , spec , dict ())
153+ mutable_config = dict (config ) if config else {}
154+
155+ if config_path :
156+ self ._spec_component .migrate_config (mutable_config )
157+ try :
158+ if mutable_config != config :
159+ with open (config_path , "w" ) as f :
160+ json .dump (mutable_config , f )
161+ self .message_repository .emit_message (
162+ create_connector_config_control_message (mutable_config )
163+ )
164+ # We have no mechanism for consuming the queue, so we print the messages to stdout
165+ for message in self .message_repository .consume_queue ():
166+ print (orjson .dumps (AirbyteMessageSerializer .dump (message )).decode ())
167+ except Exception as e :
168+ self .logger .error (f"Error migrating config: { str (e )} " )
169+ mutable_config = dict (config ) if config else {}
170+ self ._spec_component .transform_config (mutable_config )
171+ self ._config = mutable_config
172+ else :
173+ self ._config = config or {}
148174
149175 @property
150176 def resolved_manifest (self ) -> Mapping [str , Any ]:
@@ -296,12 +322,6 @@ def streams(self, config: Mapping[str, Any]) -> List[Stream]:
296322
297323 return source_streams
298324
299- def migrate_config (self , config_path : Optional [str ], config : MutableMapping [str , Any ]) -> None :
300- self ._spec_component .migrate_config (config_path , config ) if self ._spec_component else None
301-
302- def transform_config (self , config : MutableMapping [str , Any ]) -> None :
303- self ._spec_component .transform_config (config ) if self ._spec_component else None
304-
305325 @staticmethod
306326 def _initialize_cache_for_parent_streams (
307327 stream_configs : List [Dict [str , Any ]],
0 commit comments