Skip to content

Commit

Permalink
Add safety checks to translators for cases where jq returns None inst…
Browse files Browse the repository at this point in the history
…ead of expected empty list

Co-Authored-By: Fede Raimondo <f.raimondo@fz-juelich.de>
  • Loading branch information
jsheunis and fraimondo committed Jun 10, 2024
1 parent cb66310 commit bad2b5d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions datalad_catalog/translators/bids_dataset_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,20 @@ def get_authors(self):
'"email":"", "honorificSuffix":"", "identifiers":[]}]'
)
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None

return result if result is not None and len(result) > 0 else None
def get_keywords(self):
program = ". as $parent | .entities.task + .variables.dataset"
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_funding(self):
program = (
"[.Funding[]? as $fund | "
'{"name": "", "grant":"", "description":$fund}]'
)
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_publications(self):
program = (
Expand All @@ -170,12 +170,12 @@ def get_publications(self):
'"authors": []}]'
)
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_additional_display(self):
program = '[{"name": "BIDS", "content": .entities}]'
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_top_display(self):
program = (
Expand All @@ -185,7 +185,7 @@ def get_top_display(self):
'{"name": "Runs", "value": (.entities.run | length)}]'
)
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def translate(self):
translated_record = {
Expand Down
8 changes: 4 additions & 4 deletions datalad_catalog/translators/datacite_gin_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_license(self):
program = '.license | { "name": .name, "url": .url}'
result = jq.first(program, self.extracted_metadata)
# todo check for license info missing
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_authors(self):
program = (
Expand All @@ -130,7 +130,7 @@ def get_authors(self):
"else null end]"
)
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_keywords(self):
return self.extracted_metadata.get("keywords")
Expand All @@ -141,7 +141,7 @@ def get_funding(self):
'{"name": $element, "identifier": "", "description": ""}]'
)
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_publications(self):
program = (
Expand All @@ -155,7 +155,7 @@ def get_publications(self):
'"authors": []}]'
)
result = jq.first(program, self.extracted_metadata)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def translate(self):
translated_record = {
Expand Down
2 changes: 1 addition & 1 deletion datalad_catalog/translators/metalad_core_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_subdatasets(self):
'"dirs_from_path": []}]'
)
result = jq.first(program, self.graph)
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_file_url(self):
program = ".distribution? | .url?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def get_funding(self):
'{"name": .name, "identifier": "", "description": ""}]'
)
result = jq.first(program, self.graph) # [] if nothing found
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def get_publications(self):
if self.combinedpersonspubs is not None:
Expand All @@ -194,7 +194,7 @@ def get_subdatasets(self):
'"dataset_path": .name, "dirs_from_path": []}]'
)
result = jq.first(program, self.graph) # [] if nothing found
return result if len(result) > 0 else None
return result if result is not None and len(result) > 0 else None

def translate(self):
translated_record = {
Expand Down

0 comments on commit bad2b5d

Please sign in to comment.