forked from bluesky/tiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor link-writing into separate module.
- Loading branch information
1 parent
b305935
commit 0aadaca
Showing
6 changed files
with
164 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,56 @@ | ||
from .base import BaseClient | ||
import copy | ||
|
||
from .base import STRUCTURE_TYPES, BaseClient | ||
from .utils import client_for_item | ||
|
||
|
||
class UnionClient(BaseClient): | ||
def __repr__(self): | ||
return ( | ||
f"<{type(self).__name__} " | ||
f"[{', '.join(item.structure_family for item in self.structure().contents)}]>" | ||
f"<{type(self).__name__} {{" | ||
+ ", ".join(f"'{key}'" for key in self.structure().all_keys) | ||
+ "}>" | ||
) | ||
|
||
@property | ||
def contents(self): | ||
return UnionContents(self) | ||
|
||
def __getitem__(self, key): | ||
if key not in self.structure().all_keys: | ||
raise KeyError(key) | ||
raise NotImplementedError | ||
|
||
|
||
class UnionContents: | ||
def __init__(self, node): | ||
self.node = node | ||
|
||
def __repr__(self): | ||
return ( | ||
f"<{type(self).__name__} {{" | ||
+ ", ".join(f"'{item.name}'" for item in self.node.structure().contents) | ||
+ "}>" | ||
) | ||
|
||
def __getitem__(self, name): | ||
for index, union_item in enumerate(self.node.structure().contents): | ||
if union_item.name == name: | ||
structure_family = union_item.structure_family | ||
structure_dict = union_item.structure | ||
break | ||
else: | ||
raise KeyError(name) | ||
item = copy.deepcopy(self.node.item) | ||
item["attributes"]["structure_family"] = structure_family | ||
item["attributes"]["structure"] = structure_dict | ||
item["links"] = item["links"]["contents"][index] | ||
structure_type = STRUCTURE_TYPES[structure_family] | ||
structure = structure_type.from_json(structure_dict) | ||
return client_for_item( | ||
self.node.context, | ||
self.node.structure_clients, | ||
item, | ||
structure=structure, | ||
include_data_sources=self.node._include_data_sources, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
Generate the 'links' section of the response JSON. | ||
The links vary by structure family. | ||
""" | ||
from ..structures.core import StructureFamily | ||
|
||
|
||
def links_for_node(structure_family, structure, base_url, path, key): | ||
links = {} | ||
path_parts = [segment for segment in path.split("/") if segment] + [key] | ||
path_str = "/".join(path_parts) | ||
links = LINKS_BY_STRUCTURE_FAMILY[structure_family]( | ||
structure_family, structure, base_url, path_str, key | ||
) | ||
links["self"] = f"{base_url}/metadata/{path_str}" | ||
return links | ||
|
||
|
||
def links_for_array( | ||
structure_family, structure, base_url, path_str, key, data_source=None | ||
): | ||
links = {} | ||
block_template = ",".join(f"{{{index}}}" for index in range(len(structure.shape))) | ||
links["block"] = f"{base_url}/array/block/{path_str}?block={block_template}" | ||
links["full"] = f"{base_url}/array/full/{path_str}" | ||
if data_source: | ||
links["block"] += f"&data_source={data_source}" | ||
links["full"] += f"?data_source={data_source}" | ||
return links | ||
|
||
|
||
def links_for_awkward( | ||
structure_family, structure, base_url, path_str, key, data_source=None | ||
): | ||
links = {} | ||
links["buffers"] = f"{base_url}/awkward/buffers/{path_str}" | ||
links["full"] = f"{base_url}/awkward/full/{path_str}" | ||
if data_source: | ||
links["buffers"] += "?data_source={data_source}" | ||
links["full"] += "?data_source={data_source}" | ||
return links | ||
|
||
|
||
def links_for_container(structure_family, structure, base_url, path_str, key): | ||
# Cannot be used inside union, so there is no data_source parameter. | ||
links = {} | ||
links["full"] = f"{base_url}/container/full/{path_str}" | ||
links["search"] = f"{base_url}/search/{path_str}" | ||
return links | ||
|
||
|
||
def links_for_table( | ||
structure_family, structure, base_url, path_str, key, data_source=None | ||
): | ||
links = {} | ||
links["partition"] = f"{base_url}/table/partition/{path_str}?partition={{index}}" | ||
links["full"] = f"{base_url}/table/full/{path_str}" | ||
if data_source: | ||
links["partition"] += f"&data_source={data_source}" | ||
links["full"] += f"?data_source={data_source}" | ||
return links | ||
|
||
|
||
def links_for_union(structure_family, structure, base_url, path_str, key): | ||
links = {} | ||
# This contains the links for each structure. | ||
links["contents"] = [] | ||
for item in structure.contents: | ||
item_links = LINKS_BY_STRUCTURE_FAMILY[item.structure_family]( | ||
item.structure_family, | ||
item.structure, | ||
base_url, | ||
path_str, | ||
key, | ||
data_source=item.name, | ||
) | ||
item_links["self"] = f"{base_url}/metadata/{path_str}" | ||
links["contents"].append(item_links) | ||
return links | ||
|
||
|
||
LINKS_BY_STRUCTURE_FAMILY = { | ||
StructureFamily.array: links_for_array, | ||
StructureFamily.awkward: links_for_awkward, | ||
StructureFamily.container: links_for_container, | ||
StructureFamily.sparse: links_for_array, # spare and array are the same | ||
StructureFamily.table: links_for_table, | ||
StructureFamily.union: links_for_union, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters