-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
working python source #533
Changes from all commits
5f8f787
5a1c16e
13eea10
92b3bd7
d732de6
f701fa4
7cfb17c
07e7e4d
d394766
cde03b9
f926838
4d5c0c9
84c7db9
09a442c
4ad1db2
dda65c3
bbe34ad
b4a6e92
5fe3a1e
da2d155
8fc3b1a
9a23ff3
f485b29
8bf9d50
76a97e5
9342e08
75ddaa4
4cfaf8e
4aa95e0
0751013
df15670
d380330
bef954b
8f673ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from typing import Generator | ||
import yaml | ||
import json | ||
import pkgutil | ||
import warnings | ||
import python_jsonschema_objects as pjs | ||
from dataclasses import dataclass | ||
|
||
|
||
def _load_classes(yaml_path: str): | ||
|
@@ -36,38 +38,30 @@ def __init__(self, successful, field_to_error): | |
self.field_to_error = field_to_error | ||
|
||
|
||
class AirbyteSchema(object): | ||
def __init__(self, schema): | ||
self.schema = schema | ||
|
||
|
||
class AirbyteConfig(object): | ||
def __init__(self, config_string): | ||
self.config_string = config_string | ||
|
||
|
||
class Integration(object): | ||
def __init__(self): | ||
pass | ||
|
||
def spec(self) -> AirbyteSpec: | ||
raise Exception("Not Implemented") | ||
|
||
# default version reads the config_path to a string | ||
# this will often be overwritten to add fields for easy consumption or to modify the string for delegating to singer | ||
def read_config(self, config_path) -> AirbyteConfig: | ||
def read_config(self, config_path): | ||
with open(config_path, 'r') as file: | ||
contents = file.read() | ||
return AirbyteConfig(contents) | ||
return json.loads(contents) | ||
|
||
def render_config(self, config_object, rendered_config_path): | ||
with open(rendered_config_path, 'w') as fh: | ||
fh.write(config_object.config_string) | ||
# can be overridden to change an input file config | ||
def transform_config(self, raw_config): | ||
return raw_config | ||
|
||
def check(self, config_object, rendered_config_path) -> AirbyteCheckResponse: | ||
def write_config(self, config_object, path): | ||
jrhizor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with open(path, 'w') as fh: | ||
fh.write(json.dumps(config_object)) | ||
|
||
def check(self, logger, config_container) -> AirbyteCheckResponse: | ||
raise Exception("Not Implemented") | ||
|
||
def discover(self, config_object, rendered_config_path) -> AirbyteSchema: | ||
def discover(self, logger, config_container) -> AirbyteCatalog: | ||
raise Exception("Not Implemented") | ||
|
||
|
||
|
@@ -76,10 +70,56 @@ def __init__(self): | |
pass | ||
|
||
# Iterator<AirbyteMessage> | ||
def read(self, config_object, rendered_config_path, state=None) -> Generator[AirbyteMessage, None, None]: | ||
def read(self, logger, config_container, catalog_path, state=None) -> Generator[AirbyteMessage, None, None]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will come in a future PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is expected to be implemented by specific sources. There is no default implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, makes sense |
||
raise Exception("Not Implemented") | ||
|
||
|
||
class Destination(Integration): | ||
def __init__(self): | ||
pass | ||
|
||
|
||
class AirbyteLogger: | ||
def __init__(self): | ||
self.valid_log_types = ["FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"] | ||
|
||
def log_by_prefix(self, message, default_level): | ||
split_line = message.split() | ||
first_word = next(iter(split_line), None) | ||
if first_word in self.valid_log_types: | ||
log_level = first_word | ||
rendered_message = " ".join(split_line[1:]) | ||
else: | ||
log_level = default_level | ||
rendered_message = message | ||
self.log(log_level, rendered_message) | ||
|
||
def log(self, level, message): | ||
log_record = AirbyteLogMessage(level=level, message=message) | ||
log_message = AirbyteMessage(type="LOG", log=log_record) | ||
print(log_message.serialize()) | ||
|
||
def fatal(self, message): | ||
self.log("FATAL", message) | ||
|
||
def error(self, message): | ||
self.log("ERROR", message) | ||
|
||
def warn(self, message): | ||
self.log("WARN", message) | ||
|
||
def info(self, message): | ||
self.log("INFO", message) | ||
|
||
def debug(self, message): | ||
self.log("DEBUG", message) | ||
|
||
def trace(self, message): | ||
self.log("TRACE", message) | ||
|
||
@dataclass | ||
class ConfigContainer: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a comment to explain what these fields are. |
||
raw_config: object | ||
rendered_config: object | ||
raw_config_path: str | ||
rendered_config_path: str |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
import argparse | ||
import logging | ||
import sys | ||
import tempfile | ||
import os.path | ||
import importlib | ||
|
||
from airbyte_protocol import ConfigContainer | ||
from airbyte_protocol import Source | ||
from airbyte_protocol import AirbyteLogger | ||
from airbyte_protocol import AirbyteLogMessage | ||
from airbyte_protocol import AirbyteMessage | ||
|
||
impl_module = os.environ['AIRBYTE_IMPL_MODULE'] | ||
impl_class = os.environ['AIRBYTE_IMPL_PATH'] | ||
|
||
module = importlib.import_module(impl_module) | ||
impl = getattr(module, impl_class) | ||
|
||
logger = AirbyteLogger() | ||
|
||
class AirbyteEntrypoint(object): | ||
def __init__(self, source): | ||
self.source = source | ||
|
@@ -43,8 +48,8 @@ def start(self): | |
# read | ||
read_parser = subparsers.add_parser("read", help="reads the source and outputs messages to STDOUT", | ||
parents=[parent_parser]) | ||
# todo: re-add state handling | ||
# read_parser.add_argument('--state', type=str, required=False, help='path to the json-encoded state file') | ||
|
||
read_parser.add_argument('--state', type=str, required=False, help='path to the json-encoded state file') | ||
required_read_parser = read_parser.add_argument_group('required named arguments') | ||
required_read_parser.add_argument('--config', type=str, required=True, | ||
help='path to the json configuration file') | ||
|
@@ -66,25 +71,30 @@ def start(self): | |
sys.exit(0) | ||
|
||
rendered_config_path = os.path.join(temp_dir, 'config.json') | ||
config_object = source.read_config(parsed_args.config) | ||
source.render_config(config_object, rendered_config_path) | ||
raw_config = source.read_config(parsed_args.config) | ||
rendered_config = source.transform_config(raw_config) | ||
source.write_config(rendered_config, rendered_config_path) | ||
|
||
config_container = ConfigContainer( | ||
raw_config=raw_config, | ||
rendered_config=rendered_config, | ||
raw_config_path=parsed_args.config, | ||
rendered_config_path=rendered_config_path) | ||
|
||
# todo: output message for check | ||
if cmd == "check": | ||
check_result = source.check(logging, rendered_config_path) | ||
check_result = source.check(logger, config_container) | ||
if check_result.successful: | ||
print("Check succeeded") | ||
logger.info("Check succeeded") | ||
sys.exit(0) | ||
else: | ||
print("Check failed") | ||
logger.error("Check failed") | ||
sys.exit(1) | ||
elif cmd == "discover": | ||
schema = source.discover(logging, rendered_config_path) | ||
print(schema.schema) | ||
catalog = source.discover(logger, config_container) | ||
print(catalog.serialize()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we want to dictate the precise format of what we're outputting, we don't want an It's debatable if we should use |
||
sys.exit(0) | ||
elif cmd == "read": | ||
# todo: pass in state | ||
generator = source.read(logging, rendered_config_path) | ||
generator = source.read(logger, config_container, parsed_args.catalog, parsed_args.state) | ||
for message in generator: | ||
print(message.serialize()) | ||
sys.exit(0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are there two
airbyte_protocol
directories? It's not clear to me what the difference between the two is?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like standard structure for python packages:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM, thanks for bearing with my un-pythonic ways :P