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

52 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-09 02:03 -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 

12 

13__all__ = ("FileResourceHandle",) 

14 

15from io import SEEK_SET 

16from logging import Logger 

17from typing import IO, AnyStr, Iterable, Optional, TypeVar 

18 

19from ._baseResourceHandle import BaseResourceHandle 

20 

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

22 

23 

24class FileResourceHandle(BaseResourceHandle[U]): 

25 """File based specialization of `BaseResourceHandle` 

26 

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. 

42 

43 Notes 

44 ----- 

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

46 corresponding methods in the `io` module. 

47 """ 

48 

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) 

60 

61 @property 

62 def mode(self) -> str: 

63 return self._mode 

64 

65 def close(self) -> None: 

66 self._fileHandle.close() 

67 

68 @property 

69 def closed(self) -> bool: 

70 return self._fileHandle.closed 

71 

72 def fileno(self) -> int: 

73 return self._fileHandle.fileno() 

74 

75 def flush(self) -> None: 

76 self._fileHandle.close() 

77 

78 @property 

79 def isatty(self) -> bool: 

80 return self._fileHandle.isatty() 

81 

82 def readable(self) -> bool: 

83 return self._fileHandle.readable() 

84 

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

86 return self._fileHandle.readline(size) 

87 ... 

88 

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

90 return self._fileHandle.readlines(hint) 

91 

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

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

94 

95 def seekable(self) -> bool: 

96 return self._fileHandle.seekable() 

97 

98 def tell(self) -> int: 

99 return self._fileHandle.tell() 

100 

101 def truncate(self, size: Optional[int] = None) -> int: 

102 return self._fileHandle.truncate(size) 

103 

104 def writable(self) -> bool: 

105 return self._fileHandle.writable() 

106 

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

108 self._fileHandle.writelines(lines) 

109 

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

111 return self._fileHandle.read(size) 

112 

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

114 return self._fileHandle.write(b)