Coverage for src/io_collection/load/load_text.py: 100%
13 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-09-25 19:09 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-09-25 19:09 +0000
1from pathlib import Path
3from io_collection.load.load_buffer import _load_buffer_from_s3
6def load_text(location: str, key: str) -> str:
7 """
8 Load key as string from specified location.
10 Method will load from the S3 bucket if the location begins with the
11 **s3://** protocol, otherwise it assumes the location is a local path.
13 Parameters
14 ----------
15 location
16 Object location (local path or S3 bucket).
17 key
18 Object key ending in valid text extension.
20 Returns
21 -------
22 :
23 Loaded text.
24 """
26 if location[:5] == "s3://":
27 return _load_text_from_s3(location[5:], key)
28 return _load_text_from_fs(location, key)
31def _load_text_from_fs(path: str, key: str) -> str:
32 """
33 Load key as dict or list from local file system.
35 Parameters
36 ----------
37 path
38 Local object path.
39 key
40 Object key ending in valid text extension.
42 Returns
43 -------
44 :
45 Loaded text.
46 """
48 full_path = Path(path) / key
49 with full_path.open("r") as fileobj:
50 return fileobj.read()
53def _load_text_from_s3(bucket: str, key: str) -> str:
54 """
55 Load key as dict or list from AWS S3 bucket.
57 Parameters
58 ----------
59 bucket
60 AWS S3 bucket name.
61 key
62 Object key ending in valid text extension.
64 Returns
65 -------
66 :
67 Loaded text.
68 """
70 buffer = _load_buffer_from_s3(bucket, key)
71 return buffer.getvalue().decode("utf-8")