Skip to main content

PyShExC - Python ShEx compiler

Project description

This repository was originally developed by Eric Prud'hommeaux (@ericprud). All credit goes to him and other original developers. The reason for creating a detached fork (original repo) in the LinkML organization was to be able to support Python 3.14 in LinkML and to put the project in an organization that would allow for sustainability through community exposure as the library did not receive updates in the last years. Efforts to get in touch with the original developers sadly was not successful.

PyShExC - The Python ShExC Parser

This package converts the Shape Expression Compact (ShExC) into Python JSON Schema Binding (pyjsg) objects. The pyjsg object representation can be used to:

  • Produce equivalent ShEx definitions in ShEx JSON (ShExJ) notation
  • Produce equivalent ShEx definitions in ShEx RDF (ShExR) notation
  • Implement a Python ShEx interpreter

History

  • 0.4.0 -- Almost ready for 2.1 release. 9 test errors remain, all involving unicode
  • 0.4.1 -- Added contextcache. See test_shexr for how to use
  • 0.4.2 -- Tweaks in 4 and 8 byte unicode parsing
  • 0.5.0 -- Passes all 2.1 tests
  • 0.5.1 -- Fix requirements
  • 0.5.2 -- Add URL fetch ability and fix UTF8-BOM issue
  • 0.5.3 -- Factor out fetch ability for re-use in other packages
  • 0.5.4 -- Fix to 0.5.2 -- low probability type matches have to be ignored
  • 0.10.0 -- Support for Python 3.10-3.14. Change of build.
  • 0.10.1 -- Updated release workflow.

Installation

> pip install PyShExC

Usage

Command Line

usage: shexc_to_shexj [-h] [-nj] [-nr] [-j JSONFILE] [-r RDFFILE]
                      [--context CONTEXT]
                      [-f {trix,trig,ttl,n3,turtle,ntriples,xml,json-ld,nt11,pretty-xml,nquads,nt}]
                      infile

positional arguments:
  infile                Input ShExC specification

optional arguments:
  -h, --help            show this help message and exit
  -nj, --nojson         Do not produce json output
  -nr, --nordf          Do not produce rdf output
  -j JSONFILE, --jsonfile JSONFILE
                        Output ShExJ file (Default: {infile}.json)
  -r RDFFILE, --rdffile RDFFILE
                        Output ShExR file (Default: {infile}.{fmt suffix})
  --context CONTEXT     Alternative @context for json to RDF
  -f {trix,trig,ttl,n3,turtle,ntriples,xml,json-ld,nt11,pretty-xml,nquads,nt}, --format {trix,trig,ttl,n3,turtle,ntriples,xml,json-ld,nt11,pretty-xml,nquads,nt}
                        Output format (Default: turtle)
 > wget https://raw.githubusercontent.com/shexSpec/shexTest/master/schemas/FocusIRI2groupBnodeNested2groupIRIRef.shex -O test.shex
 > shexc_to_shexj test.shex  --context shex.jsonld
  JSON output written to test.json
  turtle output written to test.ttl

test.shex

<http://a.example/S1> IRI /^https?:\/\// {
   <http://a.example/p1> <http://a.example/dt1> ;
   <http://a.example/p2> BNODE {
     <http://a.example/p3> LITERAL ;
     <http://a.example/p4> IRI /^https?:\/\// @<http://a.example/S1>?
   } AND CLOSED { <http://a.example/p3> . ; <http://a.example/p4> . }
}

test.json

{
   "type": "Schema",
   "@context": "http://www.w3.org/ns/shex.jsonld",
   "shapes": [
      {
         "type": "ShapeAnd",
         "id": "http://a.example/S1",
         "shapeExprs": [
            {
               "type": "NodeConstraint",
               "nodeKind": "iri",
               "pattern": "^https?://"
            },
            {
               "type": "Shape",
               "expression": {
                  "type": "EachOf",
                  "expressions": [
                     {
                        "type": "TripleConstraint",
                        "predicate": "http://a.example/p1",
                        "valueExpr": {
                           "type": "NodeConstraint",
                           "datatype": "http://a.example/dt1"

                        }
                     },
                     {
                        "type": "TripleConstraint",
                        "predicate": "http://a.example/p2",
                        "valueExpr": {
                           "type": "ShapeAnd",
                           "shapeExprs": [
                              {
                                 "type": "ShapeAnd",
                                 "shapeExprs": [
                                    {
                                       "type": "NodeConstraint",
                                       "nodeKind": "bnode"
                                    },
                                    {
                                       "type": "Shape",
                                       "expression": {
                                          "type": "EachOf",
                                          "expressions": [
                                             {
                                                "type": "TripleConstraint",
                                                "predicate": "http://a.example/p3",
                                                "valueExpr": {
                                                   "type": "NodeConstraint",
                                                   "nodeKind": "literal"
                                                }
                                             },
                                             {
                                                "type": "TripleConstraint",
                                                "predicate": "http://a.example/p4",
                                                "valueExpr": {
                                                   "type": "ShapeAnd",
                                                   "shapeExprs": [
                                                      {
                                                         "type": "NodeConstraint",
                                                         "nodeKind": "iri",
                                                         "pattern": "^https?://"
                                                      },
                                                      "http://a.example/S1"
                                                   ]
                                                },
                                                "min": 0,
                                                "max": 1
                                             }
                                          ]
                                       }
                                    }
                                 ]
                              },
                              {
                                 "type": "Shape",
                                 "closed": "true",
                                 "expression": {
                                    "type": "EachOf",
                                    "expressions": [
                                       {
                                          "type": "TripleConstraint",
                                          "predicate": "http://a.example/p3"
                                       },
                                       {
                                          "type": "TripleConstraint",
                                          "predicate": "http://a.example/p4"
                                       }
                                    ]
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   ]
}

test.ttl

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix shex: <http://www.w3.org/ns/shex#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://a.example/S1> a shex:ShapeAnd ;
    shex:shapeExprs ( [ a shex:NodeConstraint ;
                shex:nodeKind shex:iri ;
                shex:pattern "^https?://" ] [ a shex:Shape ;
                shex:expression [ a shex:EachOf ;
                        shex:expressions ( [ a shex:TripleConstraint ;
                                    shex:predicate <http://a.example/p1> ;
                                    shex:valueExpr [ a shex:NodeConstraint ;
                                            shex:datatype <http://a.example/dt1> ] ] [ a shex:TripleConstraint ;
                                    shex:predicate <http://a.example/p2> ;
                                    shex:valueExpr [ a shex:ShapeAnd ;
                                            shex:shapeExprs ( [ a shex:ShapeAnd ;
                                                        shex:shapeExprs ( [ a shex:NodeConstraint ;
                                                                    shex:nodeKind shex:bnode ] [ a shex:Shape ;
                                                                    shex:expression [ a shex:EachOf ;
                                                                            shex:expressions ( [ a shex:TripleConstraint ;

        shex:predicate <http://a.example/p3> ;

        shex:valueExpr [ a shex:NodeConstraint ;

                shex:nodeKind shex:literal ] ] [ a shex:TripleConstraint ;

        shex:max 1 ;

        shex:min 0 ;

        shex:predicate <http://a.example/p4> ;

        shex:valueExpr [ a shex:ShapeAnd ;

                shex:shapeExprs ( [ a shex:NodeConstraint ;

                            shex:nodeKind shex:iri ;

                            shex:pattern "^https?://" ] <http://a.example/S1> ) ] ] ) ] ] ) ] [ a shex:Shape ;
                                                        shex:closed true ;
                                                        shex:expression [ a shex:EachOf ;
                                                                shex:expressions ( [ a shex:TripleConstraint ;
                                                                            shex:predicate <http://a.example/p3> ] [ a shex:TripleConstraint ;
                                                                            shex:predicate <http://a.example/p4> ] ) ] ] ) ] ] ) ] ] ) .

[] a shex:Schema ;
    shex:shapes <http://a.example/S1> .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyshexc-0.3.10.post1.tar.gz (130.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyshexc-0.3.10.post1-py3-none-any.whl (71.7 kB view details)

Uploaded Python 3

File details

Details for the file pyshexc-0.3.10.post1.tar.gz.

File metadata

  • Download URL: pyshexc-0.3.10.post1.tar.gz
  • Upload date:
  • Size: 130.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pyshexc-0.3.10.post1.tar.gz
Algorithm Hash digest
SHA256 202a88eb49d69367648441ea8088488ec152961e4b5ddc1fa1236c4d01e746d0
MD5 908af5a1f28e41db510ff6d15180159c
BLAKE2b-256 4db97da6f6b563c257b1d370b414d0d8c634f0b50d70824ff69ad13aa58723a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshexc-0.3.10.post1.tar.gz:

Publisher: pypi_publish.yml on linkml/grammar-python-antlr-linkml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshexc-0.3.10.post1-py3-none-any.whl.

File metadata

  • Download URL: pyshexc-0.3.10.post1-py3-none-any.whl
  • Upload date:
  • Size: 71.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pyshexc-0.3.10.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 c17b31e3ef4b55cb4a2493d0a5556dadaddbfdee06b213519afc6f65d65c4efb
MD5 2e158683315fa6ecdc08ec73717fec9b
BLAKE2b-256 7ce62e4bc7458968a2a3dc1fe9572c9cba246cbb6e954f6227744118e8c0279b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshexc-0.3.10.post1-py3-none-any.whl:

Publisher: pypi_publish.yml on linkml/grammar-python-antlr-linkml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page