From d49b11c0d4aa2fbaf9cb7cf8bd267114963d497a Mon Sep 17 00:00:00 2001 From: Damien Garros Date: Thu, 20 Feb 2025 06:00:19 +0100 Subject: [PATCH] Move the function `read_file` from the ctl module to the SDK --- changelog/+move-read-file.housekeeping.md | 1 + infrahub_sdk/ctl/_file.py | 13 ------------- infrahub_sdk/ctl/exceptions.py | 6 ------ infrahub_sdk/ctl/repository.py | 9 ++++----- infrahub_sdk/ctl/utils.py | 3 ++- infrahub_sdk/exceptions.py | 6 ++++++ infrahub_sdk/utils.py | 12 +++++++++++- infrahub_sdk/yaml.py | 5 ++--- 8 files changed, 26 insertions(+), 29 deletions(-) create mode 100644 changelog/+move-read-file.housekeeping.md delete mode 100644 infrahub_sdk/ctl/_file.py diff --git a/changelog/+move-read-file.housekeeping.md b/changelog/+move-read-file.housekeeping.md new file mode 100644 index 0000000..e9d818c --- /dev/null +++ b/changelog/+move-read-file.housekeeping.md @@ -0,0 +1 @@ +Move the function `read_file` from the ctl module to the SDK. \ No newline at end of file diff --git a/infrahub_sdk/ctl/_file.py b/infrahub_sdk/ctl/_file.py deleted file mode 100644 index 96629d7..0000000 --- a/infrahub_sdk/ctl/_file.py +++ /dev/null @@ -1,13 +0,0 @@ -from pathlib import Path - -from .exceptions import FileNotValidError - - -def read_file(file_name: Path) -> str: - 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 diff --git a/infrahub_sdk/ctl/exceptions.py b/infrahub_sdk/ctl/exceptions.py index 28436d5..fc764f3 100644 --- a/infrahub_sdk/ctl/exceptions.py +++ b/infrahub_sdk/ctl/exceptions.py @@ -6,9 +6,3 @@ class QueryNotFoundError(Error): def __init__(self, name: str, message: str = ""): self.message = message or f"The requested query '{name}' was not found." super().__init__(self.message) - - -class FileNotValidError(Error): - def __init__(self, name: str, message: str = ""): - self.message = message or f"Cannot parse '{name}' content." - super().__init__(self.message) diff --git a/infrahub_sdk/ctl/repository.py b/infrahub_sdk/ctl/repository.py index 8f79394..d6332d7 100644 --- a/infrahub_sdk/ctl/repository.py +++ b/infrahub_sdk/ctl/repository.py @@ -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() diff --git a/infrahub_sdk/ctl/utils.py b/infrahub_sdk/ctl/utils.py index 5c0b069..e4474d8 100644 --- a/infrahub_sdk/ctl/utils.py +++ b/infrahub_sdk/ctl/utils.py @@ -17,10 +17,10 @@ from rich.logging import RichHandler from rich.markup import escape -from ..ctl.exceptions import FileNotValidError, QueryNotFoundError from ..exceptions import ( AuthenticationError, Error, + FileNotValidError, GraphQLError, NodeNotFoundError, ResourceNotDefinedError, @@ -30,6 +30,7 @@ ) from ..yaml import YamlFile from .client import initialize_client_sync +from .exceptions import QueryNotFoundError if TYPE_CHECKING: from ..schema.repository import InfrahubRepositoryConfig diff --git a/infrahub_sdk/exceptions.py b/infrahub_sdk/exceptions.py index f8eff9c..b312044 100644 --- a/infrahub_sdk/exceptions.py +++ b/infrahub_sdk/exceptions.py @@ -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 = ""): + self.message = message or f"Cannot parse '{name}' content." + super().__init__(self.message) diff --git a/infrahub_sdk/utils.py b/infrahub_sdk/utils.py index aa065e2..2627a06 100644 --- a/infrahub_sdk/utils.py +++ b/infrahub_sdk/utils.py @@ -17,7 +17,7 @@ from infrahub_sdk.repository import GitRepoManager -from .exceptions import JsonDecodeError +from .exceptions import FileNotValidError, JsonDecodeError if TYPE_CHECKING: from graphql import GraphQLResolveInfo @@ -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: + 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: diff --git a/infrahub_sdk/yaml.py b/infrahub_sdk/yaml.py index 5cfd5bc..69d973c 100644 --- a/infrahub_sdk/yaml.py +++ b/infrahub_sdk/yaml.py @@ -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):