import functools import os import re from jinja2 import Environment, FileSystemLoader def facts_prod_mapping(db_schema='facts.prod'): """ Converts ref to dbt-models to actual tables/views in the schema. Example: 'base_dim_sometheing' => 'facts.prod.dim_something' :param db_schema: snowflake database and schema, '.' separated :return: callable which can be used as ref_function in jinja_environment """ return functools.partial(re.sub, 'base_(.+)', f'{db_schema}.\\1') def jinja_environment(templates_path='.', ref_function=facts_prod_mapping()): """ Builds jinja2 environment which can be used to expand dbt templates with macros. :param templates_path: path to your template files :param ref_function: callable which will be used for resolving {{ ref('table') }} :return: jinja2 Environment """ my_dir = os.path.dirname(os.path.realpath(__file__)) macros_dir = os.path.join(os.path.dirname(my_dir), 'macros') env = Environment( loader=FileSystemLoader([templates_path, macros_dir]), ) env.globals.update(ref=ref_function) return env