# textx-lang-irirefs
![logos](https://github.com/Jean-Francois-Baget/textx-lang-irirefs/blob/main/img/logos.jpg?raw=true)
**textx-lang-irirefs** is a python implementation of the IETF standards [RFC 3987 Internationalized Resource Identifiers](https://www.ietf.org/rfc/rfc3987.txt) and [RFC 3986 Uniform Resource Identifiers](https://www.ietf.org/rfc/rfc3986.txt), relying upon the meta-language [textX](https://github.com/textX/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](https://team.inria.fr/boreal/) team ([Inria](https://www.inria.fr/fr) and [LIRMM](https://www.lirmm.fr/)). It is part of the [textx-lang-dlgpe](https://github.com/Jean-Francois-Baget/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.
```python
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.
```python
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](https://raw.githubusercontent.com/Jean-Francois-Baget/textx-lang-irirefs/refs/heads/main/src/textxirirefs/iri.tx). For more details on textx visualization, see https://textx.github.io/textX/visualization.html.
```python
from textx.export import metamodel_export
metamodel_export(parser, 'iri.dot')
```
This codes generates a file `iri.dot` that can be visualized with [Graphviz](https://graphviz.org/).
### Parsing an IRIRef
Most importantly, the parser can be used to generate a *model* from a python string encoding an IRIRef.
```python
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.
```python
from textx.export import model_export
model_export(fullmodel, 'fullmodel.dot')
```
This file `fullmodel.dot` can also be visualized with [Graphviz](https://graphviz.org/).
![model](https://github.com/Jean-Francois-Baget/textx-lang-irirefs/blob/main/img/model.png?raw=true)
### Resolving the model of a Relative IRI against a base
```python
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
Raw data
{
"_id": null,
"home_page": null,
"name": "textx-lang-irirefs",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "python, textx, iri, uri, iriref, uriref",
"author": null,
"author_email": "Jean-Fran\u00e7ois Baget <baget@lirmm.fr>",
"download_url": "https://files.pythonhosted.org/packages/b9/74/71635b5fc6ed4677df026137a14e1dfbd0a660796b9195df182c9dde2292/textx_lang_irirefs-0.0.dev8.tar.gz",
"platform": null,
"description": "# textx-lang-irirefs\r\n\r\n![logos](https://github.com/Jean-Francois-Baget/textx-lang-irirefs/blob/main/img/logos.jpg?raw=true)\r\n\r\n\r\n**textx-lang-irirefs** is a python implementation of the IETF standards [RFC 3987 Internationalized Resource Identifiers](https://www.ietf.org/rfc/rfc3987.txt) and [RFC 3986 Uniform Resource Identifiers](https://www.ietf.org/rfc/rfc3986.txt), relying upon the meta-language [textX](https://github.com/textX/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\r\n\r\nThe `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.\r\n\r\n**textx-lang-irirefs** has been created by Jean-Fran\u00e7ois Baget at the [Boreal](https://team.inria.fr/boreal/) team ([Inria](https://www.inria.fr/fr) and [LIRMM](https://www.lirmm.fr/)). It is part of the [textx-lang-dlgpe](https://github.com/Jean-Francois-Baget/textx-lang-dlgpe) project.\r\n\r\n### Walkthrough\r\n\r\nThe 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.\r\n\r\n\r\n```python\r\nfrom textx import metamodel_for_language\r\n\r\nparser = metamodel_for_language('textxirirefs') # building the parser\r\n\r\n\r\nbase = 'http://www.lirmm.fr'\r\nrelative = '~baget'\r\n\r\nbasemodel = parser.model_from_str(base) # basemodel is an IRIRefContainer object\r\nrelativemodel = parser.model_from_str(relative) # as is relativemodel\r\n\r\nresolved = basemodel.resolve(relativemodel) # and so is resolved\r\nrecomposed = resolved.recompose() # recomposed is now a string\r\n\r\ntest = recomposed == 'http://www.lirmm.fr/~baget' # test is true\r\n```\r\n\r\n\r\n## Installation\r\n\r\n```\r\npip install textx-lang-irirefs\r\n```\r\n\r\n### Testing\r\n\r\nYou can test that everything behaves correctly (but first you have to clone the whole repository).\r\n\r\n```\r\ngit clone https://github.com/Jean-Francois-Baget/textx-lang-irirefs.git\r\ncd textx-lang-irirefs\r\npython -m unittest\r\n```\r\n\r\n```\r\n.....\r\n----------------------------------------------------------------------\r\nRan 5 tests in 0.179s\r\n\r\nOK\r\n```\r\n\r\n### irirefresolve\r\n\r\nThe 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.\r\n\r\n`irirefresolve --help`\r\n\r\n```\r\nusage: irirefresolve [-h] [-c] base relative\r\n\r\nThis command returns the resolution of a relative IRI against a base IRI, \r\naccording to the algorithm standardized in [RFC 3986] URI Generic Syntax. \r\nWhen the --compatibility flag is set, an non strict resolution algorithm \r\nis applied according to prior specifications of partial URI [RFC1630]\r\n\r\npositional arguments:\r\n base the IRI against which is resolved the relative\r\n relative the relativeIRI to resolve against a base\r\n\r\noptions:\r\n -h, --help show this help message and exit\r\n -c, --compatibility non strict resolution when in compatibility mode\r\n\r\nFrom textx-lang-irirefs (0.0dev7), (c)2023 Jean-Fran\u00e7ois Baget, Inria.\r\n```\r\n\r\nFor 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.\r\n\r\n## Usage\r\n\r\n### Building the parser\r\n\r\nThe first thing to do is to build the Json parser. This can be done with the following code.\r\n\r\n```python\r\nfrom textx import metamodel_for_language\r\n\r\nparser = metamodel_for_language('textxirirefs')\r\n```\r\n\r\n#### Visualizing the grammar\r\n\r\nThis parser can be used to obtain a graphical representation of the grammar [iri.tx](https://raw.githubusercontent.com/Jean-Francois-Baget/textx-lang-irirefs/refs/heads/main/src/textxirirefs/iri.tx). For more details on textx visualization, see https://textx.github.io/textX/visualization.html.\r\n\r\n```python\r\nfrom textx.export import metamodel_export\r\n\r\nmetamodel_export(parser, 'iri.dot')\r\n```\r\nThis codes generates a file `iri.dot` that can be visualized with [Graphviz](https://graphviz.org/).\r\n\r\n### Parsing an IRIRef\r\n\r\nMost importantly, the parser can be used to generate a *model* from a python string encoding an IRIRef.\r\n\r\n\r\n```python\r\nfull = \"http://foo@www.lirmm.fr:8080/~baget/data#fragment?query=true\"\r\nbase = \"http://www.lirmm.fr/~baget#fragment\"\r\nrelative = \"data?query=true\"\r\n\r\nfullmodel = parser.model_from_str(full)\r\nbasemodel = parser.model_from_str(base)\r\nrelativemodel = parser.model_from_str(relative)\r\n```\r\n\r\n#### Visualizing the model\r\n\r\nAs for the parser, the model can be visualized.\r\n\r\n```python\r\nfrom textx.export import model_export\r\n\r\nmodel_export(fullmodel, 'fullmodel.dot')\r\n```\r\nThis file `fullmodel.dot` can also be visualized with [Graphviz](https://graphviz.org/).\r\n\r\n\r\n![model](https://github.com/Jean-Francois-Baget/textx-lang-irirefs/blob/main/img/model.png?raw=true)\r\n\r\n### Resolving the model of a Relative IRI against a base\r\n\r\n\r\n```python\r\nresolvedmodel = basemodel.resolve(relativemodel)\r\n\r\ntest = resolvedmodel.recompose() == 'http://www.lirmm.fr/data?query=true' # test is True\r\n```\r\n\r\n## TO DO\r\n\r\n* implement correctly the `normalize()` method\r\n* classify correctly the parsed IRIRefs\r\n\r\n\r\n",
"bugtrack_url": null,
"license": "MIT AND (Apache-2.0 OR BSD-2-clause)",
"summary": "A textX implementation of IRIReferences.",
"version": "0.0.dev8",
"project_urls": null,
"split_keywords": [
"python",
" textx",
" iri",
" uri",
" iriref",
" uriref"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "21fe05c9e8635ada84e09277e04459b9ba3d538415e8ce838be3d93d0788a533",
"md5": "eba24b2a59fc86b2c4f8a46d80c9a697",
"sha256": "49019640c2826ceadbb2146b24e4af9d2c3800ed9b9e63d1c4af4108a9e9e088"
},
"downloads": -1,
"filename": "textx_lang_irirefs-0.0.dev8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "eba24b2a59fc86b2c4f8a46d80c9a697",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 17582,
"upload_time": "2024-09-30T11:32:40",
"upload_time_iso_8601": "2024-09-30T11:32:40.663937Z",
"url": "https://files.pythonhosted.org/packages/21/fe/05c9e8635ada84e09277e04459b9ba3d538415e8ce838be3d93d0788a533/textx_lang_irirefs-0.0.dev8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b97471635b5fc6ed4677df026137a14e1dfbd0a660796b9195df182c9dde2292",
"md5": "f44d69f9bc9ce819c1d2b8f5316cc165",
"sha256": "dc12ba6a9da062f382294c37d1b34e4665ce33777b26da90e6ce2ab72242dbf2"
},
"downloads": -1,
"filename": "textx_lang_irirefs-0.0.dev8.tar.gz",
"has_sig": false,
"md5_digest": "f44d69f9bc9ce819c1d2b8f5316cc165",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 15334,
"upload_time": "2024-09-30T11:32:41",
"upload_time_iso_8601": "2024-09-30T11:32:41.838282Z",
"url": "https://files.pythonhosted.org/packages/b9/74/71635b5fc6ed4677df026137a14e1dfbd0a660796b9195df182c9dde2292/textx_lang_irirefs-0.0.dev8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-30 11:32:41",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "textx-lang-irirefs"
}