|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import json |
| 6 | + |
| 7 | +import singer |
| 8 | +from singer import utils |
| 9 | +from singer.catalog import Catalog, CatalogEntry, Schema |
| 10 | +from . import streams |
| 11 | +from .context import Context |
| 12 | +from . import schemas |
| 13 | + |
| 14 | +REQUIRED_CONFIG_KEYS = ["token", "metric"] |
| 15 | + |
| 16 | +LOGGER = singer.get_logger() |
| 17 | + |
| 18 | +#def check_authorization(atx): |
| 19 | +# atx.client.get('/settings') |
| 20 | + |
| 21 | + |
| 22 | +# with tap-emarsys, they do it this way where the catalog is read in from a call to the api |
| 23 | +# but with the odd frontapp structure, we won't do that here |
| 24 | +# we never use atx in here since the schema is from file |
| 25 | +# but we would use it if we pulled schema from the API |
| 26 | +# def discover(atx): |
| 27 | +def discover(): |
| 28 | + catalog = Catalog([]) |
| 29 | + for tap_stream_id in schemas.STATIC_SCHEMA_STREAM_IDS: |
| 30 | + #print("tap stream id=",tap_stream_id) |
| 31 | + schema = Schema.from_dict(schemas.load_schema(tap_stream_id)) |
| 32 | + metadata = [] |
| 33 | + if schema.selected is True: |
| 34 | + metadata.append({ |
| 35 | + 'metadata': { |
| 36 | + 'selected': True |
| 37 | + }, |
| 38 | + 'breadcrumb': [] |
| 39 | + }) |
| 40 | + for field_name in schema.properties.keys(): |
| 41 | + #print("field name=",field_name) |
| 42 | + if field_name in schemas.PK_FIELDS[tap_stream_id]: |
| 43 | + inclusion = 'automatic' |
| 44 | + else: |
| 45 | + inclusion = 'available' |
| 46 | + metadata.append({ |
| 47 | + 'metadata': { |
| 48 | + 'inclusion': inclusion |
| 49 | + }, |
| 50 | + 'breadcrumb': ['properties', field_name] |
| 51 | + }) |
| 52 | + catalog.streams.append(CatalogEntry( |
| 53 | + stream=tap_stream_id, |
| 54 | + tap_stream_id=tap_stream_id, |
| 55 | + key_properties=schemas.PK_FIELDS[tap_stream_id], |
| 56 | + schema=schema, |
| 57 | + metadata=metadata |
| 58 | + )) |
| 59 | + return catalog |
| 60 | + |
| 61 | + |
| 62 | +# this is already defined in schemas.py though w/o dependencies. do we keep this for the sync? |
| 63 | +def load_schema(tap_stream_id): |
| 64 | + path = "schemas/{}.json".format(tap_stream_id) |
| 65 | + schema = utils.load_json(get_abs_path(path)) |
| 66 | + dependencies = schema.pop("tap_schema_dependencies", []) |
| 67 | + refs = {} |
| 68 | + for sub_stream_id in dependencies: |
| 69 | + refs[sub_stream_id] = load_schema(sub_stream_id) |
| 70 | + if refs: |
| 71 | + singer.resolve_schema_references(schema, refs) |
| 72 | + return schema |
| 73 | + |
| 74 | + |
| 75 | +def sync(atx): |
| 76 | + for tap_stream_id in schemas.STATIC_SCHEMA_STREAM_IDS: |
| 77 | + schemas.load_and_write_schema(tap_stream_id) |
| 78 | + |
| 79 | + streams.sync_selected_streams(atx) |
| 80 | + |
| 81 | + |
| 82 | +@utils.handle_top_exception(LOGGER) |
| 83 | +def main(): |
| 84 | + args = utils.parse_args(REQUIRED_CONFIG_KEYS) |
| 85 | + atx = Context(args.config, args.state) |
| 86 | + if args.discover: |
| 87 | + # the schema is static from file so we don't need to pass in atx for connection info. |
| 88 | + catalog = discover() |
| 89 | + json.dump(catalog.to_dict(), sys.stdout) |
| 90 | + else: |
| 91 | + atx.catalog = Catalog.from_dict(args.properties) \ |
| 92 | + if args.properties else discover() |
| 93 | + sync(atx) |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments