Skip to content

Tidylake

Get the current context instance.

Returns:

Name Type Description
TidyLakeContext TidyLakeContext

The current context instance.

Source code in src/tidylake/core/context.py
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
def get_or_create_context(config_file_path: str = None) -> TidyLakeContext:
    """
    Get the current context instance.

    Returns:
        TidyLakeContext: The current context instance.
    """
    global context

    # If the context instance is already created, return it, else create a new one
    if context is None:
        # Load the context configuration
        config_file = config_file_path if config_file_path else DEFAULT_CONFIG_FILE
        context_config = TidyLakeContext.get_config(config_file)

        # Get the directory of the context config file to resolve relative paths
        context_config_dir = Path(config_file).parent

        context_config_data = context_config.get("tidylake", {})

        context_config_data_plugins = context_config_data.get("plugins", {})

        # Initialize the context instance
        context = TidyLakeContext(
            name=context_config_data.get("name", "tidylake"),
            compute_engine_config=context_config_data_plugins.get("compute_engine"),
            config_dir=context_config_dir,
        )

        # Create and add data products from the configuration
        include_data_products = context_config_data.get("include_data_products", [])

        for data_product_config_file in include_data_products:
            # Resolve data product config file path relative to config directory
            data_product_config_path = context_config_dir / data_product_config_file

            # Load the data product configuration
            data_product_config = DataProduct.get_config(str(data_product_config_path))

            # Create a Data product instance from the configuration
            data_product = DataProduct(
                name=data_product_config.get("data_product").get("name"),
                schema=data_product_config.get("data_product").get("schema", {}),
                script=data_product_config.get("data_product").get("script", ""),
                config_dir=context_config_dir,
            )

            # Add the data product
            context.add_data_product(data_product)

    return context