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

1"""This module implements a simple convenience wrapper Crypt4GH on top 

2of actual Stream4GH implementation. 

3 

4""" 

5 

6from .stream.stream4gh import Stream4GH 

7from .rawio import Crypt4GHRawIO 

8from io import BufferedReader, TextIOWrapper 

9 

10 

11class Crypt4GH(Stream4GH): 

12 """This class provides the user-facing API for the Stream4GH 

13 functionality, adding the `open()` method for io-like interface. 

14 

15 """ 

16 

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. 

21 

22 Specify 'r' for explicit read mode - it is on by default. 

23 

24 Speficy 't' for explicit text mode - it is on by default. 

25 

26 Specify 'b' for binary mode. 

27 

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. 

31 

32 Parameters: 

33 mode: can contain 'r', 't' and 'b' characters. 

34 

35 Returns: 

36 BufferedReader or TextIOWrapper based on the mode. 

37 

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