Skip to content

Commit cb53d0e

Browse files
Write links in session file (#85)
* Add method to correctly write links in session file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Append the link names to the list * Add identity links in session.glu file to test reading/writing links --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent db88d0b commit cb53d0e

File tree

3 files changed

+128
-47
lines changed

3 files changed

+128
-47
lines changed

examples/session.glu

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,40 @@
5050
"log": "LoadLog",
5151
"log_item": 4
5252
},
53+
"ComponentLink": {
54+
"_type": "glue.core.component_link.ComponentLink",
55+
"frm": [
56+
"Right Ascension"
57+
],
58+
"inverse": {
59+
"_type": "types.FunctionType",
60+
"function": "glue.core.link_helpers.identity"
61+
},
62+
"to": [
63+
"RAJ2000"
64+
],
65+
"using": {
66+
"_type": "types.FunctionType",
67+
"function": "glue.core.link_helpers.identity"
68+
}
69+
},
70+
"ComponentLink_0": {
71+
"_type": "glue.core.component_link.ComponentLink",
72+
"frm": [
73+
"Declination"
74+
],
75+
"inverse": {
76+
"_type": "types.FunctionType",
77+
"function": "glue.core.link_helpers.identity"
78+
},
79+
"to": [
80+
"DEJ2000"
81+
],
82+
"using": {
83+
"_type": "types.FunctionType",
84+
"function": "glue.core.link_helpers.identity"
85+
}
86+
},
5387
"Component_0": {
5488
"_type": "glue.core.component.Component",
5589
"log": "LoadLog_0",
@@ -198,7 +232,10 @@
198232
"groups": [
199233
"Subset 1"
200234
],
201-
"links": [],
235+
"links": [
236+
"ComponentLink",
237+
"ComponentLink_0"
238+
],
202239
"subset_group_count": 1
203240
},
204241
"Declination": {

gluepyter/glue_ydoc.py

Lines changed: 88 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from copy import deepcopy
23
from typing import Dict, List, Any, Callable, Optional
34
from functools import partial
45
from jupyter_ydoc.ybasedoc import YBaseDoc
@@ -61,7 +62,6 @@ def get(self) -> str:
6162
dataset = json.loads(self._ydataset.to_json())
6263
links = json.loads(self._ylinks.to_json())
6364
tabs = json.loads(self._ytabs.to_json())
64-
6565
contents.setdefault("__main__", {})
6666

6767
tab_names = sorted(list(tabs.keys()))
@@ -91,8 +91,7 @@ def get(self) -> str:
9191
for data_name in data_names:
9292
contents[data_name] = dataset[data_name]
9393

94-
for link_name in link_names:
95-
contents[link_name] = links[link_name]
94+
self.add_links_to_contents(links, contents)
9695

9796
return json.dumps(contents, indent=2, sort_keys=True)
9897

@@ -127,47 +126,7 @@ def set(self, value: str) -> None:
127126
for attribute in contents.get(data_name, {}).get("primary_owner", []):
128127
attributes[attribute] = contents.get(attribute, {})
129128

130-
links: Dict[str, Dict] = {}
131-
for link_name in link_names:
132-
link: Dict = contents.get(link_name, {})
133-
uniform_link = {"_type": link.pop("_type")}
134-
if uniform_link["_type"] == COMPONENT_LINK_TYPE:
135-
uniform_link["data1"] = next(
136-
(
137-
k
138-
for k, v in dataset.items()
139-
if link["frm"][0] in v["primary_owner"]
140-
),
141-
None,
142-
)
143-
uniform_link["data2"] = next(
144-
(
145-
k
146-
for k, v in dataset.items()
147-
if link["to"][0] in v["primary_owner"]
148-
),
149-
None,
150-
)
151-
uniform_link["cids1"] = link.pop("frm")
152-
uniform_link["cids2"] = link.pop("to")
153-
for i in [1, 2]:
154-
uniform_link[f"cids{i}_labels"] = [
155-
attributes[attribute]["label"]
156-
for attribute in uniform_link[f"cids{i}"]
157-
]
158-
else:
159-
for i in [1, 2]:
160-
listName = link.pop(f"cids{i}")
161-
uniform_link[f"cids{i}"] = contents.get(listName, {}).get(
162-
"contents"
163-
)
164-
uniform_link[f"cids{i}_labels"] = [
165-
attributes[attribute]["label"]
166-
for attribute in uniform_link[f"cids{i}"]
167-
]
168-
169-
uniform_link.update(link)
170-
links[link_name] = uniform_link
129+
links = self.extract_links_from_file(link_names, contents, dataset, attributes)
171130

172131
with self._ydoc.begin_transaction() as t:
173132
self._ycontents.update(t, contents.items())
@@ -216,3 +175,88 @@ def remove_tab_viewer(self, tab_name: str, viewer_id: str) -> None:
216175
if tab is not None:
217176
with self._ydoc.begin_transaction() as t:
218177
tab.pop(t, viewer_id)
178+
179+
def extract_links_from_file(
180+
self,
181+
link_names: List[str],
182+
contents: Dict,
183+
dataset: Dict[str, Dict],
184+
attributes: Dict[str, Dict],
185+
) -> Dict[str, Dict]:
186+
links: Dict[str, Dict] = {}
187+
for link_name in link_names:
188+
link: Dict = deepcopy(contents.get(link_name, {}))
189+
uniform_link = {"_type": link.pop("_type")}
190+
if uniform_link["_type"] == COMPONENT_LINK_TYPE:
191+
uniform_link["data1"] = next(
192+
(
193+
k
194+
for k, v in dataset.items()
195+
if link["frm"][0] in v["primary_owner"]
196+
),
197+
None,
198+
)
199+
uniform_link["data2"] = next(
200+
(
201+
k
202+
for k, v in dataset.items()
203+
if link["to"][0] in v["primary_owner"]
204+
),
205+
None,
206+
)
207+
uniform_link["cids1"] = link.pop("frm")
208+
uniform_link["cids2"] = link.pop("to")
209+
for i in [1, 2]:
210+
uniform_link[f"cids{i}_labels"] = [
211+
attributes[attribute]["label"]
212+
for attribute in uniform_link[f"cids{i}"]
213+
]
214+
else:
215+
for i in [1, 2]:
216+
listName = link.pop(f"cids{i}")
217+
uniform_link[f"cids{i}"] = contents.get(listName, {}).get(
218+
"contents"
219+
)
220+
uniform_link[f"cids{i}_labels"] = [
221+
attributes[attribute]["label"]
222+
for attribute in uniform_link[f"cids{i}"]
223+
]
224+
225+
uniform_link.update(link)
226+
links[link_name] = uniform_link
227+
return links
228+
229+
def add_links_to_contents(self, links: Dict[str, Dict], contents: Dict):
230+
# Delete former links and attributes lists.
231+
for link_names in contents.get(self._data_collection_name, {}).get("links", []):
232+
link = contents.pop(link_names, {})
233+
234+
# Delete the list objects containing the attributes of advanced links.
235+
if link.get("_type", "") != COMPONENT_LINK_TYPE:
236+
contents.pop(link.get("cids1", None), None)
237+
contents.pop(link.get("cids2", None), None)
238+
contents[self._data_collection_name]["links"] = []
239+
240+
# Create the new links and attribute lists if necessary.
241+
lists_count = -1
242+
for link_name, link in links.items():
243+
if link["_type"] == COMPONENT_LINK_TYPE:
244+
link["frm"] = link.pop("cids1", [])
245+
link["to"] = link.pop("cids2", [])
246+
for i in [1, 2]:
247+
link.pop(f"cids{i}_labels", None)
248+
link.pop(f"data{i}", None)
249+
else:
250+
for i in [1, 2]:
251+
list_name = f"list{'' if lists_count < 0 else f'_{lists_count}'}"
252+
lists_count += 1
253+
link.pop(f"cids{i}_labels", None)
254+
link.pop(f"data{i}", None)
255+
attr_list = {
256+
"_type": "builtins.list",
257+
"contents": link.pop(f"cids{i}", []),
258+
}
259+
contents[list_name] = attr_list
260+
link[f"cids{i}"] = list_name
261+
contents[link_name] = link
262+
contents[self._data_collection_name]["links"].append(link_name)

gluepyter/tests/test_ydoc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def test_links(yglue_doc_links, identity_link):
5858
]
5959
types = [str, str, str, list, list, list, list]
6060

61-
# Links should have been populated according the session file, and all links should
62-
# have the same schema.
61+
# Links should have been populated according to the session file, and all links
62+
# should have the same schema.
6363
assert len(links) == 3
6464
for link in links.values():
6565
assert all(item in link.keys() for item in required)

0 commit comments

Comments
 (0)