Skip to content

Commit

Permalink
Update schema to reflect prod.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgadling committed Oct 17, 2024
1 parent 041769b commit 0f6435f
Show file tree
Hide file tree
Showing 10 changed files with 2,799 additions and 2,275 deletions.
53 changes: 28 additions & 25 deletions client/python/cryoet_data_portal/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@ name = "cryoet_data_portal"
version = "3.1.0"
description = "API Client to facilitate the use of the CryoET Portal. For more information about the API and the project visit https://github.com/chanzuckerberg/cryoet-data-portal/"
authors = [
{ name = "Chan Zuckerberg Initiative", email = "[email protected]" }
{ name = "Chan Zuckerberg Initiative", email = "[email protected]" },
]
license = { text = "MIT" }
readme = "README.md"
requires-python = ">= 3.7"
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Intended Audience :: Information Technology",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS :: MacOS X",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Intended Audience :: Information Technology",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS :: MacOS X",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
]
dependencies= [
"requests",
"boto3",
"deepmerge",
"gql[requests]",
"tqdm",
dependencies = [
"requests",
"boto3",
"deepmerge",
"gql[requests]",
"tqdm",
"strcase",
]

[project.urls]
Expand All @@ -42,8 +43,12 @@ documentation = "https://chanzuckerberg.github.io/cryoet-data-portal/python-api.

[tool.setuptools.packages.find]
where = ["src"]
include = ["cryoet_data_portal*"] # package names should match these glob patterns (["*"] by default)
exclude = ["tests*"] # exclude packages matching these glob patterns (empty by default)
include = [
"cryoet_data_portal*",
] # package names should match these glob patterns (["*"] by default)
exclude = [
"tests*",
] # exclude packages matching these glob patterns (empty by default)

[tool.setuptools_scm]
root = "../../.."
Expand All @@ -55,6 +60,4 @@ warn_unreachable = true
strict = true

[tool.pytest.ini_options]
markers = [
"expensive: too expensive to run regularly or in CI",
]
markers = ["expensive: too expensive to run regularly or in CI"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,48 @@
from ._client import Client
from ._models import (
Alignment,
Annotation,
AnnotationAuthor,
AnnotationFile,
AnnotationMethodLink,
AnnotationShape,
Annotation,
Dataset,
DatasetAuthor,
DatasetFunding,
Dataset,
DepositionAuthor,
Deposition,
DepositionAuthor,
DepositionType,
Frame,
FrameAcquisitionFile,
GainFile,
PerSectionAlignmentParameters,
PerSectionParameters,
Run,
TiltSeries,
Tomogram,
TomogramAuthor,
TomogramVoxelSpacing,
Tomogram,
)

__version__ = "3.1.0"

__all__ = [
"Client",
"Alignment",
"Annotation",
"AnnotationAuthor",
"AnnotationFile",
"AnnotationMethodLink",
"AnnotationShape",
"Annotation",
"Dataset",
"DatasetAuthor",
"DatasetFunding",
"Dataset",
"DepositionAuthor",
"Deposition",
"DepositionAuthor",
"DepositionType",
"Frame",
"FrameAcquisitionFile",
"GainFile",
"PerSectionAlignmentParameters",
"PerSectionParameters",
"Run",
"TiltSeries",
"TomogramAuthor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from cryoet_data_portal._constants import USER_AGENT

DEFAULT_URL = "http://localhost:9009/graphql"
DEFAULT_URL = "https://graphql.cryoetdataportal.czscience.com/graphql"


class Client:
Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(self, url: Optional[str] = None):
self.ds = DSLSchema(self.client.schema)

def build_query(
self, cls, root_field: str, gql_class_name: str, query_filters=None
self, cls, root_field: str, gql_class_name: str, query_filters=None,
):
ds = self.ds
query_filters = {} if not query_filters else {"where": query_filters}
Expand All @@ -68,7 +68,7 @@ def find(self, cls, query_filters=None):
gql_type = cls._get_gql_type()
gql_root = cls._get_gql_root_field()
response = self.client.execute(
self.build_query(cls, gql_root, gql_type, query_filters)
self.build_query(cls, gql_root, gql_type, query_filters),
)
return [cls(self, **item) for item in response[gql_root]]

Expand Down
49 changes: 33 additions & 16 deletions client/python/cryoet_data_portal/src/cryoet_data_portal/_codegen.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""This module generates model code from the GraphQL schema."""

import logging
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Optional, Tuple, Union

import strcase
from gql import Client
from gql.transport.requests import RequestsHTTPTransport
from graphql import (
Expand All @@ -19,8 +19,8 @@
GraphQLSchema,
GraphQLType,
build_schema,
print_schema,
get_named_type,
print_schema,
)
from jinja2 import Environment, FileSystemLoader

Expand All @@ -35,17 +35,18 @@
"DateTime": ("DateField()", "date"),
"numeric": ("FloatField()", "float"),
"_numeric": ("StringField()", "str"),
"tomogram_type_enum": ("StringField()", "str"),
"tomogram_processing_enum": ("StringField()", "str"),
"tomogram_reconstruction_method_enum": ("StringField()", "str"),
"annotation_file_source_enum": ("StringField()", "str"),
"alignment_method_type_enum": ("StringField()", "str"),
"annotation_method_type_enum": ("StringField()", "str"),
"annotation_file_shape_type_enum": ("StringField()", "str"),
"annotation_method_link_type_enum": ("StringField()", "str"),
"deposition_types_enum": ("StringField()", "str"),
"sample_type_enum": ("StringField()", "str"),
"tiltseries_camer_acquire_mode_enum": ("StringField()", "str"),
"tiltseries_camera_acquire_mode_enum": ("StringField()", "str"),
"tiltseries_microscope_manufacturer_enum": ("StringField()", "str"),
"fiducial_alignment_status_enum": ("StringField()", "str"),
"tomogram_processing_enum": ("StringField()", "str"),
"tomogram_reconstruction_method_enum": ("StringField()", "str"),
"alignment_type_enum": ("StringField()", "str"),
}

Expand All @@ -56,6 +57,7 @@
"Annotation": "Annotation",
"AnnotationAuthor": "AnnotationAuthor",
"AnnotationFile": "AnnotationFile",
"AnnotationMethodLink": "AnnotationMethodLink",
"AnnotationShape": "AnnotationShape",
"Dataset": "Dataset",
"DatasetAuthor": "DatasetAuthor",
Expand All @@ -64,8 +66,9 @@
"DepositionAuthor": "DepositionAuthor",
"DepositionType": "DepositionType",
"Frame": "Frame",
"FrameAcquisitionFile": "FrameAcquisitionFile",
"GainFile": "GainFile",
"PerSectionAlignmentParameters": "PerSectionAlignmentParameters",
"PerSectionParameters": "PerSectionParameters",
"Run": "Run",
"Tiltseries": "TiltSeries",
"Tomogram": "Tomogram",
Expand Down Expand Up @@ -213,6 +216,8 @@ def _parse_field(
) -> Optional[FieldInfo]:
logging.debug("_parse_field: %s, %s", name, field)
field_type = _maybe_unwrap_non_null(field.type)
if isinstance(field_type, GraphQLList):
return _parse_gql_list_field(name, field.description, field_type)
if field_type.name.endswith("Connection"): # TODO can we clean this up?
return _parse_model_list_field(gql_type, name, field_type)
if isinstance(field_type, GraphQLObjectType) and (
Expand All @@ -224,6 +229,22 @@ def _parse_field(
return None


# TODO - we only support list[list[float]] fields right now,
# so this method is overly simplistic.
def _parse_gql_list_field(
name: str,
description: Optional[str],
field_type: GraphQLList,
) -> Optional[FieldInfo]:
logging.debug("_parse_gql_list_field: %s", field_type)
return FieldInfo(
name=name,
description=description,
annotation_type="List[List[float]]",
default_value="ListField()",
)


def _parse_scalar_field(
name: str,
description: Optional[str],
Expand All @@ -249,7 +270,7 @@ def _parse_model_field(
logging.debug("_parse_model_field: %s", field_type)
model = GQL_TO_MODEL_TYPE.get(field_type.name)
if model is not None:
model_field = _camel_to_snake_case(model)
model_field = strcase.to_snake(model)
model_name = _camel_to_space_case(model)
source_model = GQL_TO_MODEL_TYPE[gql_type.name]
source_model_name = _camel_to_space_case(source_model)
Expand All @@ -269,14 +290,14 @@ def _parse_model_list_field(
) -> Optional[FieldInfo]:
logging.debug("_parse_model_list_field: %s", field_type)
of_type = get_named_type(
get_named_type(field_type.fields["edges"].type).fields["node"].type
get_named_type(field_type.fields["edges"].type).fields["node"].type,
)
if not isinstance(of_type, GraphQLNamedType):
return None
of_model = GQL_TO_MODEL_TYPE.get(of_type.name)
if of_model is not None:
source_model = GQL_TO_MODEL_TYPE[gql_type.name]
source_field = _camel_to_snake_case(source_model)
source_field = strcase.to_snake(source_model)
source_model_name = _camel_to_space_case(source_model)
of_model_name = _space_case_to_plural(_camel_to_space_case(of_model))
return FieldInfo(
Expand All @@ -294,12 +315,8 @@ def _maybe_unwrap_non_null(field_type: GraphQLType) -> GraphQLType:
return field_type


def _camel_to_snake_case(name: str) -> str:
return re.sub("(?!^)([A-Z]+)", r"_\1", name).lower()


def _camel_to_space_case(name: str) -> str:
return _camel_to_snake_case(name).replace("_", " ")
return strcase.to_snake(name).replace("_", " ")


def _space_case_to_plural(name: str) -> str:
Expand All @@ -315,7 +332,7 @@ def _load_jinja_environment() -> Environment:
lstrip_blocks=True,
keep_trailing_newline=True,
)
env.filters["to_snake"] = _camel_to_snake_case
env.filters["to_snake"] = strcase.to_snake
return env


Expand Down
Loading

0 comments on commit 0f6435f

Please sign in to comment.