Skip to content

Commit

Permalink
Merge pull request ReproNim#8 from Remi-Gau/remi_schema_creator
Browse files Browse the repository at this point in the history
Remi schema creator
  • Loading branch information
sanuann authored Jun 7, 2021
2 parents 3fad95c + 92427b6 commit 1b05a2f
Show file tree
Hide file tree
Showing 49 changed files with 2,324 additions and 463 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-18.04]
python-version: [3.7, 3.8]
python-version: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand Down
10 changes: 8 additions & 2 deletions reproschema/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ def validate(shapefile, path):
default="n-triples",
show_default=True,
)
@click.option(
"--prefixfile", default=None, type=click.Path(exists=True, dir_okay=False)
)
@click.option(
"--contextfile", default=None, type=click.Path(exists=True, dir_okay=False)
)
@click.argument("path", nargs=1, type=str)
def convert(path, format):
def convert(path, format, prefixfile, contextfile):
if not (path.startswith("http") or os.path.exists(path)):
raise ValueError(f"{path} must be a URL or an existing file or directory")
from .jsonldutils import to_newformat

print(to_newformat(path, format))
print(to_newformat(path, format, prefixfile, contextfile))


@main.command()
Expand Down
16 changes: 15 additions & 1 deletion reproschema/jsonldutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def validate_data(data, shape_file_path):
return conforms, v_text


def to_newformat(path, format):
def to_newformat(path, format, prefixfile=None, contextfile=None):
"""Convert a JSONLD document to n-triples format
Since PyLD requires an http url, a local server is started to serve the
Expand All @@ -87,6 +87,11 @@ def to_newformat(path, format):
A local path or remote url to convert to n-triples
format: str of enum
Returned format jsonld, n-triples, turtle
prefixfile: str
Prefixes to use when converting to turtle (ignored otherwise)
contextfile: str
Context to use for compaction when returning jsonld. If not provided,
a jsonld graph is returned.
Returns
-------
Expand All @@ -99,6 +104,10 @@ def to_newformat(path, format):
raise ValueError(f"{format} not in {supported_formats}")
data = load_file(path)
if format == "jsonld":
if contextfile is not None:
with open(contextfile) as fp:
context = json.load(fp)
data = jsonld.compact(data, context)
return json.dumps(data, indent=2)
kwargs = {"algorithm": "URDNA2015", "format": "application/n-quads"}
nt = jsonld.normalize(data, kwargs)
Expand All @@ -112,5 +121,10 @@ def to_newformat(path, format):
g.bind("nidm", "http://purl.org/nidash/nidm#")
g.bind("skos", "http://www.w3.org/2004/02/skos/core#")
g.bind("prov", "http://www.w3.org/ns/prov#")
if prefixfile is not None:
with open(prefixfile) as fp:
prefixes = json.load(fp)
for key, value in prefixes.items():
g.bind(key, value)
g.parse(data=nt, format="nt")
return g.serialize(format=format).decode()
8 changes: 8 additions & 0 deletions reproschema/models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# README

## reproschema file creation

The creation reproschema protocols, activities and items (or Field) are handled
by 3 python classes with the same names contained in their dedicated modules.

They all inherit from the same base class.
72 changes: 37 additions & 35 deletions reproschema/models/activity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .base import SchemaBase

DEFAULT_LANG = "en"


class Activity(SchemaBase):
"""
Expand All @@ -8,47 +10,41 @@ class to deal with reproschema activities

schema_type = "reproschema:Activity"

visible = True
required = False
skippable = True

def __init__(self, version=None):
super().__init__(version)
self.schema["ui"] = {"shuffle": [], "order": [], "addProperties": []}

def set_ui_shuffle(self, shuffle=False):
self.schema["ui"]["shuffle"] = shuffle

def set_URI(self, URI):
self.URI = URI

def get_URI(self):
return self.URI

# TODO
# preamble
# compute
# citation
# image

def set_defaults(self, name):
self._ReproschemaSchema__set_defaults(name) # this looks wrong
self.set_ui_shuffle(False)
def set_defaults(self, name="default"):
self._SchemaBase__set_defaults(name)
self.set_preamble()
self.set_ui_default()

def update_activity(self, item_info):

# TODO
# - remove the hard coding on visibility and valueRequired

# update the content of the activity schema with new item
def set_compute(self, variable, expression):
self.schema["compute"] = [
{"variableName": variable, "jsExpression": expression}
]

item_info["URI"] = "items/" + item_info["name"]
def append_item(self, item):
# See comment and TODO of the append_activity Protocol class

append_to_activity = {
"variableName": item_info["name"],
"isAbout": item_info["URI"],
"isVis": item_info["visibility"],
"valueRequired": False,
property = {
"variableName": item.get_basename(),
"isAbout": item.get_URI(),
"isVis": item.visible,
"requiredValue": item.required,
}
if item.skippable:
property["allow"] = ["reproschema:Skipped"]

self.schema["ui"]["order"].append(item_info["URI"])
self.schema["ui"]["addProperties"].append(append_to_activity)
self.schema["ui"]["order"].append(item.get_URI())
self.schema["ui"]["addProperties"].append(property)

"""
writing, reading, sorting, unsetting
"""

def sort(self):
schema_order = [
Expand All @@ -59,9 +55,15 @@ def sort(self):
"description",
"schemaVersion",
"version",
"preamble",
"citation",
"image",
"compute",
"ui",
]
self.sort_schema(schema_order)
self.sort_ui()

ui_order = ["shuffle", "order", "addProperties"]
self.sort_ui(ui_order)
def write(self, output_dir):
self.sort()
self._SchemaBase__write(output_dir)
Loading

0 comments on commit 1b05a2f

Please sign in to comment.