Skip to content

Commit

Permalink
Move the function read_file from the ctl module to the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarros committed Feb 19, 2025
1 parent 81c7493 commit 760260d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelog/+move-read-file.housekeeping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move the function `read_file` from the ctl module to the SDK.
13 changes: 0 additions & 13 deletions infrahub_sdk/ctl/_file.py

This file was deleted.

9 changes: 4 additions & 5 deletions infrahub_sdk/ctl/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
from rich.console import Console
from rich.table import Table

from infrahub_sdk.ctl.client import initialize_client

from ..async_typer import AsyncTyper
from ..ctl.exceptions import FileNotValidError
from ..ctl.utils import init_logging
from ..exceptions import FileNotValidError
from ..graphql import Mutation, Query
from ..schema.repository import InfrahubRepositoryConfig
from ._file import read_file
from ..utils import read_file
from .client import initialize_client
from .parameters import CONFIG_PARAM
from .utils import init_logging

app = AsyncTyper()
console = Console()
Expand Down
6 changes: 6 additions & 0 deletions infrahub_sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,9 @@ class UninitializedError(Error):

class InvalidResponseError(Error):
"""Raised when an object requires an initialization step before use"""


class FileNotValidError(Error):
def __init__(self, name: str, message: str = ""):

Check warning on line 137 in infrahub_sdk/exceptions.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/exceptions.py#L136-L137

Added lines #L136 - L137 were not covered by tests
self.message = message or f"Cannot parse '{name}' content."
super().__init__(self.message)
12 changes: 11 additions & 1 deletion infrahub_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from infrahub_sdk.repository import GitRepoManager

from .exceptions import JsonDecodeError
from .exceptions import FileNotValidError, JsonDecodeError

Check warning on line 20 in infrahub_sdk/utils.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/utils.py#L20

Added line #L20 was not covered by tests

if TYPE_CHECKING:
from graphql import GraphQLResolveInfo
Expand Down Expand Up @@ -342,6 +342,16 @@ def write_to_file(path: Path, value: Any) -> bool:
return written is not None


def read_file(file_name: Path) -> str:

Check warning on line 345 in infrahub_sdk/utils.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/utils.py#L345

Added line #L345 was not covered by tests
if not file_name.is_file():
raise FileNotValidError(name=str(file_name), message=f"{file_name} is not a valid file")
try:
with Path.open(file_name, encoding="utf-8") as fobj:
return fobj.read()
except UnicodeDecodeError as exc:
raise FileNotValidError(name=str(file_name), message=f"Unable to read {file_name} with utf-8 encoding") from exc


def get_user_permissions(data: list[dict]) -> dict:
groups = {}
for group in data:
Expand Down
5 changes: 2 additions & 3 deletions infrahub_sdk/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from pydantic import BaseModel, Field
from typing_extensions import Self

from .ctl._file import read_file
from .ctl.exceptions import FileNotValidError
from .utils import find_files
from .exceptions import FileNotValidError
from .utils import find_files, read_file


class InfrahubFileApiVersion(str, Enum):
Expand Down

0 comments on commit 760260d

Please sign in to comment.