Skip to content

Commit 7894775

Browse files
committed
make clusters a public attribute
1 parent 574b3db commit 7894775

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

ipyparallel/cluster/cluster.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ class ClusterManager(LoggingConfigurable):
715715
Wraps Cluster, adding lookup/list by cluster id
716716
"""
717717

718-
_clusters = Dict(help="My cluster objects")
718+
clusters = Dict(help="My cluster objects")
719719

720720
@staticmethod
721721
def _cluster_key(cluster):
@@ -777,7 +777,7 @@ def load_clusters(
777777
# totally unspecified, default to all
778778
profile_dirs = _all_profile_dirs()
779779

780-
by_cluster_file = {c.cluster_file: c for c in self._clusters.values()}
780+
by_cluster_file = {c.cluster_file: c for c in self.clusters.values()}
781781
for profile_dir in profile_dirs:
782782
cluster_files = self._cluster_files_in_profile_dir(profile_dir)
783783
# load default cluster for each profile
@@ -787,8 +787,8 @@ def load_clusters(
787787

788788
cluster = Cluster(profile_dir=profile_dir, cluster_id="")
789789
cluster_key = self._cluster_key(cluster)
790-
if cluster_key not in self._clusters:
791-
self._clusters[cluster_key] = cluster
790+
if cluster_key not in self.clusters:
791+
self.clusters[cluster_key] = cluster
792792

793793
for cluster_file in cluster_files:
794794
if cluster_file in by_cluster_file:
@@ -802,31 +802,24 @@ def load_clusters(
802802
continue
803803
else:
804804
cluster_key = self._cluster_key(cluster)
805-
self._clusters[cluster_key] = cluster
805+
self.clusters[cluster_key] = cluster
806806

807-
return self._clusters
808-
809-
def list_clusters(self):
810-
"""List current clusters"""
811-
# TODO: what should we return?
812-
# just cluster ids or the full dict?
813-
# just cluster ids for now
814-
return self._clusters.items()
807+
return self.clusters
815808

816809
def new_cluster(self, **kwargs):
817810
"""Create a new cluster"""
818811
cluster = Cluster(parent=self, **kwargs)
819812
cluster_key = self._cluster_key(cluster)
820-
if cluster_key in self._clusters:
813+
if cluster_key in self.clusters:
821814
raise KeyError(f"Cluster {cluster_key} already exists!")
822-
self._clusters[cluster_key] = cluster
815+
self.clusters[cluster_key] = cluster
823816
return cluster_key, cluster
824817

825818
def get_cluster(self, cluster_id):
826819
"""Get a Cluster object by id"""
827-
return self._clusters[cluster_id]
820+
return self.clusters[cluster_id]
828821

829822
def remove_cluster(self, cluster_id):
830823
"""Delete a cluster by id"""
831824
# TODO: check running?
832-
del self._clusters[cluster_id]
825+
del self.clusters[cluster_id]

ipyparallel/nbextension/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def post(self):
104104
if (
105105
'profile' in body
106106
and 'cluster_id' not in body
107-
and f"{body['profile']}:" not in self.cluster_mananger._clusters
107+
and f"{body['profile']}:" not in self.cluster_mananger.clusters
108108
):
109109
# if no cluster exists for a profile,
110110
# default for no cluster id instead of random

ipyparallel/tests/test_cluster.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,18 @@ async def test_cluster_repr(Cluster):
216216

217217
async def test_cluster_manager():
218218
m = cluster.ClusterManager()
219-
assert m.list_clusters() == []
220-
c = m.new_cluster(profile_dir="/tmp")
219+
assert m.clusters == {}
220+
key, c = m.new_cluster(profile_dir="/tmp")
221221
assert c.profile_dir == "/tmp"
222-
assert m.get_cluster(c.cluster_id) is c
222+
assert m.get_cluster(key) is c
223223
with pytest.raises(KeyError):
224224
m.get_cluster("nosuchcluster")
225225

226226
with pytest.raises(KeyError):
227-
m.new_cluster(cluster_id=c.cluster_id)
227+
m.new_cluster(cluster_id=c.cluster_id, profile_dir=c.profile_dir)
228228

229-
assert m.list_clusters() == [c.cluster_id]
230-
m.remove_cluster(c.cluster_id)
231-
assert m.list_clusters() == []
229+
assert list(m.clusters) == [key]
230+
m.remove_cluster(key)
232231
with pytest.raises(KeyError):
233232
m.remove_cluster("nosuchcluster")
234233

0 commit comments

Comments
 (0)