Skip to content
New issue

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

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

Already on GitHub? # to your account

Add localpat to json schema amdirt validate #168

Open
wants to merge 3 commits into
base: master
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
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
1 change: 0 additions & 1 deletion amdirt/viewer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# from amdirt import logger
from distutils.log import warn
import sys
from streamlit.web import cli as stcli
from pathlib import Path
Expand Down
Loading