Coverage for python/lsst/resources/_resourceHandles/_fileResourceHandle.py: 80%
52 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-19 03:38 -0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-19 03:38 -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 io import SEEK_SET
16from logging import Logger
17from typing import IO, AnyStr, Iterable, Optional, TypeVar
19from ._baseResourceHandle import BaseResourceHandle
21U = TypeVar("U", str, bytes)
24class FileResourceHandle(BaseResourceHandle[U]):
25 """File based specialization of `BaseResourceHandle`
27 Parameters
28 ----------
29 mode : `str`
30 Handle modes as described in the python `io` module
31 log : `~logging.Logger`
32 Logger to used when writing messages
33 filename : `str`
34 Name of the file on the filesystem to use.
35 encoding : `str` or None
36 Optionally supply the encoding of the file.
37 newline : `str`
38 When doing multiline operations, break the stream on given character.
39 Defaults to newline. If a file is opened in binary mode, this argument
40 is not used, as binary files will only split lines on the binary
41 newline representation.
43 Notes
44 -----
45 Documentation on the methods of this class line should refer to the
46 corresponding methods in the `io` module.
47 """
49 def __init__(
50 self, mode: str, log: Logger, *, filename: str, encoding: Optional[str], newline: str = "\n"
51 ):
52 super().__init__(mode, log, newline=newline)
53 self._filename = filename
54 # opening a file in binary mode does not support a newline argument
55 if "b" in mode:
56 newline_arg = None
57 else:
58 newline_arg = newline
59 self._fileHandle: IO = open(file=filename, mode=self._mode, newline=newline_arg, encoding=encoding)
61 @property
62 def mode(self) -> str:
63 return self._mode
65 def close(self) -> None:
66 self._fileHandle.close()
68 @property
69 def closed(self) -> bool:
70 return self._fileHandle.closed
72 def fileno(self) -> int:
73 return self._fileHandle.fileno()
75 def flush(self) -> None:
76 self._fileHandle.close()
78 @property
79 def isatty(self) -> bool:
80 return self._fileHandle.isatty()
82 def readable(self) -> bool:
83 return self._fileHandle.readable()
85 def readline(self, size: int = -1) -> AnyStr:
86 return self._fileHandle.readline(size)
87 ...
89 def readlines(self, hint: int = -1) -> Iterable[AnyStr]:
90 return self._fileHandle.readlines(hint)
92 def seek(self, offset: int, whence: int = SEEK_SET) -> int:
93 return self._fileHandle.seek(offset, whence)
95 def seekable(self) -> bool:
96 return self._fileHandle.seekable()
98 def tell(self) -> int:
99 return self._fileHandle.tell()
101 def truncate(self, size: Optional[int] = None) -> int:
102 return self._fileHandle.truncate(size)
104 def writable(self) -> bool:
105 return self._fileHandle.writable()
107 def writelines(self, lines: Iterable[AnyStr]) -> None:
108 self._fileHandle.writelines(lines)
110 def read(self, size: int = -1) -> AnyStr:
111 return self._fileHandle.read(size)
113 def write(self, b: U) -> int:
114 return self._fileHandle.write(b)