Coverage for python/lsst/resources/_resourceHandles/_fileResourceHandle.py: 81%

51 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-31 09:33 +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 

12 

13__all__ = ("FileResourceHandle",) 

14 

15from collections.abc import Iterable 

16from io import SEEK_SET 

17from logging import Logger 

18from typing import IO, AnyStr, TypeVar 

19 

20from ._baseResourceHandle import BaseResourceHandle 

21 

22U = TypeVar("U", str, bytes) 

23 

24 

25class FileResourceHandle(BaseResourceHandle[U]): 

26 """File based specialization of `.BaseResourceHandle` 

27 

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. 

43 

44 Notes 

45 ----- 

46 Documentation on the methods of this class line should refer to the 

47 corresponding methods in the `io` module. 

48 """ 

49 

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 newline_arg = None if "b" in mode else newline 

55 self._fileHandle: IO = open(file=filename, mode=self._mode, newline=newline_arg, encoding=encoding) 

56 

57 @property 

58 def mode(self) -> str: 

59 return self._mode 

60 

61 def close(self) -> None: 

62 self._fileHandle.close() 

63 

64 @property 

65 def closed(self) -> bool: 

66 return self._fileHandle.closed 

67 

68 def fileno(self) -> int: 

69 return self._fileHandle.fileno() 

70 

71 def flush(self) -> None: 

72 self._fileHandle.close() 

73 

74 @property 

75 def isatty(self) -> bool: 

76 return self._fileHandle.isatty() 

77 

78 def readable(self) -> bool: 

79 return self._fileHandle.readable() 

80 

81 def readline(self, size: int = -1) -> AnyStr: 

82 return self._fileHandle.readline(size) 

83 ... 

84 

85 def readlines(self, hint: int = -1) -> Iterable[AnyStr]: 

86 return self._fileHandle.readlines(hint) 

87 

88 def seek(self, offset: int, whence: int = SEEK_SET) -> int: 

89 return self._fileHandle.seek(offset, whence) 

90 

91 def seekable(self) -> bool: 

92 return self._fileHandle.seekable() 

93 

94 def tell(self) -> int: 

95 return self._fileHandle.tell() 

96 

97 def truncate(self, size: int | None = None) -> int: 

98 return self._fileHandle.truncate(size) 

99 

100 def writable(self) -> bool: 

101 return self._fileHandle.writable() 

102 

103 def writelines(self, lines: Iterable[AnyStr]) -> None: 

104 self._fileHandle.writelines(lines) 

105 

106 def read(self, size: int = -1) -> AnyStr: 

107 return self._fileHandle.read(size) 

108 

109 def write(self, b: U) -> int: 

110 return self._fileHandle.write(b)