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

domdfcoding / domdf_python_tools / 14915795784

08 May 2025 08:45PM UTC coverage: 97.313%. Remained the same
14915795784

push

github

web-flow
Updated files with 'repo_helper'. (#134)

Co-authored-by: repo-helper[bot] <74742576+repo-helper[bot]@users.noreply.github.com>

2137 of 2196 relevant lines covered (97.31%)

0.97 hits per line

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

85.71
/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, 9):  # pragma: no cover (py39+)
1✔
9
        # 3rd party
10
        import importlib_resources
1✔
11

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

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

19
if not ((3, 9) <= sys.version_info < (3, 11)):  # pragma: no cover (py39 OR py310):
1✔
20

21
        def _normalize_path(path: Any) -> str:
1✔
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))
1✔
29
                if parent:
1✔
30
                        raise ValueError(f'{path!r} must be only a file name')
×
31
                return file_name
1✔
32

33
        def open_binary(package: "Package", resource: "Resource") -> BinaryIO:
1✔
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:
1✔
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(
1✔
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(
1✔
58
                                'r',
59
                                encoding=encoding,
60
                                errors=errors,
61
                                )
62

63
        def read_text(
1✔
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:
1✔
74
                        return fp.read()
1✔
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