2021-03-16 00:49:32 +03:00
|
|
|
"""Generate lookml from namespaces."""
|
2021-03-23 00:08:38 +03:00
|
|
|
import logging
|
2021-03-16 00:49:32 +03:00
|
|
|
from pathlib import Path
|
2021-04-15 05:34:36 +03:00
|
|
|
from typing import Dict, Iterable
|
2021-03-16 00:49:32 +03:00
|
|
|
|
|
|
|
import click
|
2021-03-23 00:08:38 +03:00
|
|
|
import lkml
|
2021-03-16 00:49:32 +03:00
|
|
|
import yaml
|
|
|
|
from google.cloud import bigquery
|
|
|
|
|
2021-04-30 19:31:29 +03:00
|
|
|
from .explores import EXPLORE_TYPES
|
|
|
|
from .views import VIEW_TYPES, View, ViewDict
|
2021-03-16 00:49:32 +03:00
|
|
|
|
|
|
|
|
2021-04-14 14:56:17 +03:00
|
|
|
def _generate_views(client, out_dir: Path, views: Iterable[View]) -> Iterable[Path]:
|
|
|
|
for view in views:
|
|
|
|
path = out_dir / f"{view.name}.view.lkml"
|
2021-04-15 05:34:36 +03:00
|
|
|
lookml = {"views": view.to_lookml(client)}
|
|
|
|
path.write_text(lkml.dump(lookml))
|
2021-03-23 00:08:38 +03:00
|
|
|
yield path
|
|
|
|
|
|
|
|
|
|
|
|
def _generate_explores(
|
2021-04-29 00:30:41 +03:00
|
|
|
client, out_dir: Path, namespace: str, explores: dict, views_dir: Path
|
2021-03-23 00:08:38 +03:00
|
|
|
) -> Iterable[Path]:
|
|
|
|
for explore_name, defn in explores.items():
|
2021-04-30 19:31:29 +03:00
|
|
|
explore = EXPLORE_TYPES[defn["type"]].from_dict(explore_name, defn, views_dir)
|
2021-03-23 00:08:38 +03:00
|
|
|
file_lookml = {
|
2021-04-16 04:42:11 +03:00
|
|
|
# Looker validates all included files,
|
|
|
|
# so if we're not explicit about files here, validation takes
|
|
|
|
# forever as looker re-validates all views for every explore (if we used *).
|
|
|
|
"includes": [
|
|
|
|
f"/looker-hub/{namespace}/views/{view}.view.lkml"
|
|
|
|
for view in explore.get_dependent_views()
|
|
|
|
],
|
2021-04-15 05:34:36 +03:00
|
|
|
"explores": [explore.to_lookml()],
|
2021-03-23 00:08:38 +03:00
|
|
|
}
|
|
|
|
path = out_dir / (explore_name + ".explore.lkml")
|
|
|
|
path.write_text(lkml.dump(file_lookml))
|
|
|
|
yield path
|
|
|
|
|
|
|
|
|
2021-04-15 05:34:36 +03:00
|
|
|
def _get_views_from_dict(views: Dict[str, ViewDict]) -> Iterable[View]:
|
2021-04-14 14:56:17 +03:00
|
|
|
for view_name, view_info in views.items():
|
2021-04-30 19:31:29 +03:00
|
|
|
yield VIEW_TYPES[view_info["type"]].from_dict(view_name, view_info) # type: ignore
|
2021-04-14 14:56:17 +03:00
|
|
|
|
|
|
|
|
2021-04-29 00:30:41 +03:00
|
|
|
def _lookml(namespaces, target_dir):
|
2021-03-16 00:49:32 +03:00
|
|
|
client = bigquery.Client()
|
|
|
|
_namespaces = yaml.safe_load(namespaces)
|
|
|
|
target = Path(target_dir)
|
2021-04-14 14:56:17 +03:00
|
|
|
for namespace, lookml_objects in _namespaces.items():
|
2021-03-23 00:08:38 +03:00
|
|
|
logging.info(f"\nGenerating namespace {namespace}")
|
|
|
|
|
|
|
|
view_dir = target / namespace / "views"
|
2021-03-16 00:49:32 +03:00
|
|
|
view_dir.mkdir(parents=True, exist_ok=True)
|
2021-04-14 14:56:17 +03:00
|
|
|
views = _get_views_from_dict(lookml_objects.get("views", {}))
|
2021-03-23 00:08:38 +03:00
|
|
|
|
|
|
|
logging.info(" Generating views")
|
|
|
|
for view_path in _generate_views(client, view_dir, views):
|
|
|
|
logging.info(f" ...Generating {view_path}")
|
|
|
|
|
|
|
|
explore_dir = target / namespace / "explores"
|
|
|
|
explore_dir.mkdir(parents=True, exist_ok=True)
|
2021-04-14 14:56:17 +03:00
|
|
|
explores = lookml_objects.get("explores", {})
|
2021-03-23 00:08:38 +03:00
|
|
|
logging.info(" Generating explores")
|
|
|
|
for explore_path in _generate_explores(
|
2021-04-29 00:30:41 +03:00
|
|
|
client, explore_dir, namespace, explores, view_dir
|
2021-03-23 00:08:38 +03:00
|
|
|
):
|
|
|
|
logging.info(f" ...Generating {explore_path}")
|
2021-04-29 00:30:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
@click.command(help=__doc__)
|
|
|
|
@click.option(
|
|
|
|
"--namespaces",
|
|
|
|
default="namespaces.yaml",
|
|
|
|
type=click.File(),
|
|
|
|
help="Path to a yaml namespaces file",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--target-dir",
|
|
|
|
default="looker-hub/",
|
|
|
|
type=click.Path(),
|
|
|
|
help="Path to a directory where lookml will be written",
|
|
|
|
)
|
|
|
|
def lookml(namespaces, target_dir):
|
|
|
|
"""Generate lookml from namespaces."""
|
|
|
|
return _lookml(namespaces, target_dir)
|