Skip to content

Commit 8b164af

Browse files
Consolidate usethis._io into usethis._file as new modules
Part way toward #1256
1 parent 95d1f5c commit 8b164af

24 files changed

Lines changed: 96 additions & 81 deletions

File tree

.importlinter

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ layers =
2020
_integrations
2121
_backend
2222
_file
23-
_io | _subprocess | _console | _python
23+
_subprocess | _console | _python
2424
_config
2525
_types | errors
2626
_pipeweld
@@ -119,7 +119,9 @@ containers =
119119
layers =
120120
pyproject_toml | setup_cfg
121121
ini | toml | yaml
122-
dir | merge
122+
manager
123+
print_ | dir | merge
124+
types_
123125
exhaustive = true
124126

125127
[importlinter:contract:ui_interface]

src/usethis/_file/ini/io_.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
UnexpectedINIIOError,
2121
UnexpectedINIOpenError,
2222
)
23-
from usethis._io import (
23+
from usethis._file.manager import (
2424
KeyValueFileManager,
2525
UnexpectedFileIOError,
2626
UnexpectedFileOpenError,
27-
print_keys,
2827
)
28+
from usethis._file.print_ import print_keys
2929

3030
if TYPE_CHECKING:
3131
from collections.abc import Iterable, Sequence
@@ -34,9 +34,7 @@
3434

3535
from typing_extensions import Self
3636

37-
from usethis._io import (
38-
Key,
39-
)
37+
from usethis._file.types_ import Key
4038

4139

4240
class INIFileManager(KeyValueFileManager, metaclass=ABCMeta):
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from __future__ import annotations
22

3-
import re
43
from abc import ABCMeta, abstractmethod
5-
from typing import TYPE_CHECKING, Any, Generic, TypeAlias, TypeVar
4+
from typing import TYPE_CHECKING, Any, Generic, TypeVar
65

7-
from typing_extensions import assert_never, override
6+
from typing_extensions import override
87

98
from usethis._config import usethis_config
109
from usethis.errors import UsethisError
@@ -17,6 +16,8 @@
1716

1817
from typing_extensions import Self
1918

19+
from usethis._file.types_ import Key
20+
2021

2122
DocumentT = TypeVar("DocumentT")
2223

@@ -29,7 +30,7 @@ class UnexpectedFileIOError(UsethisError, IOError):
2930
"""Raised when an unexpected attempt is made to read or write the pyproject.toml file."""
3031

3132

32-
class UsethisFileManager(Generic[DocumentT], metaclass=ABCMeta):
33+
class FileManager(Generic[DocumentT], metaclass=ABCMeta):
3334
"""Manages file access with deferred writes using a context manager.
3435
3536
This class implements the Command Pattern, encapsulating file operations. It defers
@@ -53,7 +54,7 @@ def __init__(self) -> None:
5354

5455
@override
5556
def __eq__(self, other: object) -> bool:
56-
if not isinstance(other, UsethisFileManager):
57+
if not isinstance(other, FileManager):
5758
return NotImplemented
5859

5960
return self.relative_path == other.relative_path
@@ -181,10 +182,7 @@ def unlock(self) -> None:
181182
self._content_by_path.pop(self.path, None)
182183

183184

184-
Key: TypeAlias = str | re.Pattern[str]
185-
186-
187-
class KeyValueFileManager(UsethisFileManager, Generic[DocumentT], metaclass=ABCMeta):
185+
class KeyValueFileManager(FileManager, Generic[DocumentT], metaclass=ABCMeta):
188186
"""A manager for files which store (at least some) values in key-value mappings."""
189187

190188
@abstractmethod
@@ -225,30 +223,3 @@ def remove_from_list(
225223
) -> None:
226224
"""Remove values from a list in the configuration file."""
227225
raise NotImplementedError
228-
229-
230-
def print_keys(keys: Sequence[Key]) -> str:
231-
r"""Convert a list of keys to a string.
232-
233-
Args:
234-
keys: A list of keys.
235-
236-
Returns:
237-
A string representation of the keys.
238-
239-
Examples:
240-
>>> print_keys(["tool", "ruff", "line-length"])
241-
'tool.ruff.line-length'
242-
>>> print_keys([re.compile(r"importlinter:contracts:.*")])
243-
'<REGEX("importlinter:contracts:.*")>'
244-
"""
245-
components: list[str] = []
246-
for key in keys:
247-
if isinstance(key, str):
248-
components.append(key)
249-
elif isinstance(key, re.Pattern):
250-
components.append(f'<REGEX("{key.pattern}")>')
251-
else:
252-
assert_never(key)
253-
254-
return ".".join(components)

src/usethis/_file/print_.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from __future__ import annotations
2+
3+
import re
4+
from typing import TYPE_CHECKING
5+
6+
from typing_extensions import assert_never
7+
8+
if TYPE_CHECKING:
9+
from collections.abc import Sequence
10+
11+
from usethis._file.types_ import Key
12+
13+
14+
def print_keys(keys: Sequence[Key]) -> str:
15+
r"""Convert a list of keys to a string.
16+
17+
Args:
18+
keys: A list of keys.
19+
20+
Returns:
21+
A string representation of the keys.
22+
23+
Examples:
24+
>>> print_keys(["tool", "ruff", "line-length"])
25+
'tool.ruff.line-length'
26+
>>> print_keys([re.compile(r"importlinter:contracts:.*")])
27+
'<REGEX("importlinter:contracts:.*")>'
28+
"""
29+
components: list[str] = []
30+
for key in keys:
31+
if isinstance(key, str):
32+
components.append(key)
33+
elif isinstance(key, re.Pattern):
34+
components.append(f'<REGEX("{key.pattern}")>')
35+
else:
36+
assert_never(key)
37+
38+
return ".".join(components)

src/usethis/_file/pyproject_toml/io_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from tomlkit import TOMLDocument
3131
from typing_extensions import Self
3232

33-
from usethis._io import Key
33+
from usethis._file.types_ import Key
3434

3535

3636
class PyprojectTOMLManager(TOMLFileManager):

src/usethis/_file/setup_cfg/io_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from configupdater import ConfigUpdater as INIDocument
3131
from typing_extensions import Self
3232

33-
from usethis._io import Key
33+
from usethis._file.types_ import Key
3434

3535

3636
class SetupCFGManager(INIFileManager):

src/usethis/_file/toml/io_.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
from tomlkit.exceptions import TOMLKitError
1414
from typing_extensions import assert_never, override
1515

16+
from usethis._file.manager import (
17+
KeyValueFileManager,
18+
UnexpectedFileIOError,
19+
UnexpectedFileOpenError,
20+
)
1621
from usethis._file.merge import _deep_merge
22+
from usethis._file.print_ import print_keys
1723
from usethis._file.toml.errors import (
1824
TOMLDecodeError,
1925
TOMLNotFoundError,
@@ -23,13 +29,7 @@
2329
UnexpectedTOMLIOError,
2430
UnexpectedTOMLOpenError,
2531
)
26-
from usethis._io import (
27-
Key,
28-
KeyValueFileManager,
29-
UnexpectedFileIOError,
30-
UnexpectedFileOpenError,
31-
print_keys,
32-
)
32+
from usethis._file.types_ import Key
3333

3434
if TYPE_CHECKING:
3535
from collections.abc import Collection, Sequence
@@ -40,7 +40,7 @@
4040
from tomlkit.items import Item
4141
from typing_extensions import Never, Self
4242

43-
from usethis._io import Key
43+
from usethis._file.types_ import Key
4444

4545

4646
class TOMLFileManager(KeyValueFileManager, metaclass=ABCMeta):

src/usethis/_file/types_.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import re
2+
from typing import TypeAlias
3+
4+
Key: TypeAlias = str | re.Pattern[str]

src/usethis/_file/yaml/io_.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
from typing_extensions import assert_never, override
1717

1818
from usethis._console import info_print
19+
from usethis._file.manager import (
20+
KeyValueFileManager,
21+
UnexpectedFileIOError,
22+
UnexpectedFileOpenError,
23+
)
1924
from usethis._file.merge import _deep_merge
25+
from usethis._file.print_ import print_keys
2026
from usethis._file.yaml.errors import (
2127
UnexpectedYAMLIOError,
2228
UnexpectedYAMLOpenError,
@@ -27,12 +33,6 @@
2733
YAMLValueMissingError,
2834
)
2935
from usethis._file.yaml.update import update_ruamel_yaml_map
30-
from usethis._io import (
31-
KeyValueFileManager,
32-
UnexpectedFileIOError,
33-
UnexpectedFileOpenError,
34-
print_keys,
35-
)
3636

3737
if TYPE_CHECKING:
3838
from collections.abc import Generator, Sequence
@@ -41,8 +41,8 @@
4141

4242
from typing_extensions import Self
4343

44+
from usethis._file.types_ import Key
4445
from usethis._file.yaml.typing_ import YAMLLiteral
45-
from usethis._io import Key
4646

4747

4848
class YAMLFileManager(KeyValueFileManager, metaclass=ABCMeta):

src/usethis/_tool/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
if TYPE_CHECKING:
4141
from collections.abc import Sequence
4242

43-
from usethis._io import Key, KeyValueFileManager
43+
from usethis._file.manager import KeyValueFileManager
44+
from usethis._file.types_ import Key
4445
from usethis._tool.config import ConfigItem
4546
from usethis._tool.rule import Rule
4647

0 commit comments

Comments
 (0)