Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schematic): add generate excel manifest endpoint #2569

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions apps/schematic/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ target/
*secrets*
synapse_config.yaml
schematic_service_account_creds.json
private_localhost_certificate.crt
private_localhost.key

#schematic downloaded files
manifests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,60 @@
from schematic_api.controllers import manifest_generation_controller_impl


def generate_excel_manifest(
schema_url,
dataset_id,
add_annotations=None,
manifest_title=None,
data_type=None,
display_label_type=None,
use_strict_validation=None,
asset_view_id=None,
): # noqa: E501
"""Generates an excel file

Generates an excel file # noqa: E501

:param schema_url: The URL of a schema in jsonld or csv form
:type schema_url: str
:param dataset_id: The ID of a dataset.
:type dataset_id: str
:param add_annotations: If true, annotations are added to the manifest
:type add_annotations: bool
:param manifest_title: If making one manifest, the title of the manifest. If making multiple manifests, the prefix of the title of the manifests.
:type manifest_title: str
:param data_type: A data type
:type data_type: str
:param display_label_type: The type of label to display
:type display_label_type: str
:param use_strict_validation: If true, users are blocked from entering incorrect values. If false, users will get a warning when using incorrect values.
:type use_strict_validation: bool
:param asset_view_id: ID of view listing all project data assets. E.g. for Synapse this would be the Synapse ID of the fileview listing all data assets for a given project
:type asset_view_id: str

:rtype: Union[file, Tuple[file, int], Tuple[file, int, Dict[str, str]]
"""
return manifest_generation_controller_impl.generate_excel_manifest(
schema_url,
dataset_id,
add_annotations,
manifest_title,
data_type,
display_label_type,
use_strict_validation,
asset_view_id,
)


def generate_google_sheet_manifests(
schema_url,
add_annotations=None,
dataset_id_array=None,
manifest_title=None,
data_type_array=None,
display_label_type=None,
asset_view_id=None,
use_strict_validation=None,
asset_view_id=None,
generate_all_manifests=None,
): # noqa: E501
"""Generates a list of google sheet links
Expand All @@ -37,10 +82,10 @@ def generate_google_sheet_manifests(
:type data_type_array: List[str]
:param display_label_type: The type of label to display
:type display_label_type: str
:param asset_view_id: ID of view listing all project data assets. E.g. for Synapse this would be the Synapse ID of the fileview listing all data assets for a given project
:type asset_view_id: str
:param use_strict_validation: If true, users are blocked from entering incorrect values. If false, users will get a warning when using incorrect values.
:type use_strict_validation: bool
:param asset_view_id: ID of view listing all project data assets. E.g. for Synapse this would be the Synapse ID of the fileview listing all data assets for a given project
:type asset_view_id: str
:param generate_all_manifests: If true, a manifest for all components will be generated, datasetIds will be ignored. If false, manifests for each id in datasetIds will be generated.
:type generate_all_manifests: bool

Expand All @@ -53,7 +98,7 @@ def generate_google_sheet_manifests(
manifest_title,
data_type_array,
display_label_type,
asset_view_id,
use_strict_validation,
asset_view_id,
generate_all_manifests,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,71 @@
handle_exceptions,
get_access_token,
download_schema_file_as_jsonld,
InvalidValueError,
)


@handle_exceptions
def generate_excel_manifest(
schema_url: str,
dataset_id: str | None,
add_annotations: bool,
manifest_title: str,
data_type: str | None,
display_label_type: DisplayLabelType,
use_strict_validation: bool,
asset_view_id: str | None,
) -> tuple[str | BasicError, int]:
"""Generates a manifest in excel form

Args:
schema_url (str): The URL of the schema
dataset_id (str | None): Use this to get the existing manifest in the dataset.
Must be of same type as the data_type
add_annotations (bool): Whether or not annotatiosn get added to the manifest
manifest_title (str): Title of the manifest
data_type (str | None): The datatype of the manifest to generate
display_label_type (DisplayLabelType): The type of label to use as display
use_strict_validation (bool): Whether or not to use google sheet strict validation
asset_view_id (str): ID of the asset view

Returns:
tuple[str | BasicError], int]: A tuple
The first item is a path to an manifest in Excel form or an error object
The second item is the response status
"""
access_token = get_access_token()
if asset_view_id:
CONFIG.synapse_master_fileview_id = asset_view_id
schema_path = download_schema_file_as_jsonld(schema_url)
manifest_path_list = ManifestGenerator.create_manifests(
path_to_data_model=schema_path,
output_format="excel",
data_types=[data_type],
title=manifest_title,
access_token=access_token,
dataset_ids=[dataset_id],
strict=use_strict_validation,
use_annotations=add_annotations,
data_model_labels=display_label_type,
)
manifest = manifest_path_list[0]
assert isinstance(manifest, str)
result: str | BasicError = manifest
status = 200
return result, status


@handle_exceptions
def generate_google_sheet_manifests(
schema_url: str,
add_annotations: bool,
dataset_id_array: list[str] | None,
manifest_title: str | None,
data_type_array: list[str] | None,
display_label_type: DisplayLabelType = "class_label",
asset_view_id: str | None = None,
use_strict_validation: bool = True,
generate_all_manifests: bool = False,
display_label_type: DisplayLabelType,
use_strict_validation: bool,
asset_view_id: str | None,
generate_all_manifests: bool,
) -> tuple[GoogleSheetLinks | BasicError, int]:
"""Generates a list of links to manifests in google sheet form

Expand All @@ -43,57 +93,22 @@ def generate_google_sheet_manifests(
The type of label to use as display
Defaults to "class_label"

Raises:
ValueError: When generate_all_manifests is true and either dataset_id_array or
data_type_array are provided
ValueError: When generate_all_manifests is false and data_type_array is not provided
ValueError: When generate_all_manifests is false and dataset_id_array is provided,
but it doesn't match the length of data_type_array

Returns:
tuple[GoogleSheetLinks | BasicError, int]: A tuple
The first item is either the google sheet links of the manifests or an error object
The second item is the response status
"""

if generate_all_manifests:
if dataset_id_array:
raise InvalidValueError(
"When generate_all_manifests is True dataset_id_array must be None",
{"dataset_id_array": dataset_id_array},
)
if data_type_array:
raise InvalidValueError(
"When generate_all_manifests is True data_type_array must be None",
{"data_type_array": data_type_array},
)
data_type_array = ["all manifests"]

else:
if not data_type_array:
raise InvalidValueError(
(
"When generate_all_manifests is False data_type_array must be a list with "
"at least one item"
),
{"data_type_array": data_type_array},
)
if dataset_id_array and len(dataset_id_array) != len(data_type_array):
raise InvalidValueError(
(
"When generate_all_manifests is False data_type_array and dataset_id_array "
"must both lists with the same length"
),
{
"data_type_array": data_type_array,
"dataset_id_array": dataset_id_array,
},
)
if not data_type_array:
data_type_array = []

access_token = get_access_token()
if asset_view_id:
CONFIG.synapse_master_fileview_id = asset_view_id
schema_path = download_schema_file_as_jsonld(schema_url)

links = ManifestGenerator.create_manifests(
path_to_data_model=schema_path,
output_format="google_sheet",
Expand Down
Loading
Loading