Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get module name from sys.modules #17779

Merged
merged 11 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions airbyte-cdk/python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.99

- Low-code: Fix default stream schema loader

## 0.1.98

- Low-code: Expose WaitUntilTimeFromHeader strategy and WaitTimeFromHeader as component type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@

import json
import pkgutil
import sys
from dataclasses import InitVar, dataclass, field
from typing import Any, Mapping, Union

import __main__
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString
from airbyte_cdk.sources.declarative.schema.schema_loader import SchemaLoader
from airbyte_cdk.sources.declarative.types import Config
from dataclasses_jsonschema import JsonSchemaMixin


def _default_file_path() -> str:
main_file = __main__.__file__
module = main_file.split("/")[-2].replace("-", "_")
# schema files are always in "source_<connector_name>/schemas/<stream_name>.json
# the connector's module name can be inferred by looking at the modules loaded and look for the one starting with source_
source_modules = [
k for k, v in sys.modules.items() if "source_" in k # example: ['source_exchange_rates', 'source_exchange_rates.source']
]
if not source_modules:
raise RuntimeError("Expected at least one module starting with 'source_'")
module = source_modules[0].split(".")[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a developer went out of their way to name their modules incorrectly w/o source_, this would crash. More of a user error to deviate from the template, but we should check here and throw an error instead of crashing. I'm sure other things would fail if they did that as well, but just in case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return f"./{module}/schemas/{{{{options['name']}}}}.json"


Expand All @@ -34,9 +40,11 @@ class JsonSchema(SchemaLoader, JsonSchemaMixin):

config: Config
options: InitVar[Mapping[str, Any]]
file_path: Union[InterpolatedString, str] = field(default=_default_file_path())
file_path: Union[InterpolatedString, str] = field(default=None)

def __post_init__(self, options: Mapping[str, Any]):
if not self.file_path:
self.file_path = _default_file_path()
self.file_path = InterpolatedString.create(self.file_path, options=options)

def get_json_schema(self) -> Mapping[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion airbyte-cdk/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name="airbyte-cdk",
version="0.1.98",
version="0.1.99",
description="A framework for writing Airbyte Connectors.",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#
from .source_test.SourceTest import SourceTest

__all__ = ["SourceTest"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#


class SourceTest:
def __init__(self):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from airbyte_cdk.sources.declarative.declarative_stream import DeclarativeStream
from airbyte_cdk.sources.declarative.transformations import RecordTransformation

from .schema.source_test import SourceTest # noqa #pylint: disable=unused-import


def test_declarative_stream():
name = "stream"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import json

from airbyte_cdk.sources.declarative.yaml_declarative_source import YamlDeclarativeSource

# import pytest
# from airbyte_cdk.sources.declarative.exceptions import InvalidConnectorDefinitionException
from airbyte_cdk.sources.declarative.yaml_declarative_source import YamlDeclarativeSource

# import os
# import tempfile
Expand Down