Skip to content
Snippets Groups Projects
Commit cb9e230dd43e authored by Noé Gaumont's avatar Noé Gaumont :octopus:
Browse files

feat add setup.py, ci and test

parent 6f834823d046
No related branches found
No related tags found
1 merge request!1Add-ci
---
default:
image: python:3.7
include:
- project: "open-source/gitlab-ci-templates"
ref: "branch/default"
file:
- "templates/no-duplicated-ci-pipelines.yml" # use workflow to avoid duplicated pipelines
- "templates/lint/flake8.yml" # will do the equivalent of 'tox -e flake8'
- "templates/lint/black.yml" # will do the equivalent of 'tox -e black'
- "templates/lint/mypy.yml" # will do the equivalent of 'tox -e mypy'
- "templates/lint/check-manifest.yml" # will do the equivalent of 'tox -e check-manifest'
- "templates/tests/py3.yml" # will do the equivalent of 'tox -e py3'
# - "templates/create-release-on-heptapod.yml" # this will create a release on heptapod
# - "templates/upload-to-pypi.yml" # on a new mercurial tag (expected to be done with release-new), will push a release on pypi
# uncomment and uses to customize/extend the configuration here if needed
# (it needs to be at the same level than "- project")
# - ".gitlab-ci-extended.yml"
stages:
- lint
- tests
- release
- publish
include *.py
recursive-include owl2yams *.py
recursive-include test *.py *.owl *.yams
exclude *.ini *.example
exclude .gitlab-ci.yml
from rdflib import Graph, RDF, RDFS, OWL, XSD
from rdflib import Graph, RDF, RDFS, OWL, XSD # type: ignore
from urllib.parse import urlparse
......@@ -2,7 +2,7 @@
from urllib.parse import urlparse
from yams.schema import Schema
from yams.serialize import serialize_to_python
from yams.buildobjs import (
from yams.schema import Schema # type: ignore
from yams.serialize import serialize_to_python # type: ignore
from yams.buildobjs import ( # type: ignore
EntityType,
RelationDefinition,
RelationType,
......@@ -29,6 +29,7 @@
return url.path.split("/")[-1]
owl_model = Graph()
owl_model.parse("./model.owl", format="turtle")
def main():
owl_model = Graph()
owl_model.parse("./model.owl", format="turtle")
......@@ -34,2 +35,5 @@
# 0. Create a new YAMS Schema
schema = Schema("LOUTRE")
register_base_types(schema)
......@@ -35,3 +39,4 @@
# 0. Create a new YAMS Schema
# p = s._entities["Person"] # normalement p = s.entity_schema_for("Person")
# p.check({'name':1})
......@@ -37,10 +42,15 @@
schema = Schema("LOUTRE")
register_base_types(schema)
# p = s._entities["Person"] # normalement p = s.entity_schema_for("Person")
# p.check({'name':1})
# 1. fetch all classes
# 1. fetch all classes
for class_uri, _, _ in owl_model.triples((None, RDF.type, OWL.Class)):
class_fragment = fragment_from_uri(class_uri)
schema.add_entity_type(EntityType(class_fragment))
entity_schema = schema._entities[class_fragment]
ETYPE_URI[class_fragment] = class_uri
superior_classes = []
for _, _, superior_class_uri in owl_model.triples(
(class_uri, RDFS.subClassOf, None)
):
superior_classes.append(fragment_from_uri(superior_class_uri))
if superior_classes:
entity_schema._specialized_type = ", ".join(superior_classes)
......@@ -46,16 +56,3 @@
for class_uri, _, _ in owl_model.triples((None, RDF.type, OWL.Class)):
class_fragment = fragment_from_uri(class_uri)
schema.add_entity_type(EntityType(class_fragment))
entity_schema = schema._entities[class_fragment]
ETYPE_URI[class_fragment] = class_uri
superior_classes = []
for _, _, superior_class_uri in owl_model.triples(
(class_uri, RDFS.subClassOf, None)
):
superior_classes.append(fragment_from_uri(superior_class_uri))
if superior_classes:
entity_schema._specialized_type = ", ".join(superior_classes)
# 2. fetch all datatype properties
# 2. fetch all datatype properties
......@@ -61,22 +58,23 @@
for (
datatype_property_uri,
_,
_,
) in owl_model.triples((None, RDF.type, OWL.DatatypeProperty)):
datatype_property_uri_fragment = fragment_from_uri(datatype_property_uri)
schema.add_relation_type(RelationType(datatype_property_uri_fragment))
# take first range, if no range use RDFS.Literal
_, _, literal_type = next(
owl_model.triples((datatype_property_uri, RDFS.range, None)),
(_, _, RDFS.Literal),
)
yams_type = LITERAL_TYPES_TO_YAMS_TYPES[literal_type]
for _, _, domain_uri in owl_model.triples(
(datatype_property_uri, RDFS.domain, None)
):
domain_uri_fragment = fragment_from_uri(domain_uri)
schema.add_relation_def(
RelationDefinition(
domain_uri_fragment, datatype_property_uri_fragment, yams_type
for (
datatype_property_uri,
_,
_,
) in owl_model.triples((None, RDF.type, OWL.DatatypeProperty)):
datatype_property_uri_fragment = fragment_from_uri(datatype_property_uri)
schema.add_relation_type(RelationType(datatype_property_uri_fragment))
# take first range, if no range use RDFS.Literal
_, _, literal_type = next(
owl_model.triples((datatype_property_uri, RDFS.range, None)),
(_, _, RDFS.Literal),
)
yams_type = LITERAL_TYPES_TO_YAMS_TYPES[literal_type]
for _, _, domain_uri in owl_model.triples(
(datatype_property_uri, RDFS.domain, None)
):
domain_uri_fragment = fragment_from_uri(domain_uri)
schema.add_relation_def(
RelationDefinition(
domain_uri_fragment, datatype_property_uri_fragment, yams_type
)
)
......@@ -82,3 +80,2 @@
)
)
......@@ -84,2 +81,2 @@
print(serialize_to_python(schema))
print(serialize_to_python(schema))
setup.py 0 → 100644
#!/usr/bin/env python
# coding: utf-8
from pathlib import Path
from setuptools import find_packages, setup
ROOT = Path(__file__).parent
distname = "owl2yams"
version = "0.1.0"
license = "LGPL"
description = "A tools to transforms owl into yams schema"
long_description = (ROOT / "README.md").read_text()
author = "Fabien Amarger"
author_email = "famarger@logilab.fr"
requires = {
"yams": None,
"rdflib": None,
}
install_requires = ["{0} {1}".format(d, v or "").strip() for d, v in requires.items()]
setup(
name=distname,
version=version,
license=license,
description=description,
long_description=long_description,
author=author,
author_email=author_email,
packages=find_packages(exclude=["test"]),
include_package_data=True,
install_requires=install_requires,
zip_safe=False,
entry_points={"console_scripts": ["owl2yams = owl2yams:main"]},
)
import unittest
import pathlib
from owl2yams import main
TEST_DATA = pathlib.Path(__file__).parent / "data"
class Owl2YamsTC(unittest.TestCase):
def test_transform_owl_to_yams(self):
data_input = TEST_DATA / "model.owl" / "in"
main(data_input)
if __name__ == "__main__":
unittest.main()
tox.ini 0 → 100644
[tox]
envlist = py3,flake8,check-manifest,black
[testenv]
deps =
pytest
commands =
{envpython} -m pytest {posargs:test}
[testenv:flake8]
basepython = python3
skip_install = true
deps =
flake8 >= 3.6
commands = flake8
[testenv:check-manifest]
skip_install = true
deps =
check-manifest
commands =
{envpython} -m check_manifest {toxinidir}
[testenv:mypy]
deps =
mypy >= 0.761
commands = mypy --install-types --non-interactive owl2yams
[testenv:black]
basepython = python3
skip_install = true
deps =
black >= 19.10b0
commands = black --check .
[testenv:black-run]
basepython = python3
skip_install = true
deps =
black >= 19.10b0
commands = black .
[testenv:pypi-publish]
basepython = python3
skip_install = true
whitelist_externals = rm
deps =
twine
passenv =
TWINE_USERNAME
TWINE_PASSWORD
commands =
rm -rf build dist .egg .egg-info
python3 setup.py sdist bdist_wheel
twine check dist/*
twine upload --skip-existing dist/*
[testenv:yamllint]
skip_install = true
deps = yamllint
commands =
yamllint .
[flake8]
basepython = python3
format = pylint
ignore = W503, E203, E731, E231
max-line-length = 100
exclude = test/data/*,.tox/*
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment