Skip to content

Commit

Permalink
[feature] added local json reference option to amdirt validate
Browse files Browse the repository at this point in the history
  • Loading branch information
djyamunaq committed Feb 7, 2025
1 parent 3808231 commit 8ab522e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions amdirt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def cli(ctx, verbose, no_args_is_help=True, **kwargs):
default=None,
help="[Optional] Path/URL to remote reference sample table for archive accession validation",
)
@click.option(
"-l",
"--local_json_schema",
type=click.Path(writable=True),
help="path to folder with local JSON schemas",
)
@click.option("-m", "--markdown", is_flag=True, help="Output is in markdown format")
@click.pass_context
def validate(ctx, no_args_is_help=True, **kwargs):
Expand Down
12 changes: 12 additions & 0 deletions amdirt/configuration/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Settings:
def __init__(self):
self.local_json_schema = None

def update(self, updates):
for key, value in updates.items():
if hasattr(self, key):
setattr(self, key, value)
else:
print(f"Warning: Setting '{key}' not found.")

settings = Settings()
6 changes: 6 additions & 0 deletions amdirt/validate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from amdirt.validate.application import AMDirValidator
import warnings

import amdirt.configuration.configuration as config

def run_validation(
dataset,
schema,
schema_check,
local_json_schema,
line_dup,
columns,
doi,
Expand All @@ -14,6 +17,9 @@ def run_validation(
markdown,
verbose,
):
if local_json_schema:
config.settings.update({"local_json_schema": local_json_schema})

if not verbose:
warnings.filterwarnings("ignore")
v = AMDirValidator(schema, dataset)
Expand Down
19 changes: 17 additions & 2 deletions amdirt/validate/domain/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import pandas as pd
import amdirt.configuration.configuration as config
from amdirt.validate import exceptions
from amdirt.core import logger
from io import StringIO
Expand All @@ -11,6 +12,7 @@
from typing import AnyStr, BinaryIO, TextIO, Union
import requests
import re
import os

Schema = Union[AnyStr, BinaryIO, TextIO]
Dataset = Union[AnyStr, BinaryIO, TextIO]
Expand Down Expand Up @@ -113,13 +115,26 @@ def read_schema(self, schema: Schema) -> dict:
try:
if str(schema).startswith("http"):
res = requests.get(schema)
json_schema = {}
if res.status_code == 200:
return res.json()
json_schema = res.json()
else:
raise Exception("Could not fetch schema from URL")
else:
with open(schema, "r") as s:
return json.load(s)
json_schema = json.load(s)

if config.settings.local_json_schema:
remote_ref_path = "https://spaam-community.github.io/AncientMetagenomeDir/assets/enums/"
local_ref_path = os.path.abspath(config.settings.local_json_schema) + "/"

for key in json_schema["items"]["properties"]:
if "$ref" in json_schema["items"]["properties"][key]:
val = json_schema["items"]["properties"][key]["$ref"]
val = val.replace(remote_ref_path, local_ref_path)
json_schema["items"]["properties"][key]["$ref"] = val

return json_schema
except json.JSONDecodeError as e:
msg = str(e.with_traceback(e.__traceback__))
self.add_error(
Expand Down

0 comments on commit 8ab522e

Please sign in to comment.