Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
`s:softwareVersion` (#90)
* Allow creation of EOAP-only and xcube-server-only images, omitting
unnecessary dependencies (#56)
* Add a utility function to read annotations from notebook code (#91)

## Changes in 0.1.2

Expand Down
13 changes: 13 additions & 0 deletions test/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,16 @@ def test_read_datasets_from_product_missing_items(
params.read_datasets_from_product(tmp_path, {})
for substring in "missing", "foo", "bar":
assert substring in str(error)


def test_read_annotations_from_code():
import textwrap

annotated_code = textwrap.dedent("""
my_int: int = 42
eoproduct: "EOInput" = None
""")
assert NotebookParameters.read_annotations(annotated_code) == {
"my_int": "int",
"eoproduct": "'EOInput'",
}
10 changes: 10 additions & 0 deletions xcengine/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,13 @@ def cwl_type(type_: type) -> str:
}[type_]
except KeyError:
raise ValueError(f"Unhandled type {type_}")

@staticmethod
def read_annotations(code: str) -> dict[str, str]:
import ast

return {
ast.unparse(node.target): ast.unparse(node.annotation)
for node in ast.walk(ast.parse(code))
if isinstance(node, ast.AnnAssign)
}
Loading