Coverage for python/lsst/resources/_resourceHandles/_fileResourceHandle.py: 80%
53 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:52 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:52 -0700
1# This file is part of lsst-resources.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
11from __future__ import annotations
13__all__ = ("FileResourceHandle",)
15from collections.abc import Iterable
16from io import SEEK_SET
17from logging import Logger
18from typing import IO, AnyStr, TypeVar
20from ._baseResourceHandle import BaseResourceHandle
22U = TypeVar("U", str, bytes)
25class FileResourceHandle(BaseResourceHandle[U]):
26 """File based specialization of `.BaseResourceHandle`
28 Parameters
29 ----------
30 mode : `str`
31 Handle modes as described in the python `io` module
32 log : `~logging.Logger`
33 Logger to used when writing messages
34 filename : `str`
35 Name of the file on the filesystem to use.
36 encoding : `str` or None
37 Optionally supply the encoding of the file.
38 newline : `str`
39 When doing multiline operations, break the stream on given character.
40 Defaults to newline. If a file is opened in binary mode, this argument
41 is not used, as binary files will only split lines on the binary
42 newline representation.
44 Notes
45 -----
46 Documentation on the methods of this class line should refer to the
47 corresponding methods in the `io` module.
48 """
50 def __init__(self, mode: str, log: Logger, *, filename: str, encoding: str | None, newline: str = "\n"):
51 super().__init__(mode, log, newline=newline)
52 self._filename = filename
53 # opening a file in binary mode does not support a newline argument
54 if "b" in mode:
55 newline_arg = None
56 else:
57 newline_arg = newline
58 self._fileHandle: IO = open(file=filename, mode=self._mode, newline=newline_arg, encoding=encoding)
60 @property
61 def mode(self) -> str:
62 return self._mode
64 def close(self) -> None:
65 self._fileHandle.close()
67 @property
68 def closed(self) -> bool:
69 return self._fileHandle.closed
71 def fileno(self) -> int:
72 return self._fileHandle.fileno()
74 def flush(self) -> None:
75 self._fileHandle.close()
77 @property
78 def isatty(self) -> bool:
79 return self._fileHandle.isatty()
81 def readable(self) -> bool:
82 return self._fileHandle.readable()
84 def readline(self, size: int = -1) -> AnyStr:
85 return self._fileHandle.readline(size)
86 ...
88 def readlines(self, hint: int = -1) -> Iterable[AnyStr]:
89 return self._fileHandle.readlines(hint)
91 def seek(self, offset: int, whence: int = SEEK_SET) -> int:
92 return self._fileHandle.seek(offset, whence)
94 def seekable(self) -> bool:
95 return self._fileHandle.seekable()
97 def tell(self) -> int:
98 return self._fileHandle.tell()
100 def truncate(self, size: int | None = None) -> int:
101 return self._fileHandle.truncate(size)
103 def writable(self) -> bool:
104 return self._fileHandle.writable()
106 def writelines(self, lines: Iterable[AnyStr]) -> None:
107 self._fileHandle.writelines(lines)
109 def read(self, size: int = -1) -> AnyStr:
110 return self._fileHandle.read(size)
112 def write(self, b: U) -> int:
113 return self._fileHandle.write(b)