linked-data-python 0.0.4__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ldpy/__init__.py +35 -11
- ldpy/__main__.py +75 -250
- ldpy/build.py +91 -0
- ldpy/console.py +116 -0
- ldpy/debug.py +235 -0
- ldpy/formatter.py +329 -0
- ldpy/importer.py +110 -0
- ldpy/lsp/__init__.py +11 -0
- ldpy/lsp/__main__.py +4 -0
- ldpy/lsp/backend.py +118 -0
- ldpy/lsp/rpc.py +104 -0
- ldpy/lsp/server.py +353 -0
- ldpy/lsp/translate.py +140 -0
- ldpy/pygments_lexer.py +613 -0
- ldpy/runtime.py +931 -0
- ldpy/sparql.py +551 -0
- ldpy/transpiler/__init__.py +14 -0
- ldpy/transpiler/core.py +2558 -0
- ldpy/transpiler/errors.py +38 -0
- ldpy/transpiler/linemap.py +292 -0
- linked_data_python-0.2.0.dist-info/METADATA +158 -0
- linked_data_python-0.2.0.dist-info/RECORD +26 -0
- {linked_data_python-0.0.4.dist-info → linked_data_python-0.2.0.dist-info}/WHEEL +1 -1
- linked_data_python-0.2.0.dist-info/entry_points.txt +9 -0
- {linked_data_python-0.0.4.dist-info → linked_data_python-0.2.0.dist-info/licenses}/LICENSE.md +0 -0
- {linked_data_python-0.0.4.dist-info → linked_data_python-0.2.0.dist-info}/top_level.txt +0 -0
- ldpy/grun/lib.py +0 -63
- ldpy/grun/util.py +0 -11
- ldpy/ldpy.py +0 -183
- ldpy/rewriter/IndentedStringWriter.py +0 -54
- ldpy/rewriter/LDPythonRewriter.py +0 -677
- ldpy/rewriter/MultiChannelTokenStream.py +0 -127
- ldpy/rewriter/Result.py +0 -49
- ldpy/rewriter/__init__.py +0 -7
- ldpy/rewriter/antlr/LDPythonLexer.py +0 -870
- ldpy/rewriter/antlr/LDPythonParser.py +0 -9336
- ldpy/rewriter/antlr/LDPythonVisitor.py +0 -573
- ldpy/sparql/builtin.py +0 -326
- linked_data_python-0.0.4.dist-info/METADATA +0 -139
- linked_data_python-0.0.4.dist-info/RECORD +0 -20
- linked_data_python-0.0.4.dist-info/entry_points.txt +0 -3
ldpy/ldpy.py
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import sys
|
|
3
|
-
import math
|
|
4
|
-
|
|
5
|
-
from typing import Union, Dict, List, Set
|
|
6
|
-
from rdflib import RDF, OWL, Graph, URIRef, BNode, Literal, Variable
|
|
7
|
-
from rdflib.term import Node
|
|
8
|
-
|
|
9
|
-
from antlr4 import InputStream, Token
|
|
10
|
-
from antlr4.error.ErrorListener import ErrorListener
|
|
11
|
-
from antlr4.error.DiagnosticErrorListener import DiagnosticErrorListener
|
|
12
|
-
|
|
13
|
-
from ideas import import_hook
|
|
14
|
-
|
|
15
|
-
from ldpy.rewriter import *
|
|
16
|
-
import ldpy
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
RDFTerm = Union[URIRef, BNode, Literal]
|
|
20
|
-
Term = Union[Variable, URIRef, BNode, Literal]
|
|
21
|
-
RDFInstanceMapping = Dict[BNode, RDFTerm]
|
|
22
|
-
SolutionMapping = Dict[Variable, RDFTerm]
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class Config:
|
|
26
|
-
def __init__(self):
|
|
27
|
-
self.debug_ldpy = False
|
|
28
|
-
self.debug_lexer = False
|
|
29
|
-
self.debug_parser = False
|
|
30
|
-
self.linemap = dict()
|
|
31
|
-
self.namespaces = dict()
|
|
32
|
-
|
|
33
|
-
config = Config()
|
|
34
|
-
|
|
35
|
-
class MainErrorListener(ErrorListener):
|
|
36
|
-
|
|
37
|
-
def __init__(self):
|
|
38
|
-
self.errors = []
|
|
39
|
-
|
|
40
|
-
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
|
|
41
|
-
self.errors.append("line " + str(line) + ":" + str(column) + " " + msg)
|
|
42
|
-
|
|
43
|
-
def parseTree(input_stream, filename="<none>", diagnose=False):
|
|
44
|
-
lexer = LDPythonLexer(input_stream)
|
|
45
|
-
stream = MultiChannelTokenStream(lexer)
|
|
46
|
-
parser = LDPythonParser(stream)
|
|
47
|
-
parser.removeErrorListeners()
|
|
48
|
-
errorListener = MainErrorListener()
|
|
49
|
-
parser.addErrorListener(errorListener)
|
|
50
|
-
if diagnose:
|
|
51
|
-
diagnosticErrorListener = DiagnosticErrorListener()
|
|
52
|
-
parser.addErrorListener(diagnosticErrorListener)
|
|
53
|
-
tree = parser.file_input()
|
|
54
|
-
if parser.getNumberOfSyntaxErrors() != 0:
|
|
55
|
-
msg = "\n ".join(errorListener.errors)
|
|
56
|
-
if "extraneous input 'g{'" in msg:
|
|
57
|
-
msg = msg + "\nldpy needs a space after 'g{'"
|
|
58
|
-
exc = SyntaxError(f"ldpy SyntaxError in file {filename}:\n {msg}")
|
|
59
|
-
exc.filename = filename
|
|
60
|
-
exc.lineno = int(msg[5:msg.index(":")])
|
|
61
|
-
raise exc
|
|
62
|
-
return tree
|
|
63
|
-
|
|
64
|
-
def printTokens(lexer, tokenTypes):
|
|
65
|
-
i = 0
|
|
66
|
-
while True:
|
|
67
|
-
t:Token = lexer.nextToken()
|
|
68
|
-
print( f"[@{i},{t.start}:{t.stop}='{t.text}',<{t.type}:{tokenTypes[t.type]}>,{t.line}:{t.column} in channel {t.channel}]", file=sys.stderr)
|
|
69
|
-
i += 1
|
|
70
|
-
if t.type == -1:
|
|
71
|
-
break
|
|
72
|
-
|
|
73
|
-
def debugTokens(input_stream):
|
|
74
|
-
lexer = LDPythonLexer(input_stream)
|
|
75
|
-
import ldpy.grun.lib as lib
|
|
76
|
-
tokenTypes = lib.readTokenTypes("ldpy/rewriter/antlr/LDPython.tokens")
|
|
77
|
-
printTokens(lexer, tokenTypes)
|
|
78
|
-
|
|
79
|
-
def printTree(tree):
|
|
80
|
-
import ldpy.grun.lib as lib
|
|
81
|
-
tokenTypes = lib.readTokenTypes("ldpy/rewriter/antlr/LDPython.tokens")
|
|
82
|
-
try:
|
|
83
|
-
print(lib.format_tree(tree, True, tokenTypes), file=sys.stderr)
|
|
84
|
-
except Exception as e:
|
|
85
|
-
import traceback
|
|
86
|
-
print(traceback.format_exc(), file=sys.stderr)
|
|
87
|
-
|
|
88
|
-
def transform_source(source, filename="<none>", namespaces = dict()):
|
|
89
|
-
"""Replacement of the .ldpy source to a Python source.
|
|
90
|
-
"""
|
|
91
|
-
input_stream = InputStream(source + "\n")
|
|
92
|
-
|
|
93
|
-
if config.debug_lexer or config.debug_parser or config.debug_ldpy:
|
|
94
|
-
print(f"============== Processing file {filename} ==============", file=sys.stderr)
|
|
95
|
-
|
|
96
|
-
if config.debug_lexer:
|
|
97
|
-
print("Tokens are:", file=sys.stderr)
|
|
98
|
-
debugTokens(input_stream)
|
|
99
|
-
input_stream = InputStream(source + "\n")
|
|
100
|
-
|
|
101
|
-
tree = parseTree(input_stream, filename, False)
|
|
102
|
-
|
|
103
|
-
if config.debug_parser:
|
|
104
|
-
print("Parsed tree is:")
|
|
105
|
-
printTree(tree)
|
|
106
|
-
|
|
107
|
-
output = IndentedStringWriter()
|
|
108
|
-
visitor = LDPythonRewriter(output, namespaces)
|
|
109
|
-
try:
|
|
110
|
-
visitor.visit(tree)
|
|
111
|
-
except SyntaxError as exc:
|
|
112
|
-
msg = exc.msg
|
|
113
|
-
exc = SyntaxError(f"ldpy SyntaxError in file {filename}:\n {msg}")
|
|
114
|
-
exc.filename = filename
|
|
115
|
-
exc.lineno = int(msg[5:msg.index(":")])
|
|
116
|
-
raise exc
|
|
117
|
-
transformed_source = output.getvalue()#.strip("\n")
|
|
118
|
-
|
|
119
|
-
if config.debug_ldpy:
|
|
120
|
-
pyfilename = filename[len(os.getcwd())+1:-5] + ".py"
|
|
121
|
-
print(f"ldpy>>> =========== Transformed {pyfilename} ============", file=sys.stderr)
|
|
122
|
-
lineno=1
|
|
123
|
-
zfill = 1 if transformed_source.count("\n") == 0 else int(math.log10(transformed_source.count("\n")))+1
|
|
124
|
-
for transformed_line in transformed_source.split("\n"):
|
|
125
|
-
print(f"ldpy>>>{str(lineno).zfill(zfill)}: {transformed_line}", file=sys.stderr)
|
|
126
|
-
lineno+=1
|
|
127
|
-
print("ldpy>>> ------------------------------------", file=sys.stderr)
|
|
128
|
-
|
|
129
|
-
return transformed_source, visitor.namespaces, visitor.linemap
|
|
130
|
-
|
|
131
|
-
def instantiateBGP(input:Graph, solutionMappings, initialGraph:Graph=None):
|
|
132
|
-
if initialGraph == None:
|
|
133
|
-
initialGraph=Graph(base=input.base)
|
|
134
|
-
initialGraph.namespace_manager = input.namespace_manager
|
|
135
|
-
if solutionMappings is None:
|
|
136
|
-
return initialGraph
|
|
137
|
-
if isinstance(solutionMappings, dict):
|
|
138
|
-
solutionMappings = [solutionMappings]
|
|
139
|
-
if not isinstance(solutionMappings, list):
|
|
140
|
-
raise AssertionError("solutionMappings needs to be a dictionary, or list of dictionaries")
|
|
141
|
-
def instantiateTerm(t:Term, solutionMapping:SolutionMapping, rdfInstanceMapping:RDFInstanceMapping):
|
|
142
|
-
"""If the input term is a variable, replace it by its value in the solution mapping. Return None if this variable is not mapped. If the input term is a blank node, replace it by its value in the RDF instance mapping. Create one if it is not mapped."""
|
|
143
|
-
if isinstance(t, Variable):
|
|
144
|
-
if t in set(solutionMapping):
|
|
145
|
-
value = solutionMapping[t]
|
|
146
|
-
if value == None:
|
|
147
|
-
return None
|
|
148
|
-
if not isinstance(value, Node):
|
|
149
|
-
value = Literal(value)
|
|
150
|
-
return value
|
|
151
|
-
else:
|
|
152
|
-
return None
|
|
153
|
-
if isinstance(t, BNode):
|
|
154
|
-
if t in set(rdfInstanceMapping):
|
|
155
|
-
return rdfInstanceMapping[t]
|
|
156
|
-
else:
|
|
157
|
-
bnode = BNode()
|
|
158
|
-
rdfInstanceMapping[t] = bnode
|
|
159
|
-
return bnode
|
|
160
|
-
return t
|
|
161
|
-
for solutionMapping in solutionMappings:
|
|
162
|
-
rdfInstanceMapping={} # type: RDFInstanceMapping
|
|
163
|
-
for s,p,o in input:
|
|
164
|
-
s = instantiateTerm(s, solutionMapping, rdfInstanceMapping)
|
|
165
|
-
p = instantiateTerm(p, solutionMapping, rdfInstanceMapping)
|
|
166
|
-
o = instantiateTerm(o, solutionMapping, rdfInstanceMapping)
|
|
167
|
-
if s and p and o:
|
|
168
|
-
initialGraph.add((s, p, o))
|
|
169
|
-
return initialGraph
|
|
170
|
-
|
|
171
|
-
def hook_transform_source(source:str, filename:str, module, callback_params:Config) -> str:
|
|
172
|
-
transformed_source, namespaces, linemap = transform_source(source, filename)
|
|
173
|
-
callback_params.namespaces = namespaces
|
|
174
|
-
callback_params.linemap[filename] = linemap
|
|
175
|
-
return transformed_source
|
|
176
|
-
|
|
177
|
-
import_hook.create_hook(
|
|
178
|
-
callback_params=config,
|
|
179
|
-
transform_source=hook_transform_source,
|
|
180
|
-
hook_name=__name__,
|
|
181
|
-
extensions=[".ldpy"],
|
|
182
|
-
source_init=lambda:"import rdflib\n__base__ = None\n__namespaces__ = dict()\n"
|
|
183
|
-
)
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
#
|
|
2
|
-
# The MIT License (MIT)
|
|
3
|
-
#
|
|
4
|
-
# Copyright (c) 2022 by Maxime Lefrançois
|
|
5
|
-
#
|
|
6
|
-
# Permission is hereby granted, free of charge, to any person
|
|
7
|
-
# obtaining a copy of this software and associated documentation
|
|
8
|
-
# files (the "Software"), to deal in the Software without
|
|
9
|
-
# restriction, including without limitation the rights to use,
|
|
10
|
-
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
# copies of the Software, and to permit persons to whom the
|
|
12
|
-
# Software is furnished to do so, subject to the following
|
|
13
|
-
# conditions:
|
|
14
|
-
#
|
|
15
|
-
# The above copyright notice and this permission notice shall be
|
|
16
|
-
# included in all copies or substantial portions of the Software.
|
|
17
|
-
#
|
|
18
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
20
|
-
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
-
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
23
|
-
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
24
|
-
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
25
|
-
# OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
-
#
|
|
27
|
-
# Project : ldpython-parser; Linked-Data Python to Python/Micropython Rewriter
|
|
28
|
-
# https://gitlab.com/coswot/linked-data-python/ldpy
|
|
29
|
-
# Developed by : Maxime Lefrançois, maxime.lefrancois@emse.fr
|
|
30
|
-
#
|
|
31
|
-
from io import TextIOBase, StringIO
|
|
32
|
-
|
|
33
|
-
class IndentedStringWriter(TextIOBase):
|
|
34
|
-
|
|
35
|
-
def __init__(self, output:TextIOBase = None):
|
|
36
|
-
self._indent:int = 0
|
|
37
|
-
self.output = output or StringIO()
|
|
38
|
-
self.line = 0
|
|
39
|
-
|
|
40
|
-
def indent(self):
|
|
41
|
-
self._indent+=1
|
|
42
|
-
|
|
43
|
-
def dedent(self):
|
|
44
|
-
self._indent-=1
|
|
45
|
-
|
|
46
|
-
def write(self, s:str="", nl=True):
|
|
47
|
-
if nl:
|
|
48
|
-
self.output.write("\n" + " " * 4 * self._indent)
|
|
49
|
-
self.output.write(str(s))
|
|
50
|
-
self.line += str(s).count("\n") + (1 if nl else 0)
|
|
51
|
-
|
|
52
|
-
def getvalue(self):
|
|
53
|
-
return self.output.getvalue()
|
|
54
|
-
|