Skip to content

Commit

Permalink
fixup! Refactor transformer method
Browse files Browse the repository at this point in the history
  • Loading branch information
nadove-ucsc committed Jan 28, 2025
1 parent 12bad49 commit 0456435
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
20 changes: 12 additions & 8 deletions src/azul/indexer/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ def entity_type(cls) -> EntityType:
"""
raise NotImplementedError

@abstractmethod
def _replicate(self, entity: EntityReference) -> tuple[str, JSON]:
def _replica_type(self, entity: EntityReference) -> str:
"""
A tuple consisting of:
1. The name of the type of replica emitted by this transformer for a
given entity. See :py:attr:`Replica.replica_type`.
The name of the type of replica emitted by this transformer for a given
entity. See :py:attr:`Replica.replica_type`.
"""
return entity.entity_type

2. The contents of the replica for that entity.
@abstractmethod
def _replica_contents(self, entity: EntityReference) -> JSON:
"""
The contents of the replica emitted by this transformer for a given
entity.
"""
raise NotImplementedError

Expand Down Expand Up @@ -136,7 +139,8 @@ def _replica(self,
root_hub: EntityID,
file_hub: EntityID | None,
) -> Replica:
replica_type, contents = self._replicate(entity)
replica_type = self._replica_type(entity)
contents = self._replica_contents(entity)
coordinates = ReplicaCoordinates(content_hash=json_hash(contents).hexdigest(),
entity=entity)
return Replica(coordinates=coordinates,
Expand Down
13 changes: 6 additions & 7 deletions src/azul/plugins/metadata/anvil/indexer/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ def _transform(self,
) -> Iterable[Contribution | Replica]:
raise NotImplementedError

def _replicate(self, entity: EntityReference) -> tuple[str, JSON]:
content = ChainMap(self.bundle.entities, self.bundle.orphans)[entity]
return entity.entity_type, content
def _replica_contents(self, entity: EntityReference) -> JSON:
return ChainMap(self.bundle.entities, self.bundle.orphans)[entity]

def _convert_entity_type(self, entity_type: str) -> str:
assert entity_type == 'bundle' or entity_type.startswith('anvil_'), entity_type
Expand Down Expand Up @@ -428,9 +427,6 @@ def _activity_polymorphic_types(self) -> AbstractSet[str]:
if table['name'].endswith('activity')
}

def _is_duos(self, dataset: JSON) -> bool:
return 'duos_id' in dataset

@classmethod
def inner_entity_id(cls, entity_type: EntityType, entity: JSON) -> EntityID:
return entity['document_id']
Expand Down Expand Up @@ -509,8 +505,11 @@ def _duos_types(cls) -> FieldTypes:
def _duos(self, dataset: EntityReference) -> MutableJSON:
return self._entity(dataset, self._duos_types())

def _is_duos(self, dataset: EntityReference) -> bool:
return 'duos_id' in self.bundle.entities[dataset]

def _dataset(self, dataset: EntityReference) -> MutableJSON:
if self._is_duos(self.bundle.entities[dataset]):
if self._is_duos(dataset):
return self._duos(dataset)
else:
return super()._dataset(dataset)
Expand Down
7 changes: 3 additions & 4 deletions src/azul/plugins/metadata/hca/indexer/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,12 @@ def aggregator(cls, entity_type: EntityType) -> EntityAggregator | None:
agg_cls = SimpleAggregator
return agg_cls(entity_type)

def _replicate(self, entity: EntityReference) -> tuple[str, JSON]:
def _replica_contents(self, entity: EntityReference) -> JSON:
if entity == self.api_bundle.ref:
content = self.bundle.links
return self.bundle.links
else:
api_entity = self.api_bundle.entities[UUID(entity.entity_id)]
content = api_entity.json
return entity.entity_type, content
return api_entity.json

def _find_ancestor_samples(self,
entity: api.LinkedEntity,
Expand Down

0 comments on commit 0456435

Please sign in to comment.