Skip to content

Commit ee33075

Browse files
committed
Make changes according to libtiledb
1 parent 9952ad7 commit ee33075

File tree

3 files changed

+76
-28
lines changed

3 files changed

+76
-28
lines changed

tiledb/libtiledb/profile.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void init_profile(py::module& m) {
2020
py::init<std::optional<std::string>, std::optional<std::string>>(),
2121
py::keep_alive<1, 2>())
2222

23+
.def(py::init<Profile>())
24+
2325
.def_property_readonly("_name", &tiledb::Profile::get_name)
2426

2527
.def_property_readonly("_homedir", &tiledb::Profile::get_homedir)
@@ -34,6 +36,14 @@ void init_profile(py::module& m) {
3436

3537
.def("_save", &tiledb::Profile::save)
3638

39+
.def_static(
40+
"_load",
41+
py::overload_cast<
42+
std::optional<std::string>,
43+
std::optional<std::string>>(&tiledb::Profile::load),
44+
py::arg("name") = std::nullopt,
45+
py::arg("homedir") = std::nullopt)
46+
3747
.def("_remove", &tiledb::Profile::remove)
3848

3949
.def("_dump", &tiledb::Profile::dump);

tiledb/profile.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __repr__(self):
3838
"""
3939
return self._dump()
4040

41-
def set_param(self, param: str, value: str):
41+
def __setitem__(self, param: str, value: str):
4242
"""Sets a parameter for the profile.
4343
4444
:param param: The parameter name.
@@ -47,8 +47,7 @@ def set_param(self, param: str, value: str):
4747
"""
4848
self._set_param(param, value)
4949

50-
# maybe provide square brackets overload?
51-
def get_param(self, param: str):
50+
def __getitem__(self, param: str):
5251
"""Gets a parameter for the profile.
5352
5453
:param param: The parameter name.
@@ -57,14 +56,31 @@ def get_param(self, param: str):
5756
return self._get_param(param)
5857

5958
def save(self):
60-
"""Saves the profile.
59+
"""Saves the profile to storage.
6160
6261
:raises tiledb.TileDBError:
6362
"""
6463
self._save()
6564

65+
@classmethod
66+
def load(cls, name: str = None, homedir: str = None) -> "Profile":
67+
"""Loads a profile from storage.
68+
69+
:param name: The name of the profile.
70+
:param homedir: The home directory of the profile.
71+
:return: The loaded profile.
72+
:rtype: tiledb.Profile
73+
:raises tiledb.TileDBError:
74+
"""
75+
# This is a workaround for the from_pybind11 method due to the fact
76+
# that this class does not inherit from CtxMixin, as is commonly done.
77+
lt_obj = lt.Profile._load(name, homedir)
78+
py_obj = cls.__new__(cls)
79+
lt.Profile.__init__(py_obj, lt_obj)
80+
return py_obj
81+
6682
def remove(self):
67-
"""Removes the profile.
83+
"""Removes the profile from storage.
6884
6985
:raises tiledb.TileDBError:
7086
"""

tiledb/tests/test_profile.py

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
from pathlib import Path
22

33
import pytest
44

@@ -25,7 +25,6 @@ def setup_method(self):
2525
self.profile4 = tiledb.Profile(
2626
"test_profile", self.path("profile4_dir")
2727
) # named profile with custom home directory
28-
self.vfs.create_dir(self.path("profile4_dir"))
2928

3029

3130
class ProfileTest(ProfileTestCase):
@@ -36,31 +35,54 @@ def test_profile_name(self):
3635
assert self.profile4.name == "test_profile"
3736

3837
def test_profile_homedir(self):
39-
assert self.profile3.homedir == self.path("profile3_dir")
40-
assert self.profile4.homedir == self.path("profile4_dir")
38+
assert Path(self.profile1.homedir) == Path.home()
39+
assert Path(self.profile2.homedir) == Path.home()
40+
assert Path(self.profile3.homedir) == Path(self.path("profile3_dir"))
41+
assert Path(self.profile4.homedir) == Path(self.path("profile4_dir"))
4142

4243
def test_profile_set_get_param(self):
43-
self.profile1.set_param("rest.username", "my_username")
44-
assert self.profile1.get_param("rest.username") == "my_username"
44+
self.profile1["rest.username"] = "my_username"
45+
assert self.profile1["rest.username"] == "my_username"
4546

46-
self.profile3.set_param("rest.server_address", "https://myaddress.com")
47-
assert self.profile3.get_param("rest.server_address") == "https://myaddress.com"
47+
self.profile3["rest.server_address"] = "https://myaddress.com"
48+
assert self.profile3["rest.server_address"] == "https://myaddress.com"
4849

4950
def test_profile_repr(self):
50-
assert 'rest.password": ""' in repr(self.profile1)
51-
# set a parameter and check the repr again
52-
self.profile1.set_param("rest.password", "test")
53-
assert 'rest.password": "test"' in repr(self.profile1)
54-
55-
def test_profile_save(self):
56-
assert os.path.exists(self.profile4.homedir)
57-
58-
tiledb_dir = os.path.join(self.profile4.homedir, ".tiledb")
59-
assert not os.path.exists(tiledb_dir)
60-
51+
self.profile1["rest.password"] = "testing_the_password"
52+
self.profile1["rest.payer_namespace"] = "testing_the_namespace"
53+
self.profile1["rest.server_address"] = "https://testing_the_address.com"
54+
self.profile1["rest.token"] = "testing_the_token"
55+
self.profile1["rest.username"] = "testing_the_username"
56+
57+
import json
58+
59+
goal_dict = {
60+
"default": {
61+
"rest.password": "testing_the_password",
62+
"rest.payer_namespace": "testing_the_namespace",
63+
"rest.server_address": "https://testing_the_address.com",
64+
"rest.token": "testing_the_token",
65+
"rest.username": "testing_the_username",
66+
}
67+
}
68+
69+
assert goal_dict == json.loads(repr(self.profile1))
70+
71+
def test_profile_set_save_load_get(self):
72+
self.profile4["rest.token"] = "testing_the_token_for_profile4"
73+
self.profile4["rest.payer_namespace"] = "testing_the_namespace_for_profile4"
74+
75+
# save the profile
6176
self.profile4.save()
6277

63-
# this fails ... for some reason
64-
assert os.path.exists(tiledb_dir)
65-
66-
# TODO: also test remove
78+
# load
79+
new_profile = tiledb.Profile.load("test_profile", self.path("profile4_dir"))
80+
assert new_profile.name == "test_profile"
81+
assert new_profile.homedir == self.path("profile4_dir")
82+
assert new_profile["rest.username"] == ""
83+
assert new_profile["rest.password"] == ""
84+
assert new_profile["rest.server_address"] == "https://api.tiledb.com"
85+
assert new_profile["rest.token"] == "testing_the_token_for_profile4"
86+
assert (
87+
new_profile["rest.payer_namespace"] == "testing_the_namespace_for_profile4"
88+
)

0 commit comments

Comments
 (0)