Coverage for python / lsst / resources / _resourceHandles / _fileResourceHandle.py: 0%
54 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:32 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:32 +0000
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",)
15import logging
16from collections.abc import Iterable
17from io import SEEK_SET
18from typing import IO, TYPE_CHECKING, AnyStr, TypeVar
20from ._baseResourceHandle import BaseResourceHandle
22if TYPE_CHECKING:
23 from .._resourcePath import ResourcePath
25U = TypeVar("U", str, bytes)
28class FileResourceHandle(BaseResourceHandle[U]):
29 """File based specialization of `.BaseResourceHandle`.
31 Parameters
32 ----------
33 mode : `str`
34 Handle modes as described in the python `io` module.
35 log : `~logging.Logger`
36 Logger to used when writing messages.
37 uri : `lsst.resources.ResourcePath`
38 URI of the file on the filesystem to use.
39 encoding : `str` or None
40 Optionally supply the encoding of the file.
41 newline : `str`
42 When doing multiline operations, break the stream on given character.
43 Defaults to newline. If a file is opened in binary mode, this argument
44 is not used, as binary files will only split lines on the binary
45 newline representation.
47 Notes
48 -----
49 Documentation on the methods of this class line should refer to the
50 corresponding methods in the `io` module.
51 """
53 def __init__(
54 self, mode: str, log: logging.Logger, uri: ResourcePath, *, encoding: str | None, newline: str = "\n"
55 ):
56 super().__init__(mode, log, uri, newline=newline)
57 self._filename = uri.ospath
58 # opening a file in binary mode does not support a newline argument
59 newline_arg = None if "b" in mode else newline
60 self._fileHandle: IO = open(file=uri.ospath, mode=self._mode, newline=newline_arg, encoding=encoding)
62 @property
63 def name(self) -> str:
64 # More consistent to return the path without the file://.
65 return self._uri.ospath
67 @property
68 def mode(self) -> str:
69 return self._mode
71 def close(self) -> None:
72 self._fileHandle.close()
74 @property
75 def closed(self) -> bool:
76 return self._fileHandle.closed
78 def fileno(self) -> int:
79 return self._fileHandle.fileno()
81 def flush(self) -> None:
82 self._fileHandle.flush()
84 @property
85 def isatty(self) -> bool:
86 return self._fileHandle.isatty()
88 def readable(self) -> bool:
89 return self._fileHandle.readable()
91 def readline(self, size: int = -1) -> U:
92 return self._fileHandle.readline(size)
93 ...
95 def readlines(self, hint: int = -1) -> Iterable[U]:
96 return self._fileHandle.readlines(hint)
98 def seek(self, offset: int, whence: int = SEEK_SET) -> int:
99 return self._fileHandle.seek(offset, whence)
101 def seekable(self) -> bool:
102 return self._fileHandle.seekable()
104 def tell(self) -> int:
105 return self._fileHandle.tell()
107 def truncate(self, size: int | None = None) -> int:
108 return self._fileHandle.truncate(size)
110 def writable(self) -> bool:
111 return self._fileHandle.writable()
113 def writelines(self, lines: Iterable[AnyStr]) -> None:
114 self._fileHandle.writelines(lines)
116 def read(self, size: int = -1) -> U:
117 return self._fileHandle.read(size)
119 def write(self, b: U) -> int:
120 return self._fileHandle.write(b)