generator.utils

Utils.

 1"""Utils."""
 2
 3import os
 4import urllib.request
 5from pathlib import Path
 6
 7LOOKER_HUB_URL = "https://raw.githubusercontent.com/mozilla/looker-hub/main"
 8
 9LOOKER_HUB_DIR = os.getenv("LOOKER_HUB_DIR")
10
11
12def get_file_from_looker_hub(path: Path):
13    """Download a specific lookml artifact from looker-hub."""
14    file = path.name
15    artifact_type = path.parent.name
16    namespace = path.parent.parent.name
17    if LOOKER_HUB_DIR:
18        src = Path(LOOKER_HUB_DIR) / namespace / artifact_type / file
19        print(f"local: {src}")
20        path.parent.mkdir(parents=True, exist_ok=True)
21        path.write_text(src.read_text(encoding="utf-8"), encoding="utf-8")
22    else:
23        print(f"{LOOKER_HUB_URL}/{namespace}/{artifact_type}/{file}")
24        with urllib.request.urlopen(
25            f"{LOOKER_HUB_URL}/{namespace}/{artifact_type}/{file}"
26        ) as response:
27            lookml = response.read().decode(response.headers.get_content_charset())
28            path.parent.mkdir(parents=True, exist_ok=True)
29            path.write_text(lookml)
LOOKER_HUB_URL = 'https://raw.githubusercontent.com/mozilla/looker-hub/main'
LOOKER_HUB_DIR = None
def get_file_from_looker_hub(path: pathlib.Path):
13def get_file_from_looker_hub(path: Path):
14    """Download a specific lookml artifact from looker-hub."""
15    file = path.name
16    artifact_type = path.parent.name
17    namespace = path.parent.parent.name
18    if LOOKER_HUB_DIR:
19        src = Path(LOOKER_HUB_DIR) / namespace / artifact_type / file
20        print(f"local: {src}")
21        path.parent.mkdir(parents=True, exist_ok=True)
22        path.write_text(src.read_text(encoding="utf-8"), encoding="utf-8")
23    else:
24        print(f"{LOOKER_HUB_URL}/{namespace}/{artifact_type}/{file}")
25        with urllib.request.urlopen(
26            f"{LOOKER_HUB_URL}/{namespace}/{artifact_type}/{file}"
27        ) as response:
28            lookml = response.read().decode(response.headers.get_content_charset())
29            path.parent.mkdir(parents=True, exist_ok=True)
30            path.write_text(lookml)

Download a specific lookml artifact from looker-hub.