Skip to content

graflo.hq.registry_builder

Build a :class:DataSourceRegistry from :class:Bindings and schema models.

Handles file discovery, SQL table source creation (with auto-JOIN enrichment and datetime filtering), and connector dispatch by bound source kind.

Attributes

logger = logging.getLogger(__name__) module-attribute

Classes

RegistryBuilder

Create a :class:DataSourceRegistry from :class:Bindings.

Attributes:

Name Type Description
schema

Schema providing the resource definitions and vertex/edge config.

Source code in graflo/hq/registry_builder.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
class RegistryBuilder:
    """Create a :class:`DataSourceRegistry` from :class:`Bindings`.

    Attributes:
        schema: Schema providing the resource definitions and vertex/edge config.
    """

    def __init__(self, schema: Schema, ingestion_model: IngestionModel):
        self.schema = schema
        self.ingestion_model = ingestion_model

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def build(
        self,
        bindings: Bindings,
        ingestion_params: IngestionParams,
        connection_provider: ConnectionProvider | None = None,
        *,
        strict: bool = False,
        pushed: Mapping[str, Sequence[Mapping[str, Any]]] | None = None,
    ) -> DataSourceRegistry:
        """Return a populated :class:`DataSourceRegistry`.

        For each ingestion resource, registers every bound connector (same
        resource may have multiple physical sources).

        A source that cannot be built -- no connector bound, no connection
        configuration registered, a registration error -- is logged and
        skipped; with *strict* the build raises once every resource has been
        tried, listing them all.

        *pushed* holds the records for push connectors, keyed by connector
        name. A push connector with nothing pushed is a source that cannot be
        built.
        """
        registry = DataSourceRegistry()
        provider = connection_provider or EmptyConnectionProvider()
        failures: list[str] = []

        resources_filter: set[str] | None = None
        if ingestion_params.resources is not None:
            resources_filter = set(ingestion_params.resources)

        connectors_filter: set[str] | None = None
        if ingestion_params.connectors is not None:
            connectors_filter = bindings.resolve_connector_refs_to_hashes(
                ingestion_params.connectors
            )

        for resource in self.ingestion_model.resources:
            resource_name = resource.name
            if resources_filter is not None and resource_name not in resources_filter:
                continue
            connectors = bindings.get_connectors_for_resource(resource_name)
            if not connectors:
                msg = f"No connectors bound for resource '{resource_name}'"
                logger.warning("%s, skipping", msg)
                failures.append(msg)
                continue

            for connector in connectors:
                if (
                    connectors_filter is not None
                    and connector.hash not in connectors_filter
                ):
                    continue
                cref = connector.name or connector.hash
                kind = connector.bound_source_kind()
                if not isinstance(connector, TableConnector):
                    self._report_unnarrowed(resource_name, connector)

                if kind == BoundSourceKind.FILE:
                    if not isinstance(connector, FileConnector):
                        msg = (
                            f"Connector '{cref}' for resource '{resource_name}' "
                            f"is not a FileConnector"
                        )
                        logger.warning("%s, skipping", msg)
                        failures.append(msg)
                        continue
                    try:
                        self._register_file_sources(
                            registry,
                            resource_name,
                            connector,
                            bindings,
                            ingestion_params,
                            provider,
                        )
                    except Exception as e:
                        msg = (
                            f"Failed to register FILE source for resource "
                            f"'{resource_name}' (connector '{cref}'): {e}"
                        )
                        if not strict:
                            logger.warning("%s, skipping", msg)
                        failures.append(msg)

                elif kind == BoundSourceKind.SQL_TABLE:
                    if not isinstance(connector, TableConnector):
                        msg = (
                            f"Connector '{cref}' for resource '{resource_name}' "
                            f"is not a TableConnector"
                        )
                        logger.warning("%s, skipping", msg)
                        failures.append(msg)
                        continue
                    try:
                        self._register_sql_table_sources(
                            registry,
                            resource_name,
                            connector,
                            bindings,
                            ingestion_params,
                            provider,
                        )
                    except Exception as e:
                        msg = (
                            f"Failed to register SQL source for resource "
                            f"'{resource_name}' (connector '{cref}'): {e}"
                        )
                        if not strict:
                            logger.warning("%s, skipping", msg)
                        failures.append(msg)

                elif kind == BoundSourceKind.SPARQL:
                    if not isinstance(connector, SparqlConnector):
                        msg = (
                            f"Connector '{cref}' for resource '{resource_name}' "
                            f"is not a SparqlConnector"
                        )
                        logger.warning("%s, skipping", msg)
                        failures.append(msg)
                        continue
                    try:
                        self._register_sparql_sources(
                            registry,
                            resource_name,
                            connector,
                            bindings,
                            ingestion_params,
                            provider,
                        )
                    except Exception as e:
                        msg = (
                            f"Failed to register SPARQL source for resource "
                            f"'{resource_name}' (connector '{cref}'): {e}"
                        )
                        if not strict:
                            logger.warning("%s, skipping", msg)
                        failures.append(msg)

                elif kind == BoundSourceKind.API:
                    if not isinstance(connector, APIConnector):
                        msg = (
                            f"Connector '{cref}' for resource '{resource_name}' "
                            f"is not an APIConnector"
                        )
                        logger.warning("%s, skipping", msg)
                        failures.append(msg)
                        continue
                    try:
                        self._register_api_sources(
                            registry,
                            resource_name,
                            connector,
                            bindings,
                            ingestion_params,
                            provider,
                        )
                    except Exception as e:
                        msg = (
                            f"Failed to register API source for resource "
                            f"'{resource_name}' (connector '{cref}'): {e}"
                        )
                        if not strict:
                            logger.warning("%s, skipping", msg)
                        failures.append(msg)

                elif kind == BoundSourceKind.KAFKA:
                    if not isinstance(connector, KafkaConnector):
                        msg = (
                            f"Connector '{cref}' for resource '{resource_name}' "
                            f"is not a KafkaConnector"
                        )
                        logger.warning("%s, skipping", msg)
                        failures.append(msg)
                        continue
                    try:
                        self._register_kafka_sources(
                            registry,
                            resource_name,
                            connector,
                            bindings,
                            provider,
                        )
                    except Exception as e:
                        msg = (
                            f"Failed to register Kafka source for resource "
                            f"'{resource_name}' (connector '{cref}'): {e}"
                        )
                        if not strict:
                            logger.warning("%s, skipping", msg)
                        failures.append(msg)

                elif kind == BoundSourceKind.PUSH:
                    if not isinstance(connector, PushConnector):
                        msg = (
                            f"Connector '{cref}' for resource '{resource_name}' "
                            f"is not a PushConnector"
                        )
                        logger.warning("%s, skipping", msg)
                        failures.append(msg)
                        continue
                    records = (pushed or {}).get(connector.name)
                    if not records:
                        msg = (
                            f"Push connector '{cref}' for resource "
                            f"'{resource_name}': nothing was pushed"
                        )
                        if not strict:
                            logger.warning("%s, skipping", msg)
                        failures.append(msg)
                        continue
                    self._register_push_source(
                        registry, resource_name, connector, records
                    )

                else:
                    msg = (
                        f"Unsupported bound source kind '{kind}' "
                        f"for resource '{resource_name}' (connector '{cref}')"
                    )
                    logger.warning("%s, skipping", msg)
                    failures.append(msg)

        if strict and failures:
            details = "\n".join(f"- {item}" for item in failures)
            raise ValueError(f"Registry build failed in strict mode:\n{details}")

        return registry

    # ------------------------------------------------------------------
    # File sources
    # ------------------------------------------------------------------

    @staticmethod
    def discover_files(
        fpath: Path | str, connector: FileConnector, limit_files: int | None = None
    ) -> list[Path]:
        """Discover files matching *connector* in a directory.

        Args:
            fpath: Directory to search in.
            connector: Connector used to match files.
            limit_files: Optional cap on the number of files returned.

        Returns:
            Matching file paths.
        """
        if connector.sub_path is None:
            raise ValueError("connector.sub_path is required")
        path = Path(fpath) if isinstance(fpath, str) else fpath

        candidates = path.rglob("*") if connector.recursive else path.iterdir()
        files = sorted(
            f
            for f in candidates
            if f.is_file()
            and (
                True
                if connector.regex is None
                else re.search(connector.regex, f.name) is not None
            )
        )

        if limit_files is not None:
            files = files[:limit_files]

        return files

    def _register_file_sources(
        self,
        registry: DataSourceRegistry,
        resource_name: str,
        connector: FileConnector,
        bindings: Bindings,
        ingestion_params: IngestionParams,
        connection_provider: ConnectionProvider,
    ) -> None:
        encoding = self.ingestion_model.fetch_resource_config(resource_name).encoding
        sources = self.file_sources(
            connector,
            bindings,
            connection_provider,
            limit_files=ingestion_params.limit_files,
            encoding=encoding,
        )
        logger.info(
            "For resource name %s %d files were found", resource_name, len(sources)
        )
        for file_source in sources:
            registry.register(file_source, resource_name=resource_name)

    @classmethod
    def file_sources(
        cls,
        connector: FileConnector,
        bindings: Bindings,
        connection_provider: ConnectionProvider,
        *,
        limit_files: int | None = None,
        encoding: EncodingType = EncodingType.UTF_8,
    ) -> list[AbstractDataSource]:
        """One data source per file *connector* matches.

        With a ``conn_proxy`` the files are found under that files connection's
        root, a local directory or an S3 prefix; without one, under the local
        ``sub_path``.
        """
        if bindings.get_conn_proxy_for_connector(connector) is None:
            return [
                DataSourceFactory.create_file_data_source(
                    path=path, file_type=connector.file_type, encoding=encoding
                )
                for path in cls.discover_files(
                    connector.sub_path.expanduser(),
                    connector=connector,
                    limit_files=limit_files,
                )
            ]
        from graflo.data_source.files import file_data_source, list_files

        files = connector_config(
            connector, bindings, connection_provider, FilesConnConfig
        )
        return [
            file_data_source(
                files, relative, file_type=connector.file_type, encoding=encoding
            )
            for relative in list_files(files, connector, limit=limit_files)
        ]

    # ------------------------------------------------------------------
    # SQL / table sources
    # ------------------------------------------------------------------

    def _register_sql_table_sources(
        self,
        registry: DataSourceRegistry,
        resource_name: str,
        connector: TableConnector,
        bindings: Bindings,
        ingestion_params: IngestionParams,
        connection_provider: ConnectionProvider,
    ) -> None:
        """Register SQL table data sources for a resource.

        Uses SQLDataSource with batch processing (cursors) instead of loading
        all data into memory.

        When the matching Resource has edge actors with ``match_source`` /
        ``match_target`` and the source/target vertex types have known
        table connectors, JoinClauses and IS_NOT_NULL filters are auto-generated
        on the connector before building the SQL query.
        """
        from graflo.hq.auto_join import enrich_edge_connector_with_joins

        postgres_config = connector_config(
            connector, bindings, connection_provider, PostgresConfig
        )

        table_name = connector.table_name
        schema_name = connector.schema_name
        effective_schema = schema_name or postgres_config.schema_name or "public"

        try:
            resource = self.ingestion_model.fetch_resource(resource_name)
            if connector.view is None and not connector.joins:
                enrich_edge_connector_with_joins(
                    resource=resource,
                    connector=connector,
                    bindings=bindings,
                    vertex_config=self.schema.core_schema.vertex_config,
                )

            routed = self._routed_rows(resource_name)
            if routed == {}:
                logger.warning(
                    "Connector '%s' is not read: resource '%s' routes no value "
                    "of its rows to a class",
                    connector.name or connector.hash,
                    resource_name,
                )
                return

            # The run's date range is one more condition on the connector's
            # query: it applies whether the column comes from the connector or
            # from the run, beside any window the connector declares itself.
            date_column = connector.date_field or ingestion_params.datetime_column
            run_filters = []
            if ingestion_params.datetime_after or ingestion_params.datetime_before:
                if date_column:
                    date_range = datetime_range_filter(
                        ingestion_params.datetime_after,
                        ingestion_params.datetime_before,
                        date_column,
                    )
                    if date_range is not None:
                        run_filters.append(date_range)
                else:
                    logger.warning(
                        "datetime_after/datetime_before set but no date column: "
                        "set TableConnector.date_field or IngestionParams.datetime_column for resource %s",
                        resource_name,
                    )

            if routed is not None:
                pushed = self._routed_filter(connector, routed)
                if pushed is not None:
                    run_filters.append(pushed)
                    logger.info(
                        "Connector '%s' reads only the rows resource '%s' routes: %s",
                        connector.name or connector.hash,
                        resource_name,
                        _describe(routed),
                    )
                else:
                    self._report_unnarrowed(resource_name, connector)

            query = connector.build_query(effective_schema, extra_filters=run_filters)

            connection_string = postgres_config.to_sqlalchemy_connection_string()

            sql_config = SQLConfig(
                connection_string=connection_string,
                query=query,
            )
            sql_source = SQLDataSource(config=sql_config)

            registry.register(sql_source, resource_name=resource_name)

            logger.info(
                f"Created SQL data source for table '{effective_schema}.{table_name}' "
                f"mapped to resource '{resource_name}' "
                f"(will process in batches of {ingestion_params.batch_size})"
            )
        except Exception as e:
            logger.error(
                f"Failed to create data source for PostgreSQL table '{resource_name}': {e}",
                exc_info=True,
            )
            raise

    def _routed_rows(self, resource_name: str) -> dict[str, frozenset[Any]] | None:
        """The routed values per field a row of *resource_name* must hold to count.

        See :func:`routed_row_condition`; ``None`` when a row may count anyway.
        """
        pipeline = self.ingestion_model.fetch_resource_config(resource_name).pipeline
        return routed_row_condition(
            pipeline, self.schema.core_schema.vertex_config.vertex_set
        )

    @staticmethod
    def _routed_filter(
        connector: TableConnector, routed: Mapping[str, frozenset[Any]]
    ) -> FilterExpression | None:
        """*routed* as a condition on *connector*'s query; ``None`` if it cannot be one."""
        columns: dict[str, set[Any]] = {}
        for field, values in routed.items():
            column = connector.source_column(field)
            if column is None:
                return None
            columns.setdefault(column, set()).update(values)
        return values_in_filter(columns)

    def _report_unnarrowed(self, resource_name: str, connector: Any) -> None:
        """Warn when *connector* reads rows its resource's routers drop.

        Said when a router keeps a chosen subset (closed, or bounded by
        ``vertex_types``) and the connector fixes the routed fields on none of
        its requests. A table connector reaches here only when its query
        cannot carry the condition.
        """
        routed = self._routed_rows(resource_name)
        if routed is None:
            return
        pipeline = self.ingestion_model.fetch_resource_config(resource_name).pipeline
        if routed and not router_keeps_subset(pipeline):
            return
        if routed and not isinstance(connector, TableConnector):
            requests = connector.row_constants()
            if requests and all(set(routed) <= set(c) for c in requests):
                return
        logger.warning(
            "Connector '%s' reads every row, but resource '%s' keeps only rows "
            "with %s; the others are read and dropped",
            connector.name or connector.hash,
            resource_name,
            _describe(routed) if routed else "no routed value",
        )

    # ------------------------------------------------------------------
    # SPARQL / RDF sources
    # ------------------------------------------------------------------

    def _register_sparql_sources(
        self,
        registry: DataSourceRegistry,
        resource_name: str,
        connector: SparqlConnector,
        bindings: Bindings,
        ingestion_params: IngestionParams,
        connection_provider: ConnectionProvider,
    ) -> None:
        """Register the SPARQL data source of *connector* for a resource."""
        try:
            source = self.sparql_source(
                connector,
                bindings,
                resource_name=resource_name,
                page_size=ingestion_params.batch_size,
                connection_provider=connection_provider,
            )
        except Exception as e:
            logger.error(
                "Failed to create data source for SPARQL resource '%s': %s",
                resource_name,
                e,
                exc_info=True,
            )
            raise
        registry.register(source, resource_name=resource_name)
        logger.info(
            "Created SPARQL data source for class <%s> (%d request template(s)) "
            "from '%s' mapped to resource '%s'",
            connector.rdf_class,
            len(connector.value_combinations()),
            connector.endpoint_url or connector.rdf_file,
            resource_name,
        )

    @classmethod
    def sparql_source(
        cls,
        connector: SparqlConnector,
        bindings: Bindings,
        *,
        resource_name: str,
        page_size: int,
        connection_provider: ConnectionProvider,
    ) -> AbstractDataSource:
        """Return the data source that reads *connector*'s instances.

        * **Endpoint mode** (``connector.endpoint_url`` is set, or its
          ``conn_proxy`` names a SPARQL endpoint): a
          :class:`SparqlEndpointDataSource` that queries the remote endpoint.
        * **File mode** (``connector.rdf_file`` is set): an
          :class:`RdfFileDataSource` that parses a local RDF file.

        A connector with ``values`` becomes one source per value combination,
        read in order, its rows carrying the values.
        """
        if connector.values:
            return cls._enumerated(
                connector,
                lambda one: cls.sparql_source(
                    one,
                    bindings,
                    resource_name=resource_name,
                    page_size=page_size,
                    connection_provider=connection_provider,
                ),
            )
        # A public endpoint needs no conn_proxy; a declared one must resolve, and
        # gives the endpoint when the connector does not name one.
        endpoint_url = connector.endpoint_url
        username: str | None = None
        password: str | None = None
        if bindings.get_conn_proxy_for_connector(connector) is not None:
            cfg = connector_config(
                connector, bindings, connection_provider, SparqlEndpointConfig
            )
            username = cfg.username
            password = cfg.password
            endpoint_url = endpoint_url or cfg.query_endpoint
        if endpoint_url:
            from graflo.data_source.rdf import (
                SparqlEndpointDataSource,
                SparqlSourceConfig,
            )

            return SparqlEndpointDataSource(
                config=SparqlSourceConfig(
                    endpoint_url=endpoint_url,
                    rdf_class=connector.rdf_class,
                    graph_uri=connector.graph_uri,
                    sparql_query=connector.sparql_query,
                    username=username,
                    password=password,
                    page_size=page_size,
                ),
                same_as=connector.same_as,
                typed_objects=connector.typed_objects,
            )
        if connector.rdf_file:
            from graflo.data_source.rdf import RdfFileDataSource

            return RdfFileDataSource(
                path=connector.rdf_file,
                rdf_class=connector.rdf_class,
                same_as=connector.same_as,
                typed_objects=connector.typed_objects,
            )
        raise ValueError(
            "SparqlConnector has no endpoint_url, no rdf_file and no conn_proxy "
            "naming a SPARQL endpoint"
        )

    @staticmethod
    def _enumerated(
        connector: _Templated,
        source_for: Callable[[_Templated], AbstractDataSource],
    ) -> EnumeratedDataSource:
        """One source per value combination of *connector*, read in order."""
        members = [
            EnumeratedMember(
                values=values, source=source_for(connector.rendered(values))
            )
            for values in connector.value_combinations()
        ]
        return EnumeratedDataSource(
            members=members, source_type=members[0].source.source_type
        )

    @staticmethod
    def api_config(
        connector: APIConnector,
        bindings: Bindings,
        connection_provider: ConnectionProvider,
        *,
        page_size_override: int | None = None,
    ) -> APIConfig:
        """Resolve *connector*'s connection and return its runtime request config.

        Raises:
            ValueError: No REST API connection is registered for the connector.
        """
        runtime = connector_config(
            connector, bindings, connection_provider, RestApiConnConfig
        )
        return connector.build_api_config(
            base_url=runtime.base_url,
            auth=runtime.auth,
            default_headers=runtime.default_headers,
            page_size_override=page_size_override,
        )

    #: Request settings a child connector takes from its parent unless it sets them.
    _INHERITED_REQUEST_SETTINGS = (
        "headers",
        "timeout",
        "retries",
        "retry_backoff_factor",
        "retry_status_forcelist",
        "verify",
    )

    @classmethod
    def api_source(
        cls,
        connector: APIConnector,
        bindings: Bindings,
        connection_provider: ConnectionProvider,
        *,
        page_size_override: int | None = None,
    ) -> AbstractDataSource:
        """Return the data source that fetches *connector*'s records.

        A connector with ``values`` becomes one source per value combination,
        read in order, its rows carrying the values. A connector with a
        ``parent`` becomes one request per parent record; *bindings* name the
        parent. It uses its own ``conn_proxy`` connection when it has one, else
        its parent's, and takes the parent's request settings it does not set.

        Raises:
            ValueError: No REST API connection is registered for the connector
                or any of its parents.
        """
        from graflo.data_source.api import APIDataSource, DependentAPIDataSource

        if connector.values:
            return cls._enumerated(
                connector,
                lambda one: cls.api_source(
                    one,
                    bindings,
                    connection_provider,
                    page_size_override=page_size_override,
                ),
            )
        parent = (
            None
            if connector.parent is None
            else bindings.get_parent_connector(connector)
        )
        if parent is None or connector.parent is None:
            return APIDataSource(
                config=cls.api_config(
                    connector,
                    bindings,
                    connection_provider,
                    page_size_override=page_size_override,
                )
            )
        unset = [
            name
            for name in cls._INHERITED_REQUEST_SETTINGS
            if name not in connector.model_fields_set
        ]
        effective = connector.model_copy(
            update={name: getattr(parent, name) for name in unset}
        )
        runtime = cls._runtime_connection(connector, bindings, connection_provider)
        config = effective.build_api_config(
            base_url=runtime.base_url,
            auth=runtime.auth,
            default_headers=runtime.default_headers,
            page_size_override=page_size_override,
        )
        return DependentAPIDataSource(
            parent=cls.api_source(parent, bindings, connection_provider),
            config=config,
            fields=dict(connector.parent.fields),
        )

    @staticmethod
    def _runtime_connection(
        connector: APIConnector,
        bindings: Bindings,
        connection_provider: ConnectionProvider,
    ) -> RestApiConnConfig:
        """The REST connection of *connector*, else of its nearest parent with a ``conn_proxy``."""
        current: APIConnector | None = connector
        while current is not None:
            if bindings.get_conn_proxy_for_connector(current) is not None:
                return connector_config(
                    current, bindings, connection_provider, RestApiConnConfig
                )
            current = bindings.get_parent_connector(current)
        raise ValueError(
            f"API connector {connector.name or connector.path!r} and its parents "
            "have no conn_proxy"
        )

    def _register_api_sources(
        self,
        registry: DataSourceRegistry,
        resource_name: str,
        connector: APIConnector,
        bindings: Bindings,
        ingestion_params: IngestionParams,
        connection_provider: ConnectionProvider,
    ) -> None:
        """Register REST API data sources for a resource."""
        # The connector's declared page size is what the endpoint accepts; the
        # global batch size replaces it only when the caller set one explicitly.
        page_size_override = (
            ingestion_params.batch_size
            if connector.pagination is not None
            and "batch_size" in ingestion_params.model_fields_set
            else None
        )
        api_source = self.api_source(
            connector,
            bindings,
            connection_provider,
            page_size_override=page_size_override,
        )
        registry.register(api_source, resource_name=resource_name)

        logger.info(
            "Created API data source for path '%s' (%d request template(s)) "
            "mapped to resource '%s'",
            connector.path,
            len(connector.value_combinations()),
            resource_name,
        )

    @staticmethod
    def _register_push_source(
        registry: DataSourceRegistry,
        resource_name: str,
        connector: PushConnector,
        records: Sequence[Mapping[str, Any]],
    ) -> None:
        """Register pushed records as an in-memory source for a resource."""
        from graflo.data_source.api_response import get_at_path
        from graflo.data_source.memory import InMemoryDataSource

        rows: list[dict] = []
        for record in records:
            if connector.records_path is None:
                items: list[Any] = [record]
            else:
                found = get_at_path(record, connector.records_path)
                items = found if isinstance(found, list) else []
            rows.extend(
                {**connector.row_annotations, **item}
                for item in items
                if isinstance(item, Mapping)
            )
        registry.register(InMemoryDataSource(data=rows), resource_name)

    def _register_kafka_sources(
        self,
        registry: DataSourceRegistry,
        resource_name: str,
        connector: KafkaConnector,
        bindings: Bindings,
        connection_provider: ConnectionProvider,
    ) -> None:
        """Register Kafka topic data sources for a resource."""
        kafka_conn = connector_config(
            connector, bindings, connection_provider, KafkaConnConfig
        )

        from graflo.data_source.kafka import KafkaDataSource

        kafka_config = connector.build_kafka_config(conn=kafka_conn)
        kafka_source = KafkaDataSource(config=kafka_config)
        registry.register(kafka_source, resource_name=resource_name)

        logger.info(
            "Created Kafka data source for topics %s (group=%s) mapped to resource '%s'",
            connector.topics,
            connector.group_id,
            resource_name,
        )

Attributes

ingestion_model = ingestion_model instance-attribute
schema = schema instance-attribute

Methods:

__init__(schema, ingestion_model)
Source code in graflo/hq/registry_builder.py
def __init__(self, schema: Schema, ingestion_model: IngestionModel):
    self.schema = schema
    self.ingestion_model = ingestion_model
api_config(connector, bindings, connection_provider, *, page_size_override=None) staticmethod

Resolve connector's connection and return its runtime request config.

Raises:

Type Description
ValueError

No REST API connection is registered for the connector.

Source code in graflo/hq/registry_builder.py
@staticmethod
def api_config(
    connector: APIConnector,
    bindings: Bindings,
    connection_provider: ConnectionProvider,
    *,
    page_size_override: int | None = None,
) -> APIConfig:
    """Resolve *connector*'s connection and return its runtime request config.

    Raises:
        ValueError: No REST API connection is registered for the connector.
    """
    runtime = connector_config(
        connector, bindings, connection_provider, RestApiConnConfig
    )
    return connector.build_api_config(
        base_url=runtime.base_url,
        auth=runtime.auth,
        default_headers=runtime.default_headers,
        page_size_override=page_size_override,
    )
api_source(connector, bindings, connection_provider, *, page_size_override=None) classmethod

Return the data source that fetches connector's records.

A connector with values becomes one source per value combination, read in order, its rows carrying the values. A connector with a parent becomes one request per parent record; bindings name the parent. It uses its own conn_proxy connection when it has one, else its parent's, and takes the parent's request settings it does not set.

Raises:

Type Description
ValueError

No REST API connection is registered for the connector or any of its parents.

Source code in graflo/hq/registry_builder.py
@classmethod
def api_source(
    cls,
    connector: APIConnector,
    bindings: Bindings,
    connection_provider: ConnectionProvider,
    *,
    page_size_override: int | None = None,
) -> AbstractDataSource:
    """Return the data source that fetches *connector*'s records.

    A connector with ``values`` becomes one source per value combination,
    read in order, its rows carrying the values. A connector with a
    ``parent`` becomes one request per parent record; *bindings* name the
    parent. It uses its own ``conn_proxy`` connection when it has one, else
    its parent's, and takes the parent's request settings it does not set.

    Raises:
        ValueError: No REST API connection is registered for the connector
            or any of its parents.
    """
    from graflo.data_source.api import APIDataSource, DependentAPIDataSource

    if connector.values:
        return cls._enumerated(
            connector,
            lambda one: cls.api_source(
                one,
                bindings,
                connection_provider,
                page_size_override=page_size_override,
            ),
        )
    parent = (
        None
        if connector.parent is None
        else bindings.get_parent_connector(connector)
    )
    if parent is None or connector.parent is None:
        return APIDataSource(
            config=cls.api_config(
                connector,
                bindings,
                connection_provider,
                page_size_override=page_size_override,
            )
        )
    unset = [
        name
        for name in cls._INHERITED_REQUEST_SETTINGS
        if name not in connector.model_fields_set
    ]
    effective = connector.model_copy(
        update={name: getattr(parent, name) for name in unset}
    )
    runtime = cls._runtime_connection(connector, bindings, connection_provider)
    config = effective.build_api_config(
        base_url=runtime.base_url,
        auth=runtime.auth,
        default_headers=runtime.default_headers,
        page_size_override=page_size_override,
    )
    return DependentAPIDataSource(
        parent=cls.api_source(parent, bindings, connection_provider),
        config=config,
        fields=dict(connector.parent.fields),
    )
build(bindings, ingestion_params, connection_provider=None, *, strict=False, pushed=None)

Return a populated :class:DataSourceRegistry.

For each ingestion resource, registers every bound connector (same resource may have multiple physical sources).

A source that cannot be built -- no connector bound, no connection configuration registered, a registration error -- is logged and skipped; with strict the build raises once every resource has been tried, listing them all.

pushed holds the records for push connectors, keyed by connector name. A push connector with nothing pushed is a source that cannot be built.

Source code in graflo/hq/registry_builder.py
def build(
    self,
    bindings: Bindings,
    ingestion_params: IngestionParams,
    connection_provider: ConnectionProvider | None = None,
    *,
    strict: bool = False,
    pushed: Mapping[str, Sequence[Mapping[str, Any]]] | None = None,
) -> DataSourceRegistry:
    """Return a populated :class:`DataSourceRegistry`.

    For each ingestion resource, registers every bound connector (same
    resource may have multiple physical sources).

    A source that cannot be built -- no connector bound, no connection
    configuration registered, a registration error -- is logged and
    skipped; with *strict* the build raises once every resource has been
    tried, listing them all.

    *pushed* holds the records for push connectors, keyed by connector
    name. A push connector with nothing pushed is a source that cannot be
    built.
    """
    registry = DataSourceRegistry()
    provider = connection_provider or EmptyConnectionProvider()
    failures: list[str] = []

    resources_filter: set[str] | None = None
    if ingestion_params.resources is not None:
        resources_filter = set(ingestion_params.resources)

    connectors_filter: set[str] | None = None
    if ingestion_params.connectors is not None:
        connectors_filter = bindings.resolve_connector_refs_to_hashes(
            ingestion_params.connectors
        )

    for resource in self.ingestion_model.resources:
        resource_name = resource.name
        if resources_filter is not None and resource_name not in resources_filter:
            continue
        connectors = bindings.get_connectors_for_resource(resource_name)
        if not connectors:
            msg = f"No connectors bound for resource '{resource_name}'"
            logger.warning("%s, skipping", msg)
            failures.append(msg)
            continue

        for connector in connectors:
            if (
                connectors_filter is not None
                and connector.hash not in connectors_filter
            ):
                continue
            cref = connector.name or connector.hash
            kind = connector.bound_source_kind()
            if not isinstance(connector, TableConnector):
                self._report_unnarrowed(resource_name, connector)

            if kind == BoundSourceKind.FILE:
                if not isinstance(connector, FileConnector):
                    msg = (
                        f"Connector '{cref}' for resource '{resource_name}' "
                        f"is not a FileConnector"
                    )
                    logger.warning("%s, skipping", msg)
                    failures.append(msg)
                    continue
                try:
                    self._register_file_sources(
                        registry,
                        resource_name,
                        connector,
                        bindings,
                        ingestion_params,
                        provider,
                    )
                except Exception as e:
                    msg = (
                        f"Failed to register FILE source for resource "
                        f"'{resource_name}' (connector '{cref}'): {e}"
                    )
                    if not strict:
                        logger.warning("%s, skipping", msg)
                    failures.append(msg)

            elif kind == BoundSourceKind.SQL_TABLE:
                if not isinstance(connector, TableConnector):
                    msg = (
                        f"Connector '{cref}' for resource '{resource_name}' "
                        f"is not a TableConnector"
                    )
                    logger.warning("%s, skipping", msg)
                    failures.append(msg)
                    continue
                try:
                    self._register_sql_table_sources(
                        registry,
                        resource_name,
                        connector,
                        bindings,
                        ingestion_params,
                        provider,
                    )
                except Exception as e:
                    msg = (
                        f"Failed to register SQL source for resource "
                        f"'{resource_name}' (connector '{cref}'): {e}"
                    )
                    if not strict:
                        logger.warning("%s, skipping", msg)
                    failures.append(msg)

            elif kind == BoundSourceKind.SPARQL:
                if not isinstance(connector, SparqlConnector):
                    msg = (
                        f"Connector '{cref}' for resource '{resource_name}' "
                        f"is not a SparqlConnector"
                    )
                    logger.warning("%s, skipping", msg)
                    failures.append(msg)
                    continue
                try:
                    self._register_sparql_sources(
                        registry,
                        resource_name,
                        connector,
                        bindings,
                        ingestion_params,
                        provider,
                    )
                except Exception as e:
                    msg = (
                        f"Failed to register SPARQL source for resource "
                        f"'{resource_name}' (connector '{cref}'): {e}"
                    )
                    if not strict:
                        logger.warning("%s, skipping", msg)
                    failures.append(msg)

            elif kind == BoundSourceKind.API:
                if not isinstance(connector, APIConnector):
                    msg = (
                        f"Connector '{cref}' for resource '{resource_name}' "
                        f"is not an APIConnector"
                    )
                    logger.warning("%s, skipping", msg)
                    failures.append(msg)
                    continue
                try:
                    self._register_api_sources(
                        registry,
                        resource_name,
                        connector,
                        bindings,
                        ingestion_params,
                        provider,
                    )
                except Exception as e:
                    msg = (
                        f"Failed to register API source for resource "
                        f"'{resource_name}' (connector '{cref}'): {e}"
                    )
                    if not strict:
                        logger.warning("%s, skipping", msg)
                    failures.append(msg)

            elif kind == BoundSourceKind.KAFKA:
                if not isinstance(connector, KafkaConnector):
                    msg = (
                        f"Connector '{cref}' for resource '{resource_name}' "
                        f"is not a KafkaConnector"
                    )
                    logger.warning("%s, skipping", msg)
                    failures.append(msg)
                    continue
                try:
                    self._register_kafka_sources(
                        registry,
                        resource_name,
                        connector,
                        bindings,
                        provider,
                    )
                except Exception as e:
                    msg = (
                        f"Failed to register Kafka source for resource "
                        f"'{resource_name}' (connector '{cref}'): {e}"
                    )
                    if not strict:
                        logger.warning("%s, skipping", msg)
                    failures.append(msg)

            elif kind == BoundSourceKind.PUSH:
                if not isinstance(connector, PushConnector):
                    msg = (
                        f"Connector '{cref}' for resource '{resource_name}' "
                        f"is not a PushConnector"
                    )
                    logger.warning("%s, skipping", msg)
                    failures.append(msg)
                    continue
                records = (pushed or {}).get(connector.name)
                if not records:
                    msg = (
                        f"Push connector '{cref}' for resource "
                        f"'{resource_name}': nothing was pushed"
                    )
                    if not strict:
                        logger.warning("%s, skipping", msg)
                    failures.append(msg)
                    continue
                self._register_push_source(
                    registry, resource_name, connector, records
                )

            else:
                msg = (
                    f"Unsupported bound source kind '{kind}' "
                    f"for resource '{resource_name}' (connector '{cref}')"
                )
                logger.warning("%s, skipping", msg)
                failures.append(msg)

    if strict and failures:
        details = "\n".join(f"- {item}" for item in failures)
        raise ValueError(f"Registry build failed in strict mode:\n{details}")

    return registry
discover_files(fpath, connector, limit_files=None) staticmethod

Discover files matching connector in a directory.

Parameters:

Name Type Description Default
fpath Path | str

Directory to search in.

required
connector FileConnector

Connector used to match files.

required
limit_files int | None

Optional cap on the number of files returned.

None

Returns:

Type Description
list[Path]

Matching file paths.

Source code in graflo/hq/registry_builder.py
@staticmethod
def discover_files(
    fpath: Path | str, connector: FileConnector, limit_files: int | None = None
) -> list[Path]:
    """Discover files matching *connector* in a directory.

    Args:
        fpath: Directory to search in.
        connector: Connector used to match files.
        limit_files: Optional cap on the number of files returned.

    Returns:
        Matching file paths.
    """
    if connector.sub_path is None:
        raise ValueError("connector.sub_path is required")
    path = Path(fpath) if isinstance(fpath, str) else fpath

    candidates = path.rglob("*") if connector.recursive else path.iterdir()
    files = sorted(
        f
        for f in candidates
        if f.is_file()
        and (
            True
            if connector.regex is None
            else re.search(connector.regex, f.name) is not None
        )
    )

    if limit_files is not None:
        files = files[:limit_files]

    return files
file_sources(connector, bindings, connection_provider, *, limit_files=None, encoding=EncodingType.UTF_8) classmethod

One data source per file connector matches.

With a conn_proxy the files are found under that files connection's root, a local directory or an S3 prefix; without one, under the local sub_path.

Source code in graflo/hq/registry_builder.py
@classmethod
def file_sources(
    cls,
    connector: FileConnector,
    bindings: Bindings,
    connection_provider: ConnectionProvider,
    *,
    limit_files: int | None = None,
    encoding: EncodingType = EncodingType.UTF_8,
) -> list[AbstractDataSource]:
    """One data source per file *connector* matches.

    With a ``conn_proxy`` the files are found under that files connection's
    root, a local directory or an S3 prefix; without one, under the local
    ``sub_path``.
    """
    if bindings.get_conn_proxy_for_connector(connector) is None:
        return [
            DataSourceFactory.create_file_data_source(
                path=path, file_type=connector.file_type, encoding=encoding
            )
            for path in cls.discover_files(
                connector.sub_path.expanduser(),
                connector=connector,
                limit_files=limit_files,
            )
        ]
    from graflo.data_source.files import file_data_source, list_files

    files = connector_config(
        connector, bindings, connection_provider, FilesConnConfig
    )
    return [
        file_data_source(
            files, relative, file_type=connector.file_type, encoding=encoding
        )
        for relative in list_files(files, connector, limit=limit_files)
    ]
sparql_source(connector, bindings, *, resource_name, page_size, connection_provider) classmethod

Return the data source that reads connector's instances.

  • Endpoint mode (connector.endpoint_url is set, or its conn_proxy names a SPARQL endpoint): a :class:SparqlEndpointDataSource that queries the remote endpoint.
  • File mode (connector.rdf_file is set): an :class:RdfFileDataSource that parses a local RDF file.

A connector with values becomes one source per value combination, read in order, its rows carrying the values.

Source code in graflo/hq/registry_builder.py
@classmethod
def sparql_source(
    cls,
    connector: SparqlConnector,
    bindings: Bindings,
    *,
    resource_name: str,
    page_size: int,
    connection_provider: ConnectionProvider,
) -> AbstractDataSource:
    """Return the data source that reads *connector*'s instances.

    * **Endpoint mode** (``connector.endpoint_url`` is set, or its
      ``conn_proxy`` names a SPARQL endpoint): a
      :class:`SparqlEndpointDataSource` that queries the remote endpoint.
    * **File mode** (``connector.rdf_file`` is set): an
      :class:`RdfFileDataSource` that parses a local RDF file.

    A connector with ``values`` becomes one source per value combination,
    read in order, its rows carrying the values.
    """
    if connector.values:
        return cls._enumerated(
            connector,
            lambda one: cls.sparql_source(
                one,
                bindings,
                resource_name=resource_name,
                page_size=page_size,
                connection_provider=connection_provider,
            ),
        )
    # A public endpoint needs no conn_proxy; a declared one must resolve, and
    # gives the endpoint when the connector does not name one.
    endpoint_url = connector.endpoint_url
    username: str | None = None
    password: str | None = None
    if bindings.get_conn_proxy_for_connector(connector) is not None:
        cfg = connector_config(
            connector, bindings, connection_provider, SparqlEndpointConfig
        )
        username = cfg.username
        password = cfg.password
        endpoint_url = endpoint_url or cfg.query_endpoint
    if endpoint_url:
        from graflo.data_source.rdf import (
            SparqlEndpointDataSource,
            SparqlSourceConfig,
        )

        return SparqlEndpointDataSource(
            config=SparqlSourceConfig(
                endpoint_url=endpoint_url,
                rdf_class=connector.rdf_class,
                graph_uri=connector.graph_uri,
                sparql_query=connector.sparql_query,
                username=username,
                password=password,
                page_size=page_size,
            ),
            same_as=connector.same_as,
            typed_objects=connector.typed_objects,
        )
    if connector.rdf_file:
        from graflo.data_source.rdf import RdfFileDataSource

        return RdfFileDataSource(
            path=connector.rdf_file,
            rdf_class=connector.rdf_class,
            same_as=connector.same_as,
            typed_objects=connector.typed_objects,
        )
    raise ValueError(
        "SparqlConnector has no endpoint_url, no rdf_file and no conn_proxy "
        "naming a SPARQL endpoint"
    )

Functions: