• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

domdfcoding / domdf_python_tools / 7166137878

11 Dec 2023 10:30AM UTC coverage: 97.456%. Remained the same
7166137878

push

github

domdfcoding
Bump version v3.7.0 -> v3.8.0

1 of 1 new or added line in 1 file covered. (100.0%)

2145 of 2201 relevant lines covered (97.46%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/domdf_python_tools/compat/importlib_resources.py
1
# noqa: D100,DALL000
2

3
# stdlib
4
import os
1✔
5
import sys
1✔
6
from typing import Any, BinaryIO, TextIO
1✔
7

8
if sys.version_info[:2] < (3, 7):  # pragma: no cover (py37+)
9
        # 3rd party
10
        import importlib_resources
11

12
        globals().update(importlib_resources.__dict__)
13

14
else:  # pragma: no cover (<py39)
15
        # stdlib
16
        import importlib.resources
17
        globals().update(importlib.resources.__dict__)
18

19
if not ((3, 7) <= sys.version_info < (3, 11)):  # pragma: no cover (py37 OR py38 OR py39 OR py310):
20

21
        def _normalize_path(path: Any) -> str:
22
                """
23
                Normalize a path by ensuring it is a string.
24

25
                If the resulting string contains path separators, an exception is raised.
26
                """
27

28
                parent, file_name = os.path.split(str(path))
29
                if parent:
30
                        raise ValueError(f'{path!r} must be only a file name')
31
                return file_name
32

33
        def open_binary(package: "Package", resource: "Resource") -> BinaryIO:
34
                """
35
                Return a file-like object opened for binary reading of the resource.
36
                """
37

38
                return (files(package) / _normalize_path(resource)).open("rb")
39

40
        def read_binary(package: "Package", resource: "Resource") -> bytes:
41
                """
42
                Return the binary contents of the resource.
43
                """
44

45
                return (files(package) / _normalize_path(resource)).read_bytes()
46

47
        def open_text(
48
                        package: "Package",
49
                        resource: "Resource",
50
                        encoding: str = "utf-8",
51
                        errors: str = "strict",
52
                        ) -> TextIO:
53
                """
54
                Return a file-like object opened for text reading of the resource.
55
                """
56

57
                return (files(package) / _normalize_path(resource)).open(
58
                                'r',
59
                                encoding=encoding,
60
                                errors=errors,
61
                                )
62

63
        def read_text(
64
                        package: "Package",
65
                        resource: "Resource",
66
                        encoding: str = "utf-8",
67
                        errors: str = "strict",
68
                        ) -> str:
69
                """
70
                Return the decoded string of the resource.
71
                """
72

73
                with open_text(package, resource, encoding, errors) as fp:
74
                        return fp.read()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc