pybioos 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pybioos might be problematic. Click here for more details.

@@ -0,0 +1,229 @@
1
+ import datetime
2
+ from typing import Iterable, List, Union
3
+
4
+ import pandas as pd
5
+ import tos
6
+ from cachetools import TTLCache, cached
7
+ from pandas import DataFrame
8
+ from tos.credential import FederationCredentials, FederationToken
9
+ from tos.models2 import ListedObject
10
+
11
+ from bioos.config import Config
12
+ from bioos.errors import ParameterError
13
+ from bioos.internal.tos import TOSHandler
14
+ from bioos.models.models import DisplayListedObject
15
+ from bioos.utils.common_tools import SingletonType, dict_str, s3_endpoint_mapping
16
+
17
+
18
+ class FileResource(metaclass=SingletonType):
19
+ TOS_RETRY_TIMES = 5
20
+ DEFAULT_PRE_SIGNED_TIME = 15 * 60
21
+
22
+ def __init__(self, workspace_id=str, bucket=str):
23
+ self.workspace_id = workspace_id
24
+ self.bucket = bucket
25
+ res = Config.service().get_tos_access({
26
+ 'WorkspaceID': self.workspace_id,
27
+ })
28
+ self.endpoint = s3_endpoint_mapping(res["Endpoint"])
29
+ self.region = res["Region"]
30
+ self.tos_handler = TOSHandler(
31
+ tos.TosClientV2(ak=None,
32
+ sk=None,
33
+ region=None,
34
+ endpoint=self.endpoint,
35
+ auth=tos.FederationAuth(
36
+ FederationCredentials(
37
+ self._refresh_federation_credentials),
38
+ self.region),
39
+ max_retry_count=self.TOS_RETRY_TIMES), self.bucket)
40
+
41
+ def __repr__(self) -> str:
42
+ info_dict = dict_str({
43
+ "name": self.bucket,
44
+ "endpoint": self.endpoint,
45
+ "region": self.region,
46
+ "size": f"{self.size} bytes",
47
+ "counts": self.counts,
48
+ })
49
+ return f"BucketInfo:\n{info_dict}"
50
+
51
+ def _refresh_federation_credentials(self) -> FederationToken:
52
+ res = Config.service().get_tos_access({
53
+ 'WorkspaceID': self.workspace_id,
54
+ })
55
+
56
+ tos_ak = res["AccessKey"]
57
+ tos_sk = res["SecretKey"]
58
+ sts_token = res["SessionToken"]
59
+
60
+ return FederationToken(
61
+ tos_ak, tos_sk, sts_token,
62
+ (datetime.datetime.fromisoformat(res["ExpiredTime"]) -
63
+ datetime.timedelta(minutes=5)).timestamp())
64
+
65
+ @property
66
+ @cached(cache=TTLCache(maxsize=10, ttl=1))
67
+ def size(self) -> int:
68
+ """Returns the size of all files.
69
+
70
+ :return: size of all files calculated with bytes
71
+ :rtype: int
72
+ """
73
+ size = 0
74
+ for o in self._list_with_cache():
75
+ size += o.size
76
+ return size
77
+
78
+ @property
79
+ @cached(cache=TTLCache(maxsize=10, ttl=1))
80
+ def counts(self) -> int:
81
+ """Returns the number of all files .
82
+
83
+ :return: number of all files
84
+ :rtype: int
85
+ """
86
+ return len(self._list_with_cache())
87
+
88
+ @cached(cache=TTLCache(maxsize=10, ttl=1))
89
+ def _list_with_cache(self) -> List[ListedObject]:
90
+ return self.tos_handler.list_objects("", 0)
91
+
92
+ def _build_s3_url(self, file_path) -> str:
93
+ return f"s3://{self.bucket}/{file_path}"
94
+
95
+ def _build_https_url(self, file_path) -> str:
96
+ return self.tos_handler.presign_download_url(
97
+ file_path, self.DEFAULT_PRE_SIGNED_TIME)
98
+
99
+ def s3_urls(self, sources: Union[str, Iterable[str]]) -> List[str]:
100
+ """Returns the S3 URLs for all the specified files .
101
+
102
+ *Example*:
103
+ ::
104
+
105
+ ws = bioos.workspace("foo")
106
+ ws.files.s3_urls(sources = ['bar/baz.txt']) #output: ["s3://xxxxxx-foo/bar/baz.txt"]
107
+
108
+ :param sources: The name of the file or a batch of files
109
+ :type sources: Union[str, Iterable[str]]
110
+ :return: S3 URLS of the file or a batch of file
111
+ :rtype: List[str]
112
+ """
113
+ if isinstance(sources, str):
114
+ sources = [sources]
115
+
116
+ if isinstance(sources, Iterable):
117
+ return [self._build_s3_url(elem) for elem in sources]
118
+ raise ParameterError("sources")
119
+
120
+ def https_urls(self, sources: Union[str, Iterable[str]]) -> List[str]:
121
+ """Returns the HTTPS URLs for all the specified files .
122
+
123
+ *Example*:
124
+ ::
125
+
126
+ ws = bioos.workspace("foo")
127
+ ws.files.https_urls(sources = ['bar/baz.txt']) #output: ["https://xxxxxx-foo.tos-s3-xxxxxx.volces.com/bar/baz.txt"]
128
+
129
+ :param sources: The name of the file or a batch of files
130
+ :type sources: Union[str, Iterable[str]]
131
+ :return: Pre-signed Downloading HTTPS URLS of the file or a batch of file
132
+ :rtype: List[str]
133
+ """
134
+ if isinstance(sources, str):
135
+ sources = [sources]
136
+
137
+ if isinstance(sources, Iterable):
138
+ return [self._build_https_url(elem) for elem in sources]
139
+ raise ParameterError("sources")
140
+
141
+ def list(self) -> DataFrame:
142
+ """Lists all files' information .
143
+
144
+ *Example*:
145
+ ::
146
+
147
+ ws = bioos.workspace("foo")
148
+ ws.files.list()
149
+
150
+ :return: files' information
151
+ :rtype: DataFrame
152
+ """
153
+ files = self.tos_handler.list_objects("", 0)
154
+ return pd.DataFrame.from_records([
155
+ DisplayListedObject(f, self._build_s3_url(f.key),
156
+ self._build_https_url(f.key)).__dict__
157
+ for f in files
158
+ ])
159
+
160
+ # TODO support s3 url input in the future
161
+ def download(self, sources: Union[str, Iterable[str]], target: str,
162
+ flatten: bool) -> bool:
163
+ """Downloads all the specified file from internal tos bucket bound to workspace to
164
+ local path.
165
+
166
+ *Example*:
167
+ ::
168
+
169
+ ws = bioos.workspace("foo")
170
+ ws.files.download(sources = ['foo/bar.txt'], target="baz/", flatten=False)
171
+
172
+ :param sources: The name of the file or a batch of files from internal tos bucket to download
173
+ :type sources: Union[str, Iterable[str]]
174
+ :param target: Local path
175
+ :type target: str
176
+ :param flatten: Whether to flatten the files locally
177
+ :type flatten: bool
178
+ :return: Downloading result
179
+ :rtype: bool
180
+ """
181
+
182
+ if isinstance(sources, str):
183
+ sources = [sources]
184
+
185
+ return len(self.tos_handler.download_objects(sources, target,
186
+ flatten)) == 0
187
+
188
+ def upload(self, sources: Union[str, Iterable[str]], target: str,
189
+ flatten: bool) -> bool:
190
+ """Uploads a local file or a batch of local files to internal tos bucket bound to workspace.
191
+
192
+ *Example*:
193
+ ::
194
+
195
+ ws = bioos.workspace("foo")
196
+ ws.files.upload(sources = ['foo/bar.txt'], target="baz/", flatten=False)
197
+
198
+ :param sources: The name of the local file or a batch of local files to upload
199
+ :type sources: Union[str, Iterable[str]]
200
+ :param target: The target internal path of the file or a batch of files
201
+ :type target: str
202
+ :param flatten: Whether to flatten the files at internal
203
+ :type flatten: bool
204
+ :return: Uploading result
205
+ :rtype: bool
206
+ """
207
+ if isinstance(sources, str):
208
+ sources = [sources]
209
+
210
+ return len(self.tos_handler.upload_objects(sources, target,
211
+ flatten)) == 0
212
+
213
+ def delete(self, sources: Union[str, Iterable[str]]) -> bool:
214
+ """Deletes the given file from the tos bucket bound to workspace .
215
+
216
+ *Example*:
217
+ ::
218
+
219
+ ws = bioos.workspace("foo")
220
+ ws.files.delete(sources = ['foo/bar.txt', "baz.csv"])
221
+
222
+ :param sources: The name of the file to delete
223
+ :type sources: Union[str, Iterable[str]]
224
+ :return: Deleting result
225
+ :rtype: bool
226
+ """
227
+ if isinstance(sources, str):
228
+ sources = [sources]
229
+ return len(self.tos_handler.delete_objects(sources)) == 0
@@ -0,0 +1,45 @@
1
+ from typing import Iterable, Union
2
+
3
+ from bioos.utils.common_tools import SingletonType
4
+
5
+
6
+ class UtilityResource(metaclass=SingletonType):
7
+ """
8
+ **[Deprecated] This resource will be implemented in the future**
9
+ Common tool collection
10
+ """
11
+
12
+ @staticmethod
13
+ def upload_wdl_image(source_image: str, target_image: str) -> bool:
14
+ """Uploads an executable WDL image .
15
+
16
+ *Example*:
17
+ ::
18
+
19
+ bioos.utility.upload_wdl_image(source_image="aaa/bbb:1.0.0", target_image:"AAA/BBB:miracle-1.0.0")
20
+
21
+ :param source_image: A local executable WDL image name
22
+ :type source_image: str
23
+ :param target_image: Target image name
24
+ :type target_image: str
25
+ :return: Result of uploading
26
+ :rtype: bool
27
+ """
28
+ pass
29
+
30
+ @staticmethod
31
+ def json_transfer(
32
+ json_: Union[str, Iterable[str]]) -> Union[str, Iterable[str]]:
33
+ """Convert the local JSON to meet the requirements of MiracleCloud
34
+
35
+ *Example*:
36
+ ::
37
+
38
+ bioos.utility.json_transfer(json='{\"aaa\":\"bbb\"}')
39
+
40
+ :param json_: JSON or JSON list to be converted
41
+ :type json_: Union[str, Iterable[str]]
42
+ :return: JSON or JSON list after converting
43
+ :rtype: Union[str, Iterable[str]]
44
+ """
45
+ pass