Coverage for oarepo_c4gh / crypt4gh / crypt4gh.py: 100%
21 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-06 16:58 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-06 16:58 +0000
1"""This module implements a simple convenience wrapper Crypt4GH on top
2of actual Stream4GH implementation.
4"""
6from .stream.stream4gh import Stream4GH
7from .rawio import Crypt4GHRawIO
8from io import BufferedReader, TextIOWrapper
11class Crypt4GH(Stream4GH):
12 """This class provides the user-facing API for the Stream4GH
13 functionality, adding the `open()` method for io-like interface.
15 """
17 def open(self, mode: str = None, encoding: str = None) -> Crypt4GHRawIO:
18 """Creates a Crypt4GHRawIO wrapper around self and returns
19 appropriate text or binary reader based on the mode and
20 encoding arguments.
22 Specify 'r' for explicit read mode - it is on by default.
24 Speficy 't' for explicit text mode - it is on by default.
26 Specify 'b' for binary mode.
28 For text mode (which is on by default) use `encoding` to
29 specify the text encoding wanted. If `None` the
30 `locale.getencoding()` is used.
32 Parameters:
33 mode: can contain 'r', 't' and 'b' characters.
35 Returns:
36 BufferedReader or TextIOWrapper based on the mode.
38 """
39 mode_read = True
40 mode_text = True
41 if mode is not None:
42 for ch in mode:
43 if ch == "r":
44 mode_read = True
45 elif ch == "t":
46 mode_text = True
47 elif ch == "b":
48 mode_text = False
49 else:
50 raise OSError
51 raw = Crypt4GHRawIO(self)
52 buf = BufferedReader(raw)
53 if mode_text:
54 return TextIOWrapper(buf, encoding)
55 return buf