Skip to content

Commit d9ed642

Browse files
authored
Merge pull request #275 from opsmill/dga-20250219-fix-read-file
Move the function `read_file` from the ctl module to the SDK.
2 parents 81c7493 + 61adb22 commit d9ed642

File tree

8 files changed

+26
-29
lines changed

8 files changed

+26
-29
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move the function `read_file` from the ctl module to the SDK.

infrahub_sdk/ctl/_file.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

infrahub_sdk/ctl/exceptions.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,3 @@ class QueryNotFoundError(Error):
66
def __init__(self, name: str, message: str = ""):
77
self.message = message or f"The requested query '{name}' was not found."
88
super().__init__(self.message)
9-
10-
11-
class FileNotValidError(Error):
12-
def __init__(self, name: str, message: str = ""):
13-
self.message = message or f"Cannot parse '{name}' content."
14-
super().__init__(self.message)

infrahub_sdk/ctl/repository.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
from rich.console import Console
99
from rich.table import Table
1010

11-
from infrahub_sdk.ctl.client import initialize_client
12-
1311
from ..async_typer import AsyncTyper
14-
from ..ctl.exceptions import FileNotValidError
15-
from ..ctl.utils import init_logging
12+
from ..exceptions import FileNotValidError
1613
from ..graphql import Mutation, Query
1714
from ..schema.repository import InfrahubRepositoryConfig
18-
from ._file import read_file
15+
from ..utils import read_file
16+
from .client import initialize_client
1917
from .parameters import CONFIG_PARAM
18+
from .utils import init_logging
2019

2120
app = AsyncTyper()
2221
console = Console()

infrahub_sdk/ctl/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
from rich.logging import RichHandler
1818
from rich.markup import escape
1919

20-
from ..ctl.exceptions import FileNotValidError, QueryNotFoundError
2120
from ..exceptions import (
2221
AuthenticationError,
2322
Error,
23+
FileNotValidError,
2424
GraphQLError,
2525
NodeNotFoundError,
2626
ResourceNotDefinedError,
@@ -30,6 +30,7 @@
3030
)
3131
from ..yaml import YamlFile
3232
from .client import initialize_client_sync
33+
from .exceptions import QueryNotFoundError
3334

3435
if TYPE_CHECKING:
3536
from ..schema.repository import InfrahubRepositoryConfig

infrahub_sdk/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,9 @@ class UninitializedError(Error):
131131

132132
class InvalidResponseError(Error):
133133
"""Raised when an object requires an initialization step before use"""
134+
135+
136+
class FileNotValidError(Error):
137+
def __init__(self, name: str, message: str = ""):
138+
self.message = message or f"Cannot parse '{name}' content."
139+
super().__init__(self.message)

infrahub_sdk/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from infrahub_sdk.repository import GitRepoManager
1919

20-
from .exceptions import JsonDecodeError
20+
from .exceptions import FileNotValidError, JsonDecodeError
2121

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

344344

345+
def read_file(file_name: Path) -> str:
346+
if not file_name.is_file():
347+
raise FileNotValidError(name=str(file_name), message=f"{file_name} is not a valid file")
348+
try:
349+
with Path.open(file_name, encoding="utf-8") as fobj:
350+
return fobj.read()
351+
except UnicodeDecodeError as exc:
352+
raise FileNotValidError(name=str(file_name), message=f"Unable to read {file_name} with utf-8 encoding") from exc
353+
354+
345355
def get_user_permissions(data: list[dict]) -> dict:
346356
groups = {}
347357
for group in data:

infrahub_sdk/yaml.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
from pydantic import BaseModel, Field
99
from typing_extensions import Self
1010

11-
from .ctl._file import read_file
12-
from .ctl.exceptions import FileNotValidError
13-
from .utils import find_files
11+
from .exceptions import FileNotValidError
12+
from .utils import find_files, read_file
1413

1514

1615
class InfrahubFileApiVersion(str, Enum):

0 commit comments

Comments
 (0)