Skip to content

Commit

Permalink
read schema from resources
Browse files Browse the repository at this point in the history
  • Loading branch information
hkir-dev committed Sep 20, 2024
1 parent 0bb728b commit 0410ac0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/cell_annotation_schema/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import os
import json
import yaml
import warnings

from typing import Union
from urllib.request import urlopen
from pathlib import Path
from importlib import resources
from linkml_runtime.linkml_model import SchemaDefinition
from linkml_runtime.loaders import yaml_loader

from cell_annotation_schema import schemas


def is_web_url(path):
"""
Expand Down Expand Up @@ -49,8 +53,15 @@ def read_schema(schema: Union[str, dict]) -> SchemaDefinition:
"""
try:
if isinstance(schema, str) and str(schema).lower() in get_cas_schema_names().keys():
schema_dir = os.path.join(os.path.dirname(__file__), "../../build")
schema = os.path.join(schema_dir, get_cas_schema_names()[str(schema).lower()])
schema_name = get_cas_schema_names()[str(schema).lower()]
schema_file = resources.files(schemas) / schema_name
if os.path.exists(schema_file):
# read from resources (schemas) package
schema = yaml.safe_load(Path(schema_file).read_text())
else:
# read from build folder
schema_dir = os.path.join(os.path.dirname(__file__), "../../build")
schema = os.path.join(schema_dir, schema_name)
if isinstance(schema, Path):
schema = str(schema)
if isinstance(schema, dict):
Expand Down
4 changes: 4 additions & 0 deletions src/cell_annotation_schema/schemas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Schema files will be copied from the `build` folder and placed into this folder during pypi packaging.

Please see `pypi-publish.yaml` for details.

Empty file.

0 comments on commit 0410ac0

Please sign in to comment.