Skip to content

Commit

Permalink
Switch to a python package
Browse files Browse the repository at this point in the history
Switches to using a python package that can be published to PyPi
  • Loading branch information
JPEWdev committed Jan 22, 2024
1 parent cfd6c6d commit 39a1d82
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 62 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: Upload Python Package
on:
- push

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build

- name: Publish distribution to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
skip_existing: true

- name: Publish distribution to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
35 changes: 35 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: Test shacl2code
on:
- push
- pull_request

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
steps:
- uses: actions/checkout@v4
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: |
python -m build
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: psf/black@stable
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Convert SHACL Model to code bindings

This tool can be used to convert a JSONLD SHACL model into various code
bindings
37 changes: 37 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[project]
name = "shacl2code"
description = "Convert SHACL model file to code bindings"
version = "0.0.1"
dependencies = [
"jinja2 >= 3.1.2",
"pyld >= 2.0.3",
]
required-python = ">= 3.8"
authors = [
{name = "Joshua Watt", email = "[email protected]"},
]
readme = "README.md"
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Software Development :: Build Tools",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]

[project.urls]
Homepage = "https://github.com/JPEWdev/shacl2code"
Repository = "https://github.com/JPEWdev/shacl2code.git"
Issues = "https://github.com/JPEWdev/shacl2code/issues"

[project.scripts]
shacl2code = "shacl2code:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
2 changes: 2 additions & 0 deletions shacl2code/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .model import Model
from .main import main
5 changes: 5 additions & 0 deletions shacl2code/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pathlib import Path

THIS_FILE = Path(__file__)
THIS_DIR = THIS_FILE.parent
TEMPLATE_DIR = THIS_DIR / "templates"
66 changes: 66 additions & 0 deletions shacl2code/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#! /usr/bin/env python3
#
# Copyright (c) 2024 Joshua Watt
#
# SPDX-License-Identifier: MIT

import argparse
import json
import sys
import urllib.request
from pathlib import Path

from . import Model
from .const import TEMPLATE_DIR


def get_langs():
langs = []
for child in TEMPLATE_DIR.iterdir():
if child.suffixes and child.suffixes[-1] == ".j2":
langs.append(child.stem)
return langs


def main():
parser = argparse.ArgumentParser(description="Convert JSON-LD model to python")
parser.add_argument(
"--lang",
help="Output Language",
choices=get_langs(),
required=True,
)
parser.add_argument(
"input",
help="Input JSON-LD model (path, URL, or '-')",
)
parser.add_argument(
"output",
help="Output file name or '-' for stdout",
)

args = parser.parse_args()

if "://" in args.input:
with urllib.request.urlopen(args.input) as url:
model_data = json.load(url)
elif args.input == "-":
model_data = json.load(sys.stdin)
else:
with Path(args.input).open("r") as f:
model_data = json.load(f)

m = Model(model_data)
render = m.render_template(args.lang)

if args.output == "-":
print(render)
else:
with Path(args.output).open("w") as f:
f.write(render)

return 0


if __name__ == "__main__":
sys.exit(main())
64 changes: 2 additions & 62 deletions shacl2code.py → shacl2code/model.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@
#
# SPDX-License-Identifier: MIT

import argparse
import json
import keyword
import re
import sys
import tempfile
import textwrap
import urllib.parse
import urllib.request
from pathlib import Path
import pprint

from pyld import jsonld
from jinja2 import Environment, FileSystemLoader, TemplateRuntimeError

from .const import TEMPLATE_DIR

THIS_FILE = Path(__file__)
THIS_DIR = THIS_FILE.parent
TEMPLATE_DIR = THIS_DIR / "templates"


def get_prop(o, name, key, default=None):
Expand All @@ -38,14 +30,6 @@ def to_var_name(name):
return name


def get_langs():
langs = []
for child in TEMPLATE_DIR.iterdir():
if child.suffixes and child.suffixes[-1] == ".j2":
langs.append(child.stem)
return langs


class Model(object):
def __init__(self, model_data):
self.model = jsonld.expand(model_data)
Expand Down Expand Up @@ -237,47 +221,3 @@ def abort_helper(msg):
enums=self.get_template_enums(),
classes=self.get_template_classes(),
)


def main():
parser = argparse.ArgumentParser(description="Convert JSON-LD model to python")
parser.add_argument(
"--lang",
help="Output Language",
choices=get_langs(),
required=True,
)
parser.add_argument(
"input",
help="Input JSON-LD model (path, URL, or '-')",
)
parser.add_argument(
"output",
help="Output file name or '-' for stdout",
)

args = parser.parse_args()

if "://" in args.input:
with urllib.request.urlopen(args.input) as url:
model_data = json.load(url)
elif args.input == "-":
model_data = json.load(sys.stdin)
else:
with Path(args.input).open("r") as f:
model_data = json.load(f)

m = Model(model_data)
render = m.render_template(args.lang)

if args.output == "-":
print(render)
else:
with Path(args.output).open("w") as f:
f.write(render)

return 0


if __name__ == "__main__":
sys.exit(main())
File renamed without changes.

0 comments on commit 39a1d82

Please sign in to comment.