Skip to content

Commit

Permalink
Wrap ruamel yaml library import due to type support
Browse files Browse the repository at this point in the history
Signed-off-by: Wei-Chun, Chang <[email protected]>
  • Loading branch information
wcchang1115 committed Nov 7, 2023
1 parent 02b474b commit 35eee78
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 69 deletions.
5 changes: 4 additions & 1 deletion piperider_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

from dateutil import tz
from rich.console import Console
from piperider_cli import yaml as pyml
try:
from piperider_cli.yaml import yaml_rich_type as pyml
except ImportError:
from piperider_cli.yaml import yaml_less_type as pyml

Check warning on line 15 in piperider_cli/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/__init__.py#L14-L15

Added lines #L14 - L15 were not covered by tests

PIPERIDER_USER_HOME = os.path.expanduser('~/.piperider')
if os.access(os.path.expanduser('~/'), os.W_OK) is False:
Expand Down
10 changes: 6 additions & 4 deletions piperider_cli/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from rich.console import Console

from piperider_cli import raise_exception_when_directory_not_writable
from piperider_cli.yaml import round_trip_load_yaml, safe_load_yaml
from piperider_cli.cli_utils import DbtUtil
from piperider_cli.datasource import DATASOURCE_PROVIDERS, DataSource
from piperider_cli.datasource.unsupported import UnsupportedDataSource
Expand All @@ -23,7 +22,10 @@
PipeRiderInvalidDataSourceError, \
DbtProjectNotFoundError, \
DbtProfileNotFoundError
from piperider_cli import yaml as pyml
try:
from piperider_cli.yaml import yaml_rich_type as pyml
except ImportError:
from piperider_cli.yaml import yaml_less_type as pyml

Check warning on line 28 in piperider_cli/configuration.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/configuration.py#L27-L28

Added lines #L27 - L28 were not covered by tests

# ref: https://docs.getdbt.com/dbt-cli/configure-your-profile
DBT_PROFILES_DIR_DEFAULT = '~/.dbt/'
Expand Down Expand Up @@ -488,7 +490,7 @@ def _load(cls, piperider_config_path=None, dbt_profile: str = None, dbt_target:
credentials = None
piperider_config_path = piperider_config_path or FileSystem.PIPERIDER_CONFIG_PATH

config = safe_load_yaml(piperider_config_path)
config = pyml.safe_load_yaml(piperider_config_path)
if config is None:
raise PipeRiderConfigError(piperider_config_path)

Expand Down Expand Up @@ -602,7 +604,7 @@ def flush_datasource(self, path):
:return:
"""

config = round_trip_load_yaml(path)
config = pyml.round_trip_load_yaml(path)

Check warning on line 607 in piperider_cli/configuration.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/configuration.py#L607

Added line #L607 was not covered by tests
if config is None:
raise PipeRiderConfigError(path)

Expand Down
5 changes: 4 additions & 1 deletion piperider_cli/dbtutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from jinja2 import UndefinedError
from rich.console import Console
from rich.table import Table
from piperider_cli import yaml as pyml
try:
from piperider_cli.yaml import yaml_rich_type as pyml
except ImportError:
from piperider_cli.yaml import yaml_less_type as pyml

Check warning on line 16 in piperider_cli/dbtutil.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/dbtutil.py#L15-L16

Added lines #L15 - L16 were not covered by tests

from piperider_cli import load_jinja_template, load_jinja_string_template
from piperider_cli.dbt.list_task import load_manifest, list_resources_unique_id_from_manifest, load_full_manifest
Expand Down
5 changes: 4 additions & 1 deletion piperider_cli/event/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import sentry_sdk
from rich.console import Console
from piperider_cli import yaml as pyml
try:
from piperider_cli.yaml import yaml_rich_type as pyml
except ImportError:
from piperider_cli.yaml import yaml_less_type as pyml

Check warning on line 11 in piperider_cli/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/event/__init__.py#L10-L11

Added lines #L10 - L11 were not covered by tests

from piperider_cli import PIPERIDER_USER_HOME, PIPERIDER_USER_PROFILE, is_executed_manually
from piperider_cli.event.collector import Collector
Expand Down
5 changes: 4 additions & 1 deletion piperider_cli/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from rich.table import Table

from piperider_cli import clone_directory
from piperider_cli.yaml import safe_load_yaml
try:
from piperider_cli.yaml.yaml_rich_type import safe_load_yaml
except ImportError:
from piperider_cli.yaml.yaml_less_type import safe_load_yaml

Check warning on line 14 in piperider_cli/initializer.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/initializer.py#L13-L14

Added lines #L13 - L14 were not covered by tests

from piperider_cli.configuration import Configuration, FileSystem
from piperider_cli.datasource import DataSource, FANCY_USER_INPUT
Expand Down
5 changes: 4 additions & 1 deletion piperider_cli/recipes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import piperider_cli.dbtutil as dbtutil
from piperider_cli import get_run_json_path, load_jinja_template, load_json
from piperider_cli import yaml as pyml
try:
from piperider_cli.yaml import yaml_rich_type as pyml
except ImportError:
from piperider_cli.yaml import yaml_less_type as pyml

Check warning on line 17 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L16-L17

Added lines #L16 - L17 were not covered by tests
from piperider_cli.configuration import Configuration, FileSystem
from piperider_cli.dbt import dbt_version
from piperider_cli.error import RecipeConfigException
Expand Down
60 changes: 0 additions & 60 deletions piperider_cli/yaml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,60 +0,0 @@
from pathlib import Path
from typing import Any, Callable, Optional, Union

from ruamel import yaml
from ruamel.yaml import StreamTextType, StreamType, VersionType
from ruamel.yaml import CommentedMap as _cm, CommentedSeq as _cs

_yaml = yaml.YAML()

CommentedMap = _cm
CommentedSeq = _cs
YAMLError = yaml.YAMLError


def load(stream: Union[Path, StreamTextType]) -> Any:
return _yaml.load(stream)


def allow_duplicate_keys_loader() -> Callable:
yml = yaml.YAML()
yml.allow_duplicate_keys = True
return yml.load


def safe_load(stream: StreamTextType, version: Optional[VersionType] = None) -> Any:
return yaml.safe_load(stream, version)


def dump(
data: Union[Path, StreamType], stream: Any = None, *, transform: Any = None
) -> Any:
return _yaml.dump(data, stream, transform=transform)


def safe_load_yaml(file_path):
try:
with open(file_path, 'r') as f:
payload = safe_load(f)
except yaml.YAMLError as e:
print(e)
return None
except FileNotFoundError:
return None
return payload


def round_trip_load_yaml(file_path):
with open(file_path, 'r') as f:
try:
payload = load(f)
except yaml.YAMLError as e:
print(e)
return None
return payload


def round_trip_dump(
data: Any,
stream: Optional[StreamType] = None):
return yaml.round_trip_dump(data, stream)
54 changes: 54 additions & 0 deletions piperider_cli/yaml/yaml_less_type/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import Any, Callable

Check warning on line 1 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L1

Added line #L1 was not covered by tests

from ruamel import yaml
from ruamel.yaml import CommentedMap as _cm, CommentedSeq as _cs

Check warning on line 4 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L3-L4

Added lines #L3 - L4 were not covered by tests

_yaml = yaml.YAML()

Check warning on line 6 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L6

Added line #L6 was not covered by tests

CommentedMap = _cm
CommentedSeq = _cs
YAMLError = yaml.YAMLError

Check warning on line 10 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L8-L10

Added lines #L8 - L10 were not covered by tests


def load(stream) -> Any:
return _yaml.load(stream)

Check warning on line 14 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L13-L14

Added lines #L13 - L14 were not covered by tests


def allow_duplicate_keys_loader() -> Callable:
yml = yaml.YAML()
yml.allow_duplicate_keys = True
return yml.load

Check warning on line 20 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L17-L20

Added lines #L17 - L20 were not covered by tests


def safe_load(stream, version=None) -> Any:
return yaml.safe_load(stream, version)

Check warning on line 24 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L23-L24

Added lines #L23 - L24 were not covered by tests


def dump(data, stream=None, *, transform=None) -> Any:
return _yaml.dump(data, stream, transform=transform)

Check warning on line 28 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L27-L28

Added lines #L27 - L28 were not covered by tests


def safe_load_yaml(file_path):
try:
with open(file_path, 'r') as f:
payload = safe_load(f)
except yaml.YAMLError as e:
print(e)
return None
except FileNotFoundError:
return None
return payload

Check warning on line 40 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L31-L40

Added lines #L31 - L40 were not covered by tests


def round_trip_load_yaml(file_path):
with open(file_path, 'r') as f:
try:
payload = load(f)
except yaml.YAMLError as e:
print(e)
return None
return payload

Check warning on line 50 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L43-L50

Added lines #L43 - L50 were not covered by tests


def round_trip_dump(data, stream=None):
return yaml.round_trip_dump(data, stream)

Check warning on line 54 in piperider_cli/yaml/yaml_less_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_less_type/__init__.py#L53-L54

Added lines #L53 - L54 were not covered by tests
60 changes: 60 additions & 0 deletions piperider_cli/yaml/yaml_rich_type/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from pathlib import Path
from typing import Any, Callable, Optional, Union

from ruamel import yaml
from ruamel.yaml import StreamTextType, StreamType, VersionType
from ruamel.yaml import CommentedMap as _cm, CommentedSeq as _cs

_yaml = yaml.YAML()

CommentedMap = _cm
CommentedSeq = _cs
YAMLError = yaml.YAMLError


def load(stream: Union[Path, StreamTextType]) -> Any:
return _yaml.load(stream)


def allow_duplicate_keys_loader() -> Callable:
yml = yaml.YAML()
yml.allow_duplicate_keys = True
return yml.load


def safe_load(stream: StreamTextType, version: Optional[VersionType] = None) -> Any:
return yaml.safe_load(stream, version)


def dump(
data: Union[Path, StreamType], stream: Any = None, *, transform: Any = None
) -> Any:
return _yaml.dump(data, stream, transform=transform)


def safe_load_yaml(file_path):
try:
with open(file_path, 'r') as f:
payload = safe_load(f)
except yaml.YAMLError as e:
print(e)
return None

Check warning on line 41 in piperider_cli/yaml/yaml_rich_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_rich_type/__init__.py#L40-L41

Added lines #L40 - L41 were not covered by tests
except FileNotFoundError:
return None
return payload


def round_trip_load_yaml(file_path):
with open(file_path, 'r') as f:
try:
payload = load(f)
except yaml.YAMLError as e:
print(e)
return None
return payload

Check warning on line 54 in piperider_cli/yaml/yaml_rich_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_rich_type/__init__.py#L48-L54

Added lines #L48 - L54 were not covered by tests


def round_trip_dump(
data: Any,
stream: Optional[StreamType] = None):
return yaml.round_trip_dump(data, stream)

Check warning on line 60 in piperider_cli/yaml/yaml_rich_type/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/yaml/yaml_rich_type/__init__.py#L60

Added line #L60 was not covered by tests

0 comments on commit 35eee78

Please sign in to comment.