humalab 0.0.3__py3-none-any.whl → 0.0.4__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 humalab might be problematic. Click here for more details.
- humalab/assets/__init__.py +4 -0
- humalab/assets/files/__init__.py +4 -0
- humalab/assets/files/resource_file.py +41 -0
- humalab/assets/files/urdf_file.py +65 -0
- humalab/assets/resource_manager.py +57 -0
- humalab/dists/categorical.py +3 -0
- humalab/humalab.py +68 -0
- {humalab-0.0.3.dist-info → humalab-0.0.4.dist-info}/METADATA +1 -1
- {humalab-0.0.3.dist-info → humalab-0.0.4.dist-info}/RECORD +13 -11
- humalab/assets/resource_file.py +0 -28
- humalab/assets/resource_handler.py +0 -175
- {humalab-0.0.3.dist-info → humalab-0.0.4.dist-info}/WHEEL +0 -0
- {humalab-0.0.3.dist-info → humalab-0.0.4.dist-info}/entry_points.txt +0 -0
- {humalab-0.0.3.dist-info → humalab-0.0.4.dist-info}/licenses/LICENSE +0 -0
- {humalab-0.0.3.dist-info → humalab-0.0.4.dist-info}/top_level.txt +0 -0
humalab/assets/__init__.py
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ResourceFile:
|
|
5
|
+
def __init__(self,
|
|
6
|
+
name: str,
|
|
7
|
+
version: int,
|
|
8
|
+
filename: str,
|
|
9
|
+
resource_type: str,
|
|
10
|
+
description: str | None = None,
|
|
11
|
+
created_at: datetime | None = None):
|
|
12
|
+
self._name = name
|
|
13
|
+
self._version = version
|
|
14
|
+
self._filename = filename
|
|
15
|
+
self._resource_type = resource_type
|
|
16
|
+
self._description = description
|
|
17
|
+
self._created_at = created_at
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def name(self) -> str:
|
|
21
|
+
return self._name
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def version(self) -> int:
|
|
25
|
+
return self._version
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def filename(self) -> str:
|
|
29
|
+
return self._filename
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def resource_type(self) -> str:
|
|
33
|
+
return self._resource_type
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def created_at(self) -> datetime | None:
|
|
37
|
+
return self._created_at
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def description(self) -> str | None:
|
|
41
|
+
return self._description
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
import os
|
|
3
|
+
import glob
|
|
4
|
+
from humalab.assets.files.resource_file import ResourceFile
|
|
5
|
+
from humalab.assets.archive import extract_archive
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class URDFFile(ResourceFile):
|
|
9
|
+
def __init__(self,
|
|
10
|
+
name: str,
|
|
11
|
+
version: int,
|
|
12
|
+
filename: str,
|
|
13
|
+
urdf_filename: str | None = None,
|
|
14
|
+
description: str | None = None,
|
|
15
|
+
created_at: datetime | None = None,):
|
|
16
|
+
super().__init__(name=name,
|
|
17
|
+
version=version,
|
|
18
|
+
description=description,
|
|
19
|
+
filename=filename,
|
|
20
|
+
resource_type="URDF",
|
|
21
|
+
created_at=created_at)
|
|
22
|
+
self._urdf_base_filename = urdf_filename
|
|
23
|
+
self._urdf_filename, self._root_path = self._extract()
|
|
24
|
+
self._urdf_filename = os.path.join(self._urdf_filename, self._urdf_filename)
|
|
25
|
+
|
|
26
|
+
def _extract(self):
|
|
27
|
+
working_path = os.path.dirname(self._filename)
|
|
28
|
+
if not os.path.exists(working_path):
|
|
29
|
+
_, ext = os.path.splitext(self._filename)
|
|
30
|
+
ext = ext.lstrip('.') # Remove leading dot
|
|
31
|
+
if ext.lower() != "urdf":
|
|
32
|
+
extract_archive(self._filename, working_path)
|
|
33
|
+
try:
|
|
34
|
+
os.remove(self._filename)
|
|
35
|
+
except Exception as e:
|
|
36
|
+
print(f"Error removing saved file {self._filename}: {e}")
|
|
37
|
+
local_filename = self.search_resource_file(self._urdf_base_filename, working_path)
|
|
38
|
+
if local_filename is None:
|
|
39
|
+
raise ValueError(f"Resource filename {self._urdf_base_filename} not found in {working_path}")
|
|
40
|
+
return local_filename, working_path
|
|
41
|
+
|
|
42
|
+
def search_resource_file(self, resource_filename: str | None, working_path: str) -> str | None:
|
|
43
|
+
found_filename = None
|
|
44
|
+
if resource_filename:
|
|
45
|
+
search_path = os.path.join(working_path, "**")
|
|
46
|
+
search_pattern = os.path.join(search_path, resource_filename)
|
|
47
|
+
files = glob.glob(search_pattern, recursive=True)
|
|
48
|
+
if len(files) > 0:
|
|
49
|
+
found_filename = files[0]
|
|
50
|
+
|
|
51
|
+
if found_filename is None:
|
|
52
|
+
ext = "urdf"
|
|
53
|
+
search_pattern = os.path.join(working_path, "**", f"*.{ext}")
|
|
54
|
+
files = glob.glob(search_pattern, recursive=True)
|
|
55
|
+
if len(files) > 0:
|
|
56
|
+
found_filename = files[0]
|
|
57
|
+
return found_filename
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def urdf_filename(self) -> str | None:
|
|
61
|
+
return self._urdf_filename
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def root_path(self) -> str:
|
|
65
|
+
return self._root_path
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from humalab.assets.files.resource_file import ResourceFile
|
|
2
|
+
from humalab.humalab_config import HumalabConfig
|
|
3
|
+
from humalab.humalab_api_client import HumaLabApiClient
|
|
4
|
+
from humalab.assets.files.urdf_file import URDFFile
|
|
5
|
+
import os
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ResourceManager:
|
|
10
|
+
def __init__(self,
|
|
11
|
+
api_key: str | None = None,
|
|
12
|
+
host: str | None = None,
|
|
13
|
+
timeout: float | None = None):
|
|
14
|
+
self._humalab_config = HumalabConfig()
|
|
15
|
+
self._base_url = host or self._humalab_config.base_url
|
|
16
|
+
self._api_key = api_key or self._humalab_config.api_key
|
|
17
|
+
self._timeout = timeout or self._humalab_config.timeout
|
|
18
|
+
|
|
19
|
+
self._api_client = HumaLabApiClient(base_url=self._base_url,
|
|
20
|
+
api_key=self._api_key,
|
|
21
|
+
timeout=self._timeout)
|
|
22
|
+
|
|
23
|
+
def _asset_dir(self, name: str, version: int) -> str:
|
|
24
|
+
return os.path.join(self._humalab_config.workspace_path, "assets", name, f"{version}")
|
|
25
|
+
|
|
26
|
+
def _create_asset_dir(self, name: str, version: int) -> bool:
|
|
27
|
+
asset_dir = self._asset_dir(name, version)
|
|
28
|
+
if not os.path.exists(asset_dir):
|
|
29
|
+
os.makedirs(asset_dir, exist_ok=True)
|
|
30
|
+
return True
|
|
31
|
+
return False
|
|
32
|
+
|
|
33
|
+
def download(self,
|
|
34
|
+
name: str,
|
|
35
|
+
version: int | None=None) -> Any:
|
|
36
|
+
resource = self._api_client.get_resource(name=name, version=version)
|
|
37
|
+
file_content = self._api_client.download_resource(name="lerobot")
|
|
38
|
+
filename = os.path.basename(resource['resource_url'])
|
|
39
|
+
filename = os.path.join(self._asset_dir(name, resource["version"]), filename)
|
|
40
|
+
if self._create_asset_dir(name, resource["version"]):
|
|
41
|
+
with open(filename, "wb") as f:
|
|
42
|
+
f.write(file_content)
|
|
43
|
+
|
|
44
|
+
if resource["resource_type"].lower() == "urdf":
|
|
45
|
+
return URDFFile(name=resource["name"],
|
|
46
|
+
version=resource["version"],
|
|
47
|
+
description=resource.get("description"),
|
|
48
|
+
filename=filename,
|
|
49
|
+
urdf_filename=resource.get("filename"),
|
|
50
|
+
created_at=resource.get("created_at"))
|
|
51
|
+
|
|
52
|
+
return ResourceFile(name=name,
|
|
53
|
+
version=resource["version"],
|
|
54
|
+
filename=filename,
|
|
55
|
+
resource_type=resource["resource_type"],
|
|
56
|
+
description=resource.get("description"),
|
|
57
|
+
created_at=resource.get("created_at"))
|
humalab/dists/categorical.py
CHANGED
|
@@ -20,6 +20,9 @@ class Categorical(Distribution):
|
|
|
20
20
|
super().__init__(generator=generator)
|
|
21
21
|
self._choices = choices
|
|
22
22
|
self._size = size
|
|
23
|
+
if weights is not None and not np.isclose(sum(weights), 1.0):
|
|
24
|
+
weight_sum = sum(weights)
|
|
25
|
+
weights = [w / weight_sum for w in weights]
|
|
23
26
|
self._weights = weights
|
|
24
27
|
|
|
25
28
|
def _sample(self) -> int | float | np.ndarray:
|
humalab/humalab.py
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
from contextlib import contextmanager
|
|
2
|
+
|
|
3
|
+
from humalab.assets.files.resource_file import ResourceFile
|
|
4
|
+
from humalab.assets.files.urdf_file import URDFFile
|
|
5
|
+
from humalab.assets.resource_manager import ResourceManager
|
|
2
6
|
from humalab.run import Run
|
|
3
7
|
from humalab.humalab_config import HumalabConfig
|
|
4
8
|
from humalab.humalab_api_client import HumaLabApiClient
|
|
@@ -131,6 +135,69 @@ if __name__ == "__main__":
|
|
|
131
135
|
print("Friction: ", scenario.scenario.cup.friction)
|
|
132
136
|
print("Num Objects: ", scenario.scenario.num_objects)
|
|
133
137
|
|
|
138
|
+
scenario_string = """
|
|
139
|
+
scenario:
|
|
140
|
+
cup:
|
|
141
|
+
position: "${uniform: [0.7, 0.7, 0.7],
|
|
142
|
+
[1.5, 1.3, 0.7], [2, 3]}"
|
|
143
|
+
orientation: "${uniform: 0.3, 0.7}"
|
|
144
|
+
asset: "${categorical: ['lerobot', 'apple2', 'apple3'], [0.1, 0.3, 0.5]}"
|
|
145
|
+
friction: "${gaussian: 0.3, 0.05}"
|
|
146
|
+
hello: 13
|
|
147
|
+
jkjk: test
|
|
148
|
+
num_objects: "${discrete: 5, 10}"
|
|
149
|
+
dfdjkjk: "hello"
|
|
150
|
+
"""
|
|
151
|
+
with init(entity="default",
|
|
152
|
+
project="test",
|
|
153
|
+
name="my first run",
|
|
154
|
+
description="testing the humalab sdk",
|
|
155
|
+
tags=["tag1", "tag2"],
|
|
156
|
+
scenario=scenario_string,
|
|
157
|
+
num_env=None) as run:
|
|
158
|
+
print(f"Run ID: {run.id}")
|
|
159
|
+
print(f"Run Name: {run.name}")
|
|
160
|
+
print(f"Run Description: {run.description}")
|
|
161
|
+
print(f"Run Tags: {run.tags}")
|
|
162
|
+
print(f"Run Scenario YAML:\n{run.scenario.yaml}")
|
|
163
|
+
|
|
164
|
+
scenario = run.scenario
|
|
165
|
+
# Simulate some operations
|
|
166
|
+
print("CUP position: ", scenario.scenario.cup.position)
|
|
167
|
+
print("CUP orientation: ", scenario.scenario.cup.orientation)
|
|
168
|
+
print("Asset: ", scenario.scenario.cup.asset)
|
|
169
|
+
print("Friction: ", scenario.scenario.cup.friction)
|
|
170
|
+
print("Num Objects: ", scenario.scenario.num_objects)
|
|
171
|
+
scenario.reset()
|
|
172
|
+
print("======================SCENARIO RESET==========================")
|
|
173
|
+
print("CUP position: ", scenario.scenario.cup.position)
|
|
174
|
+
print("CUP orientation: ", scenario.scenario.cup.orientation)
|
|
175
|
+
print("Asset: ", scenario.scenario.cup.asset)
|
|
176
|
+
print("Friction: ", scenario.scenario.cup.friction)
|
|
177
|
+
print("Num Objects: ", scenario.scenario.num_objects)
|
|
178
|
+
|
|
179
|
+
resource = ResourceManager()
|
|
180
|
+
urdf_file: URDFFile = resource.download(name="lerobot", version=1)
|
|
181
|
+
print("URDF File: ", urdf_file.filename)
|
|
182
|
+
print("URDF Description: ", urdf_file.description)
|
|
183
|
+
print("URDF Created At: ", urdf_file.created_at)
|
|
184
|
+
print("URDF Root Path: ", urdf_file._root_path)
|
|
185
|
+
print("URDF Root Path: ", urdf_file._urdf_filename)
|
|
186
|
+
|
|
187
|
+
urdf_file: URDFFile = resource.download(name="lerobot")
|
|
188
|
+
print("URDF File: ", urdf_file.filename)
|
|
189
|
+
print("URDF Description: ", urdf_file.description)
|
|
190
|
+
print("URDF Created At: ", urdf_file.created_at)
|
|
191
|
+
print("URDF Root Path: ", urdf_file._root_path)
|
|
192
|
+
print("URDF Root Path: ", urdf_file._urdf_filename)
|
|
193
|
+
|
|
194
|
+
atlas_file: ResourceFile = resource.download(name="atlas_description_test")
|
|
195
|
+
print("Atlas File: ", atlas_file.filename)
|
|
196
|
+
print("Atlas Description: ", atlas_file.description)
|
|
197
|
+
print("Atlas Created At: ", atlas_file.created_at)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
"""
|
|
134
201
|
humalab_config = HumalabConfig()
|
|
135
202
|
base_url = humalab_config.base_url
|
|
136
203
|
api_key = humalab_config.api_key
|
|
@@ -147,3 +214,4 @@ if __name__ == "__main__":
|
|
|
147
214
|
with open(filename, "wb") as f:
|
|
148
215
|
f.write(file_content)
|
|
149
216
|
print(f"Resource file downloaded: {filename}")
|
|
217
|
+
"""
|
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
humalab/__init__.py,sha256=LIL4eEPT_Boiond6pZegbAPvZwNX-ZWHHMVPkv3ehcU,140
|
|
2
2
|
humalab/constants.py,sha256=YSiUI7R7g0gkImD6ezufRlOMdIBS5wJ--dLy11Qb2qY,135
|
|
3
|
-
humalab/humalab.py,sha256=
|
|
3
|
+
humalab/humalab.py,sha256=gudSi9nWLY09jJk-JJhAr0d5Gzmru4Y7FA2ttO7HXSQ,8617
|
|
4
4
|
humalab/humalab_api_client.py,sha256=V1PSe1pVngfBUJgxWW-lJzrM1ElQH2ZIolHiDieXlec,8806
|
|
5
5
|
humalab/humalab_config.py,sha256=CdjPgZFGYmmeYq5qsL84ZN8tGdoRP2TjaqS3wojKejg,2698
|
|
6
6
|
humalab/humalab_test.py,sha256=X07HDp9JLllGMuJ2kYxs4WB1o1r1VErn30AAvmn4zuM,19005
|
|
7
7
|
humalab/run.py,sha256=QloVqoZAWlBL5eYVCOqdIRWsjaUoY9BVQvd1BvNEgmY,7841
|
|
8
8
|
humalab/scenario.py,sha256=Zc9ymN5VI-cSnXhX7KYQOM62FCCm8GeAYSyUiUBZl_8,8275
|
|
9
9
|
humalab/scenario_test.py,sha256=9AyDj_l_43bO_ovhzhIvADTh4bAkq8YaMnPQ_sRPTog,28604
|
|
10
|
-
humalab/assets/__init__.py,sha256=
|
|
10
|
+
humalab/assets/__init__.py,sha256=fTtwY08nfLxv7whDLGAMWRxwBJDrEZFb-1t19M4cxlc,138
|
|
11
11
|
humalab/assets/archive.py,sha256=PALZRnpNExDfXiURVNEj6xxWqcW4nd9UBZ7QIHa8mI8,3119
|
|
12
|
-
humalab/assets/
|
|
13
|
-
humalab/assets/
|
|
12
|
+
humalab/assets/resource_manager.py,sha256=GeVfjerdtvRL4njQO8a75bZaOf_h4KdFTwN8h103vsg,2605
|
|
13
|
+
humalab/assets/files/__init__.py,sha256=9XF-Zp2nldx060v8A3tMTcB5o5vJCUTFKF9rrIFZE_s,111
|
|
14
|
+
humalab/assets/files/resource_file.py,sha256=mvwmE13dQWUu4XGi4kL6hzsYadldt3fVkGubRZ1D-6w,1020
|
|
15
|
+
humalab/assets/files/urdf_file.py,sha256=t91OdI5vvpv9-ELLyjrE9mkgd1TT2y3pPVpUYSXKZA0,2647
|
|
14
16
|
humalab/dists/__init__.py,sha256=Q7zQbFGC_CnTgExMcnRtJdqJitqy1oBk4D6Qyvunlqc,387
|
|
15
17
|
humalab/dists/bernoulli.py,sha256=6JdeuEPdoL2hVwcP94anmTbIDk_TsSeUctooCzugQag,1500
|
|
16
|
-
humalab/dists/categorical.py,sha256=
|
|
18
|
+
humalab/dists/categorical.py,sha256=t0LwN8PKAXa2Qx4RjYRGbTs-6InT3s4Qbi0srr7iDZQ,2025
|
|
17
19
|
humalab/dists/discrete.py,sha256=Yk-JebgGyBgmTrtibmaF5sJLIsdgsCSUwa8arO8XUHQ,2167
|
|
18
20
|
humalab/dists/distribution.py,sha256=t0qTUjS_N0adiy_j2fdf-NHSlRs1pr0swpnszizs04I,1048
|
|
19
21
|
humalab/dists/gaussian.py,sha256=ueGm8CLTj8cVxfU4fi5cQKZVnPRI6dBy_PLzUMqn2-A,1838
|
|
@@ -24,9 +26,9 @@ humalab/metrics/__init__.py,sha256=e0PPkAMP5nW-kGfb67SjMMlgxK9Bkp7nQVD-JWoV-qw,2
|
|
|
24
26
|
humalab/metrics/dist_metric.py,sha256=C7IpdFolw-VpkTHv-HTAK63kafH-WUjRTLxbF7h4W7g,921
|
|
25
27
|
humalab/metrics/metric.py,sha256=W1mWQKEPVs9x257zzJvJQmAadRNnDqGAEU2BAq1skwM,4194
|
|
26
28
|
humalab/metrics/summary.py,sha256=CloYeVkYgZAiaeM2gS6V8_tABukTkxAFJt0mpfmpbLI,2255
|
|
27
|
-
humalab-0.0.
|
|
28
|
-
humalab-0.0.
|
|
29
|
-
humalab-0.0.
|
|
30
|
-
humalab-0.0.
|
|
31
|
-
humalab-0.0.
|
|
32
|
-
humalab-0.0.
|
|
29
|
+
humalab-0.0.4.dist-info/licenses/LICENSE,sha256=Gy0Nw_Z9pbrNSu-loW-PCDWJyrC8eWpIqqIGW-DFtp8,1064
|
|
30
|
+
humalab-0.0.4.dist-info/METADATA,sha256=TlQItL7q4I-T3NcRMuLKCHcef5Tg85fgMOlY_AEtjbw,1704
|
|
31
|
+
humalab-0.0.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
32
|
+
humalab-0.0.4.dist-info/entry_points.txt,sha256=aY-hS7Kg8y5kGgYA14YmtTz5odBrgJIZ2fQMXAbVW_U,49
|
|
33
|
+
humalab-0.0.4.dist-info/top_level.txt,sha256=hp7XXBDE40hi9T3Jx6mPFc6wJbAMzektD5VWXlSCW6o,8
|
|
34
|
+
humalab-0.0.4.dist-info/RECORD,,
|
humalab/assets/resource_file.py
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class ResourceFile:
|
|
4
|
-
def __init__(self,
|
|
5
|
-
name: str,
|
|
6
|
-
version: int,
|
|
7
|
-
resource_type: str,
|
|
8
|
-
filepath: str):
|
|
9
|
-
self._name = name
|
|
10
|
-
self._version = version
|
|
11
|
-
self._resource_type = resource_type
|
|
12
|
-
self._filepath = filepath
|
|
13
|
-
|
|
14
|
-
@property
|
|
15
|
-
def name(self) -> str:
|
|
16
|
-
return self._name
|
|
17
|
-
|
|
18
|
-
@property
|
|
19
|
-
def version(self) -> int:
|
|
20
|
-
return self._version
|
|
21
|
-
|
|
22
|
-
@property
|
|
23
|
-
def resource_type(self) -> str:
|
|
24
|
-
return self._resource_type
|
|
25
|
-
|
|
26
|
-
@property
|
|
27
|
-
def filepath(self) -> str:
|
|
28
|
-
return self._filepath
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import mongoengine as me
|
|
2
|
-
from minio import Minio
|
|
3
|
-
import os
|
|
4
|
-
import glob
|
|
5
|
-
import tempfile
|
|
6
|
-
import trimesh
|
|
7
|
-
from humalab_service.humalab_host import HumaLabHost
|
|
8
|
-
from humalab_service.services.stores.obj_store import ObjStore
|
|
9
|
-
from humalab_service.services.stores.namespace import ObjectType
|
|
10
|
-
from humalab_service.db.resource import ResourceDocument
|
|
11
|
-
from humalab.assets.archive import extract_archive
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
ASSET_TYPE_TO_EXTENSIONS = {
|
|
15
|
-
"urdf": {"urdf"},
|
|
16
|
-
"mjcf": {"xml"},
|
|
17
|
-
"mesh": trimesh.available_formats(),
|
|
18
|
-
"usd": {"usd"},
|
|
19
|
-
"controller": {"py"},
|
|
20
|
-
"global_controller": {"py"},
|
|
21
|
-
"terminator": {"py"},
|
|
22
|
-
"data": {},
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
class ResourceHandler:
|
|
26
|
-
def __init__(self, working_path: str=tempfile.gettempdir()):
|
|
27
|
-
self._minio_client = Minio(
|
|
28
|
-
HumaLabHost.get_minio_service_address(),
|
|
29
|
-
access_key="humalab",
|
|
30
|
-
secret_key="humalab123",
|
|
31
|
-
secure=False
|
|
32
|
-
)
|
|
33
|
-
self._obj_store = ObjStore(self._minio_client)
|
|
34
|
-
self.working_path_root = working_path
|
|
35
|
-
|
|
36
|
-
def search_resource_file(self, resource_filename: str | None, working_path: str, morph_type: str) -> str | None:
|
|
37
|
-
found_filename = None
|
|
38
|
-
if resource_filename:
|
|
39
|
-
search_path = os.path.join(working_path, "**")
|
|
40
|
-
search_pattern = os.path.join(search_path, resource_filename)
|
|
41
|
-
files = glob.glob(search_pattern, recursive=True)
|
|
42
|
-
if len(files) > 0:
|
|
43
|
-
found_filename = files[0]
|
|
44
|
-
|
|
45
|
-
if found_filename is None:
|
|
46
|
-
for ext in ASSET_TYPE_TO_EXTENSIONS[morph_type]:
|
|
47
|
-
search_pattern = os.path.join(working_path, "**", f"*.{ext}")
|
|
48
|
-
files = glob.glob(search_pattern, recursive=True)
|
|
49
|
-
if len(files) > 0:
|
|
50
|
-
found_filename = files[0]
|
|
51
|
-
break
|
|
52
|
-
return found_filename
|
|
53
|
-
|
|
54
|
-
def _save_file(self, filepath: str, data: bytes) -> str:
|
|
55
|
-
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
|
56
|
-
with open(filepath, "wb") as f:
|
|
57
|
-
f.write(data)
|
|
58
|
-
return filepath
|
|
59
|
-
|
|
60
|
-
def _save_and_extract(self, working_path: str, local_filepath: str, file_content: bytes, file_type: str):
|
|
61
|
-
os.makedirs(working_path, exist_ok=True)
|
|
62
|
-
saved_filepath = self._save_file(local_filepath, file_content)
|
|
63
|
-
_, ext = os.path.splitext(saved_filepath)
|
|
64
|
-
ext = ext.lstrip('.') # Remove leading dot
|
|
65
|
-
if ext not in ASSET_TYPE_TO_EXTENSIONS[file_type]:
|
|
66
|
-
extract_archive(saved_filepath, working_path)
|
|
67
|
-
try:
|
|
68
|
-
os.remove(saved_filepath)
|
|
69
|
-
except Exception as e:
|
|
70
|
-
print(f"Error removing saved file {saved_filepath}: {e}")
|
|
71
|
-
|
|
72
|
-
def save_file(self,
|
|
73
|
-
resource_name: str,
|
|
74
|
-
resource_version: int,
|
|
75
|
-
object_type: ObjectType,
|
|
76
|
-
file_content: bytes,
|
|
77
|
-
file_url: str,
|
|
78
|
-
resource_type: str,
|
|
79
|
-
filename: str | None = None) -> str:
|
|
80
|
-
working_path = self.get_working_path(object_type, resource_name, resource_version)
|
|
81
|
-
local_filepath = os.path.join(working_path, os.path.basename(file_url))
|
|
82
|
-
local_filename = None
|
|
83
|
-
if os.path.exists(os.path.dirname(working_path)):
|
|
84
|
-
# if directory exists, try to search for the file
|
|
85
|
-
local_filename = self.search_resource_file(filename, working_path, resource_type)
|
|
86
|
-
if local_filename is None:
|
|
87
|
-
# if not found, save the file and extract the archive,
|
|
88
|
-
# then search for the file again
|
|
89
|
-
self._save_and_extract(working_path, local_filepath, file_content, resource_type)
|
|
90
|
-
local_filename = self.search_resource_file(filename, working_path, resource_type)
|
|
91
|
-
if local_filename is None:
|
|
92
|
-
# if still not found, raise an error
|
|
93
|
-
raise ValueError(f"Resource filename {filename} not found in {working_path}")
|
|
94
|
-
return local_filename
|
|
95
|
-
|
|
96
|
-
def get_working_path(self, obj_type: ObjectType, uuid: str, version: int) -> str:
|
|
97
|
-
return os.path.join(self.working_path_root, "humalab", obj_type.value, uuid + "_" + str(version))
|
|
98
|
-
|
|
99
|
-
def query_and_download_resource(self, resource_name: str, resource_version: int | None = None) -> tuple[str, str]:
|
|
100
|
-
""" Query the resource from the database and download it from the object store.
|
|
101
|
-
Args:
|
|
102
|
-
resource_name (str): The name of the resource.
|
|
103
|
-
resource_version (int | None): The version of the resource. If None, the latest version will be used.
|
|
104
|
-
Returns:
|
|
105
|
-
tuple[str, str]: A tuple containing the local filename of the resource and the working directory
|
|
106
|
-
where the resource was downloaded and extracted.
|
|
107
|
-
"""
|
|
108
|
-
me.connect("humalab", host=f"mongodb://{HumaLabHost.get_mongodb_service_address()}")
|
|
109
|
-
if resource_version is None:
|
|
110
|
-
resource = ResourceDocument.objects(name=resource_name.strip(), latest=True).first()
|
|
111
|
-
else:
|
|
112
|
-
resource = ResourceDocument.objects(name=resource_name.strip(), version=resource_version).first()
|
|
113
|
-
if resource is None:
|
|
114
|
-
raise ValueError(f"Resource {resource_name}:{resource_version} not found")
|
|
115
|
-
return self.download_resource(resource.name, resource.version, resource.file_url, resource.resource_type.value, resource.filename)
|
|
116
|
-
|
|
117
|
-
def download_resource(self,
|
|
118
|
-
resource_name: str,
|
|
119
|
-
resource_version: int,
|
|
120
|
-
resource_url: str,
|
|
121
|
-
resource_type: str,
|
|
122
|
-
resource_filename: str | None = None) -> tuple[str, str]:
|
|
123
|
-
""" Download a resource from the object store.
|
|
124
|
-
Args:
|
|
125
|
-
resource_name (str): The name of the resource.
|
|
126
|
-
resource_version (int): The version of the resource.
|
|
127
|
-
resource_url (str): The URL of the resource in the object store.
|
|
128
|
-
resource_type (str): The type of the resource (e.g., "urdf", "mjcf", "mesh", etc.).
|
|
129
|
-
resource_filename (str | None): The specific filename to search for within the resource directory. If None, the first file found will be used.
|
|
130
|
-
Returns:
|
|
131
|
-
tuple[str, str]: A tuple containing the local filename of the resource and the working directory
|
|
132
|
-
where the resource was downloaded and extracted.
|
|
133
|
-
"""
|
|
134
|
-
return self._download_resource(resource_name,
|
|
135
|
-
resource_version,
|
|
136
|
-
ObjectType.RESOURCE,
|
|
137
|
-
resource_url,
|
|
138
|
-
resource_type,
|
|
139
|
-
resource_filename)
|
|
140
|
-
|
|
141
|
-
def _download_resource(self,
|
|
142
|
-
resource_name: str,
|
|
143
|
-
resource_version: int,
|
|
144
|
-
object_type: ObjectType,
|
|
145
|
-
resource_url: str,
|
|
146
|
-
resource_type: str,
|
|
147
|
-
resource_filename: str | None = None) -> tuple[str, str]:
|
|
148
|
-
"""
|
|
149
|
-
Download an asset from the object store and extract it if necessary.
|
|
150
|
-
|
|
151
|
-
Args:
|
|
152
|
-
asset_uuid (str): The UUID of the asset.
|
|
153
|
-
asset_version (int): The version of the asset.
|
|
154
|
-
asset_type (ObjectType): The type of the asset (e.g., URDF, MJCF, Mesh, etc.).
|
|
155
|
-
asset_url (str): The URL of the asset in the object store.
|
|
156
|
-
asset_file_type (str): The type of the asset file (e.g., "urdf", "mjcf", "mesh", etc.).
|
|
157
|
-
asset_filename (str | None): The specific filename to search for within the asset directory. If None, the first file found will be used.
|
|
158
|
-
|
|
159
|
-
Returns:
|
|
160
|
-
tuple[str, str]: A tuple containing the local filename of the asset and the working directory
|
|
161
|
-
where the asset was downloaded and extracted.
|
|
162
|
-
"""
|
|
163
|
-
working_path = self.get_working_path(object_type, resource_name, resource_version)
|
|
164
|
-
local_filepath = os.path.join(working_path, os.path.basename(resource_url))
|
|
165
|
-
if not os.path.exists(working_path):
|
|
166
|
-
os.makedirs(working_path, exist_ok=True)
|
|
167
|
-
self._obj_store.download_file(object_type, resource_url, local_filepath)
|
|
168
|
-
_, ext = os.path.splitext(resource_url)
|
|
169
|
-
ext = ext.lower().lstrip('.')
|
|
170
|
-
if ext not in ASSET_TYPE_TO_EXTENSIONS[resource_type]:
|
|
171
|
-
extract_archive(local_filepath, working_path)
|
|
172
|
-
local_filename = self.search_resource_file(resource_filename, working_path, resource_type)
|
|
173
|
-
if local_filename is None:
|
|
174
|
-
raise ValueError(f"Resource filename {resource_filename} not found in {working_path}")
|
|
175
|
-
return local_filename, working_path
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|