Skip to content

Commit 2ed12bb

Browse files
author
Marco Ratta
committed
1.0.0.0 code
1 parent 35bf4d6 commit 2ed12bb

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ keywords = [
1515
"rdflib",
1616
"sematic web",
1717
"knowledge graphs",
18-
"linked data"
18+
"linked data",
19+
"networkx"
1920
]
2021
authors = [
2122
{name = "Marco Ratta", email = "marco.ratta1@open.ac.uk"},

src/pysparql_anything/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
Manages the package's namespace, helps the installation process of the API.
33
44
Author: Marco Ratta
5-
Date: 16/12/2023
5+
Last Modified: 23/06/2025
66
"""
77

88
import requests
99
from github import Github
1010
from github.GithubException import RateLimitExceededException
1111
from pysparql_anything import utilities
12-
from pysparql_anything.__about__ import __SparqlAnything__, __uri__, __version__
12+
from pysparql_anything.__about__ import (
13+
__SparqlAnything__, __uri__, __version__
14+
)
1315

1416
# Checks if SPARQL Anything is not installed. Installs it if so.
1517
try:
@@ -33,5 +35,9 @@
3335
print(f' A {type(exc)} exception has been raised. \n'
3436
+ 'Installation unsuccessful!!!')
3537
raise
38+
3639
# Launches the JVM
37-
from pysparql_anything.sparql_anything import SparqlAnything
40+
import pysparql_anything.sparql_anything
41+
42+
# Type Aliases
43+
SparqlAnything = pysparql_anything.sparql_anything.SparqlAnything

src/pysparql_anything/cli.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""
22
This module contains the CLI for the SPARQL Anything tool.
33
4-
Author: Marco ratta
5-
Date: 18/06/2025
4+
Author: Marco Ratta
5+
Last Modified: 23/06/2025
66
"""
77

88
from argparse import ArgumentParser, SUPPRESS
99
from pysparql_anything.args_handlers import transform_cli_args
1010
from pysparql_anything.sparql_anything_reflection import (
1111
SPARQLAnythingReflection
1212
)
13+
from pysparql_anything.__about__ import __SparqlAnything__
14+
from pysparql_anything.utilities import remove_jar
1315

1416

1517
def setup_parser(a_parser: ArgumentParser) -> ArgumentParser:
@@ -91,6 +93,9 @@ def setup_parser(a_parser: ArgumentParser) -> ArgumentParser:
9193
a_parser.add_argument(
9294
"-e", "--explain", help="OPTIONAL - Explain query execution"
9395
)
96+
a_parser.add_argument(
97+
"--uninstall", help="Uninstalls PySPARQL-Anything from the system."
98+
)
9499
return a_parser
95100

96101

@@ -105,7 +110,12 @@ def main() -> None:
105110
parser = setup_parser(parser)
106111
# Process the arguments for the Java main class.
107112
args = parser.parse_args()
108-
java_args = transform_cli_args(args)
109-
# Run the query
110-
sa = SPARQLAnythingReflection(java_args[0])
111-
sa.main(java_args[1])
113+
if 'uninstall' in args:
114+
print(f'Uninstalling SPARQL-Anything {__SparqlAnything__} JAR.')
115+
remove_jar()
116+
print(f"SPARQL Anything {__SparqlAnything__} JAR sucessfully removed.")
117+
else:
118+
java_args = transform_cli_args(args)
119+
# Run the query
120+
sa = SPARQLAnythingReflection(java_args[0])
121+
sa.main(java_args[1])

src/pysparql_anything/sparql_anything_reflection.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
to Python users.
66
77
Author: Marco Ratta
8-
Date: 18/12/2023
8+
Last Modified: 22/06/2025
99
"""
1010

1111
from collections.abc import Sequence
@@ -57,7 +57,7 @@ def __init__(
5757
self.System = autoclass("java.lang.System")
5858
# Redirect Java STDERR
5959
self.err_bs = self.BAOS()
60-
self.err_ps = self.PrintStream(self.err_bs, False)
60+
self.err_ps = self.PrintStream(self.err_bs, True)
6161
self.System.setErr(self.err_ps)
6262
# Create the SPARQLAnything class object
6363
self.reflection = autoclass(__jarMainPath__)
@@ -98,13 +98,17 @@ def call_main(self, args: list[str]) -> SPARQLAnythingQueryOutput:
9898
"""
9999
# Capture STDOUT locally
100100
baos = self.BAOS()
101-
ps = self.PrintStream(baos, False)
101+
ps = self.PrintStream(baos, True)
102102
old_ps = self.System.out
103103
self.System.setOut(ps)
104104
# Call Sparql Anything main method
105-
self.reflection.main(args)
105+
try:
106+
self.reflection.main(args)
107+
except Exception as e:
108+
self.System.setOut(old_ps)
109+
self.err_bs.reset()
110+
raise e
106111
# Put things back
107-
self.System.out.flush()
108112
self.System.setOut(old_ps)
109113
# Convert streams to strings
110114
sa_output = baos.toString()

src/pysparql_anything/utilities.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Interacts with the SPARQL Anything GitHub repository.
55
66
Author: Marco Ratta
7-
Date: 29/02/2024
7+
Last Modified: 23/06/2025
88
"""
99

1010
import os
@@ -28,7 +28,7 @@ def has_jar():
2828
return False
2929

3030

31-
def get_release_uri(ghub: Github, uri: str, version: str) -> str:
31+
def get_release_url(ghub: Github, uri: str, version: str) -> str:
3232
"""
3333
Retrieves the download url for latest SPARQL Anything release.\n
3434
Args:\n
@@ -43,12 +43,12 @@ def get_release_uri(ghub: Github, uri: str, version: str) -> str:
4343
"""
4444
try:
4545
repo = ghub.get_repo(uri)
46-
release = repo.get_release(version)
47-
jar = None
46+
release = repo.get_release(version)
47+
url = ""
4848
for asset in release.assets:
4949
if asset.name == f'sparql-anything-{version}.jar':
50-
jar = asset
51-
return jar.browser_download_url
50+
url = asset.browser_download_url
51+
return url
5252
except RateLimitExceededException as exc:
5353
print('WARNING !!! get_release_url() raised a '
5454
+ f'{type(exc)} exception and passed it on.')
@@ -73,7 +73,7 @@ def download_sparql_anything(ghub: Github, uri: str, version: str) -> None:
7373
path2jar = os.path.join(
7474
get_module_path(), f'sparql-anything-{version}.jar'
7575
)
76-
dl_link = get_release_uri(ghub, uri, version)
76+
dl_link = get_release_url(ghub, uri, version)
7777
request = requests.get(dl_link, stream=True, timeout=10.0)
7878
length = int(request.headers.get('content-length', 0))
7979
with open(path2jar, 'wb') as jar:
@@ -123,16 +123,13 @@ def get_path2jar():
123123
return path
124124

125125

126-
def remove_sparql_anything():
126+
def remove_jar():
127127
"""
128128
Removes the SPARQL Anything jar from the installation folder.\n
129129
Raises:\n
130130
FileNotFoundError.
131131
"""
132132
try:
133133
os.remove(get_path2jar())
134-
print("SPARQL Anything sucessfully removed")
135134
except FileNotFoundError as err:
136-
print('WARNING !!! remove_sparql_anything() raised a '
137-
+ f'{type(err)} exception and re-raised it.')
138-
raise
135+
raise err

0 commit comments

Comments
 (0)