Metadata-Version: 2.1
Name: linked-data-python
Version: 0.0.3
Summary: The python package "linked-data-python" can execute .ldpy files and run an interactive ldpy console.
Home-page: https://gitlab.com/coswot/ldpy
Author: Maxime Lefrançois
Author-email: maxime.lefrancois@emse.fr
License: MIT
Platform: any
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Operating System :: OS Independent
Classifier: Natural Language :: English
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: antlr4
Requires-Dist: ideas
Requires-Dist: rdflib

# Linked-Data Python

The Linked-Data Python package allow to run .ldpy scripts, run a ldpy interactive console, and import .ldpy library modules.

![](https://gitlab.com/coswot/ldpy/-/raw/master/ldpyIcon.png)

## Installation

You can install the Linked-Data Python 
```
pip install linked-data-python
```

## The Linked-Data Python syntax

The extension ["linked-data-python" for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=MaximeLefrancois.linked-data-python) enables the syntax highlighting for Linked-Data Python source files (extensions .ldpy).

The Linked-Data Python grammar only uses grammar rules supported by MicroPython 1.18, and adds support for Linked Data languages primitives and constructs: 

- prefix declaration statements: `@prefix ex: <http://example.org/> .`
- base declaration statements: `@base <http://example.org/> .`
- IRIs: `<http://example.org/>
- Prefixed names: `ex:Person` 
- RDF literals: `"hello"^^xsd:string`, `f"hello {world}"@en`
- RDF graphs: `g{ ex:Person a owl:Class ; rdfs:label "Person"@en }`

Furthermore, it allows:

- formatted IRIs: `f<http://example/org/{ id }/>`
- formatted nodes in RDF graphs: `f{ ex:Person ex:age ?{ age } }`

Example programs are available in the `examples` folder of the source code repository.


# How to use

## In the command line

The Linked-Data Python package is a command line application, name `ldpy`. It can also be run as `python -m ldpy`.

```
$ python -m ldpy -h
usage: __main__.py [-h] [-v] [-l] [-p] [-s] [-i] [-m MODULE | source]

ldpy extends the Python syntax with primitives from the Semantic Web such as namespaces, RDF terms, and RDF graphs.

If no source is given, ldpy will start an interactive console.

positional arguments:
  source                Program read from script file. The file extension may be .ldpy or .py.

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         only displays the current version.
  -l, --debug-lexer     print the lexer output
  -p, --debug-parser    print the parser output
  -s, --show_changes    shows the transformed code before it is executed.
  -i                    starts the interactive console after executing a source
  -m MODULE, --module MODULE
                        Run library module as a script. The module may be a .ldpy or a .py file.
```

## As a module

```
import ldpy

ldpy.config.debug_ldpy = True # print transformed source  
ldpy.config.debug_lexer = False # print tokens
ldpy.config.debug_parser = False # print syntax tree

ldpy.transform_source(source) # transform a Linked-Data Python source into a Python source
```

## Run from the source code

### 1. Dependencies

```
# cd ldpy
pip install -r requirements.txt
```

### 2. Generate the parser

Generate the `SWPythonLexer`, `SWPythonParser`, and `SWPythonVisitor` using ANTLR4:

```
# cd ldpy
antlr4 -o ldpy/rewriter/antlr -package ldpy.rewriter.antlr -Xexact-output-dir -Dlanguage=Python3 -visitor -no-listener grammars/LDPython.g4
```


## Release Notes

All notable changes to the "linked-data-python" are documented below.

## [0.0.3] - 2023-02-24

- use `ideas` import hooks to enable using `.ldpy` files from import statements
- added interactive ldpy console
- added line mapping to point to the right line in the `.ldpy` file on error
- corrected small bugs in the parser

## [0.0.2] - 2022-04-30

- corrected many small bugs
- added many examples
- add `-d` functionality

## [0.0.1] - 2022-04-29

- Initial release


