Skip to content

Commit

Permalink
mabye this worked?
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Nov 4, 2024
1 parent 08e76ef commit b8f27fa
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions src/ga4gh/vrs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def is_ga4gh_identifiable():
def has_valid_ga4gh_id(self):
return self.id and GA4GH_IR_REGEXP.match(self.id) is not None

def compute_digest(self, store=True, as_version: PrevVrsVersion | None = None) -> str:
def compute_digest(self, store: bool = True, as_version: PrevVrsVersion | None = None) -> str:
"""A sha512t24u digest created using the VRS Computed Identifier algorithm.
Stores the digest in the object if ``store`` is ``True``.
Expand All @@ -276,7 +276,13 @@ def compute_digest(self, store=True, as_version: PrevVrsVersion | None = None) -
raise AttributeError('This class does not support prior version identifiers.')
return digest

def get_or_create_ga4gh_identifier(self, in_place='default', recompute=False, as_version=None) -> str:
def get_or_create_ga4gh_identifier(
self,
in_place: str = 'default',
recompute: bool = False,
as_version: PrevVrsVersion | None = None,
store_digest: bool = True,
) -> str:
"""Sets and returns a GA4GH Computed Identifier for the object.
Overwrites the existing identifier if overwrite is True.
Expand All @@ -291,47 +297,61 @@ def get_or_create_ga4gh_identifier(self, in_place='default', recompute=False, as
Digests will be recalculated even if present if recompute is True.
If ``as_version`` is provided, other parameters are ignored and an identifier is
returned following the conventions of the VRS version indicated by
``as_version_``.
:param in_place:
:param recompute:
:param as_version: If provided, other parameters are ignored and a computed
identifier is returned following the conventions of the given VRS version.
:param store_digest: if ``False``, don't set the object's ``digest`` field.
"""
if as_version is not None:
return self.compute_ga4gh_identifier(as_version=as_version)

if in_place == 'default':
if self.id is None:
self.id = self.compute_ga4gh_identifier(recompute)
self.id = self.compute_ga4gh_identifier(recompute, store_digest=store_digest)
elif in_place == 'always':
self.id = self.compute_ga4gh_identifier(recompute)
self.id = self.compute_ga4gh_identifier(recompute, store_digest=store_digest)
elif in_place == 'never':
return self.compute_ga4gh_identifier(recompute)
return self.compute_ga4gh_identifier(recompute, store_digest=store_digest)
else:
raise ValueError("Expected 'in_place' to be one of 'default', 'always', or 'never'")

if self.has_valid_ga4gh_id():
return self.id
else:
return self.compute_ga4gh_identifier(recompute)

def compute_ga4gh_identifier(self, recompute=False, as_version=None):
return self.compute_ga4gh_identifier(recompute, store_digest=store_digest)

def compute_ga4gh_identifier(
self,
recompute: bool = False,
as_version: PrevVrsVersion | None = None,
store_digest: bool = True,
) -> str:
"""Returns a GA4GH Computed Identifier.
If ``as_version`` is provided, other parameters are ignored and a computed
identifier is returned following the conventions of the VRS version indicated by
``as_version_``.
:param recompute:
:param as_version: If provided, other parameters are ignored and a computed
identifier is returned following the conventions of the given VRS version.
:param store_digest: if ``False``, don't set the object's ``digest`` field.
"""
if as_version is None:
self.get_or_create_digest(recompute)
self.get_or_create_digest(recompute, store=store_digest)
return f'{CURIE_NAMESPACE}{CURIE_SEP}{self.ga4gh.prefix}{GA4GH_PREFIX_SEP}{self.digest}'
else:
digest = self.compute_digest(as_version=as_version)
return f'{CURIE_NAMESPACE}{CURIE_SEP}{self.ga4gh.priorPrefix[as_version]}{GA4GH_PREFIX_SEP}{digest}'

def get_or_create_digest(self, recompute=False) -> str:
"""Sets and returns a sha512t24u digest of the GA4GH Identifiable Object, or creates
the digest if it does not exist."""
def get_or_create_digest(self, recompute: bool = False, store: bool = True) -> str:
"""Get a ``sha512t24u`` digest of the GA4GH Identifiable Object, or create it
if it doesn't exist
:param recompute: if ``True``, force regeneration of the digest regardless
of whether it's already set
:param store: if ``False``, don't set the object's ``digest`` field.
:return: computed digest
"""
if self.digest is None or recompute:
return self.compute_digest()
return self.compute_digest(store=store)
return self.digest

class ga4gh(_ValueObject.ga4gh):
Expand Down

0 comments on commit b8f27fa

Please sign in to comment.