Coverage for sources/mimeogram/__/io.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-17 22:34 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Common I/O primitives. ''' 

22 

23 

24from . import imports as __ 

25from . import asyncf as _asyncf 

26 

27 

28async def acquire_text_file_async( 

29 file: str | __.Path, 

30 charset: str = 'utf-8', 

31 deserializer: __.Absential[ 

32 __.typx.Callable[ [ str ], __.typx.Any ] ] = __.absent, 

33) -> __.typx.Any: 

34 ''' Reads file asynchronously. ''' 

35 from aiofiles import open as open_ 

36 async with open_( file, encoding = charset ) as stream: 

37 data = await stream.read( ) 

38 if not __.is_absent( deserializer ): 

39 return deserializer( data ) 

40 return data 

41 

42 

43async def acquire_text_files_async( 

44 *files: str | __.Path, 

45 charset: str = 'utf-8', 

46 deserializer: __.Absential[ 

47 __.typx.Callable[ [ str ], __.typx.Any ] ] = __.absent, 

48 return_exceptions: bool = False 

49) -> __.typx.Sequence[ __.typx.Any ]: 

50 ''' Reads files in parallel asynchronously. ''' 

51 # TODO? Batch to prevent fd exhaustion over large file sets. 

52 return await _asyncf.gather_async( 

53 *( acquire_text_file_async( 

54 file, charset = charset, deserializer = deserializer ) 

55 for file in files ), 

56 error_message = 'Failure to read files.', 

57 return_exceptions = return_exceptions )