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 23, 2025
1 parent 4d55068 commit d49b11c
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 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.

6 changes: 0 additions & 6 deletions infrahub_sdk/ctl/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
3 changes: 2 additions & 1 deletion infrahub_sdk/ctl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
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 = ""):
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

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:
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 d49b11c

Please sign in to comment.