Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added encoding option, docstring and kept format of original file #7

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/cube_dbt/dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from urllib.request import urlopen
from cube_dbt.model import Model

import locale

class Dbt:
def __init__(self, manifest: dict) -> None:
self.manifest = manifest
Expand All @@ -13,16 +14,36 @@ def __init__(self, manifest: dict) -> None:
pass

@staticmethod
def from_file(manifest_path: str) -> 'Dbt':
with open(manifest_path, 'r') as file:
manifest = json.loads(file.read())
return Dbt(manifest)
def from_file(manifest_path: str, encoding:str = None) -> 'Dbt':
"""Reads a DBT manifest.json file from local path

Args:
manifest_path (str): The path to the manifest file, read from the top-level directory of the Cube environment
encoding (str, optional): Encoding for the manifest.json file. Uses the system locale preferred encoding if not specified.

Returns:
Dbt: Dbt manifest class to interact with in Cube
"""
if encoding is None:
encoding = locale.getpreferredencoding()
with open(manifest_path, "r", encoding=encoding) as file:
manifest = json.loads(file.read())
return Dbt(manifest)

@staticmethod
def from_url(manifest_url: str) -> 'Dbt':
"""
Creates an instance of the Dbt class by loading a JSON manifest from a specified URL.

Args:
manifest_url (str): The URL pointing to the JSON manifest file. This URL should be accessible and the file should be in a valid JSON format.

Returns:
Dbt: An instance of the Dbt class initialized with the manifest loaded from the given URL.
"""
with urlopen(manifest_url) as file:
manifest = json.loads(file.read())
return Dbt(manifest)
manifest = json.loads(file.read())
return Dbt(manifest)

def filter(self, paths: list[str]=[], tags: list[str]=[], names: list[str]=[]) -> 'Dbt':
self.paths = paths
Expand Down