Skip to main content

A textX implementation of IRIReferences.

Project description

textx-lang-irirefs

logos

textx-lang-irirefs is a python implementation of the IETF standards RFC 3987 Internationalized Resource Identifiers and RFC 3986 Uniform Resource Identifiers, relying upon the meta-language textX. Though many python libraries allow for the parsing of IRIRefs, it is a good alternative when you want to use those IRIRefs in your own textX grammar

The textxirirefs package provides a parser (basically a textX metamodel), able to build a textx model from a string representing an IRIRef. This model can be visualized, for educational purpose, but more importantly can be normalized, recomposed, or resolved against a base IRI.

textx-lang-irirefs has been created by Jean-François Baget at the Boreal team (Inria and LIRMM). It is part of the textx-lang-dlgpe project.

Walkthrough

The following code demonstrates, in python, how to build a parser, generate a model from a python string respecting the RFC 3987 or RFC 3986 standards, resolve the model against a base IRI, normalize then recompose the result.

from textx import metamodel_for_language

parser = metamodel_for_language('textxirirefs') # building the parser


base = 'http://www.lirmm.fr'
relative = '~baget'

basemodel = parser.model_from_str(base) # basemodel is an IRIRefContainer object
relativemodel = parser.model_from_str(relative) # as is relativemodel

resolved = basemodel.resolve(relativemodel) # and so is resolved
recomposed = resolved.recompose() # recomposed is now a string

test = recomposed == 'http://www.lirmm.fr/~baget' # test is true

Installation

pip install textx-lang-irirefs

Testing

You can test that everything behaves correctly (but first you have to clone the whole repository).

git clone https://github.com/Jean-Francois-Baget/textx-lang-irirefs.git
cd textx-lang-irirefs
python -m unittest
.....
----------------------------------------------------------------------
Ran 5 tests in 0.179s

OK

irirefresolve

The installation also generates the irirefresolve command, that can be used to resolve a relative IRI against a base IRI without having to write any python code.

irirefresolve --help

usage: irirefresolve [-h] [-c] base relative

This command returns the resolution of a relative IRI against a base IRI, 
according to the algorithm standardized in [RFC 3986] URI Generic Syntax. 
When the --compatibility flag is set, an non strict resolution algorithm 
is applied according to prior specifications of partial URI [RFC1630]

positional arguments:
  base                 the IRI against which is resolved the relative
  relative             the relativeIRI to resolve against a base

options:
  -h, --help           show this help message and exit
  -c, --compatibility  non strict resolution when in compatibility mode

From textx-lang-irirefs (0.0dev7), (c)2023 Jean-François Baget, Inria.

For instance, irirefresolve -c "http://www.w3.org/2001/XMLSchema#" "integer" returns http://www.w3.org/2001/integer, an unexpected (but correct) behaviour when one is used to the turtle syntax.

Usage

Building the parser

The first thing to do is to build the Json parser. This can be done with the following code.

from textx import metamodel_for_language

parser = metamodel_for_language('textxirirefs')

Visualizing the grammar

This parser can be used to obtain a graphical representation of the grammar iri.tx. For more details on textx visualization, see https://textx.github.io/textX/visualization.html.

from textx.export import metamodel_export

metamodel_export(parser, 'iri.dot')

This codes generates a file iri.dot that can be visualized with Graphviz.

Parsing an IRIRef

Most importantly, the parser can be used to generate a model from a python string encoding an IRIRef.

full = "http://foo@www.lirmm.fr:8080/~baget/data#fragment?query=true"
base = "http://www.lirmm.fr/~baget#fragment"
relative = "data?query=true"

fullmodel = parser.model_from_str(full)
basemodel = parser.model_from_str(base)
relativemodel = parser.model_from_str(relative)

Visualizing the model

As for the parser, the model can be visualized.

from textx.export import model_export

model_export(fullmodel, 'fullmodel.dot')

This file fullmodel.dot can also be visualized with Graphviz.

model

Resolving the model of a Relative IRI against a base

resolvedmodel = basemodel.resolve(relativemodel)

test = resolvedmodel.recompose() == 'http://www.lirmm.fr/data?query=true' # test is True

TO DO

  • implement correctly the normalize() method
  • classify correctly the parsed IRIRefs

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

textx_lang_irirefs-0.0.dev8.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

textx_lang_irirefs-0.0.dev8-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file textx_lang_irirefs-0.0.dev8.tar.gz.

File metadata

  • Download URL: textx_lang_irirefs-0.0.dev8.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.3

File hashes

Hashes for textx_lang_irirefs-0.0.dev8.tar.gz
Algorithm Hash digest
SHA256 dc12ba6a9da062f382294c37d1b34e4665ce33777b26da90e6ce2ab72242dbf2
MD5 f44d69f9bc9ce819c1d2b8f5316cc165
BLAKE2b-256 b97471635b5fc6ed4677df026137a14e1dfbd0a660796b9195df182c9dde2292

See more details on using hashes here.

File details

Details for the file textx_lang_irirefs-0.0.dev8-py3-none-any.whl.

File metadata

File hashes

Hashes for textx_lang_irirefs-0.0.dev8-py3-none-any.whl
Algorithm Hash digest
SHA256 49019640c2826ceadbb2146b24e4af9d2c3800ed9b9e63d1c4af4108a9e9e088
MD5 eba24b2a59fc86b2c4f8a46d80c9a697
BLAKE2b-256 21fe05c9e8635ada84e09277e04459b9ba3d538415e8ce838be3d93d0788a533

See more details on using hashes here.

Supported by

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