Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"kevinrose.vsc-python-indent",
"ms-python.python",
"njpwerner.autodocstring",
"donjayamanne.python-environment-manager"
]
}
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0",
"configurations": [
{
"name": "Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Parse RDF File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [
"-f",
"tests/data/doap.rdf"
]
},
{
"name": "Parse n3 File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [
"-f",
"tests/data/doap.n3"
]
}
]
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# be-graph-local-py
Creation and editing of local graph files for publication and automated discovery

Creation and editing of local graph files for publication and automated discovery.

## Getting Started

1. Install required packages with Poetry.
2. Run `python main.py -f "tests/data/doap.rdf"` to run the program on an example file.

## Poetry Installation

To install Poetry, follow these steps:

1. Open your terminal or command prompt.
2. Run the following command to install Poetry:

```bash
curl -sSL https://install.python-poetry.org | python3 -
```

If you don't have `curl` installed, you can also use `wget`:

```bash
wget -O - https://install.python-poetry.org | python3 -
```

3. Once the installation is complete, you can verify it by running:

```bash
poetry --version
```

This should display the version number of Poetry installed on your system.

Congratulations! You have successfully installed Poetry. You can now use it to manage your Python dependencies and projects.

For more information on how to use Poetry, refer to the [official documentation](https://python-poetry.org/docs/).

## Tests

Run `pytest` to run all the tests in the `tests/` directory.
1 change: 1 addition & 0 deletions be_graph_local_py/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .rdf import read_rdf, get_project_name
1 change: 1 addition & 0 deletions be_graph_local_py/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .arguments import Arguments
14 changes: 14 additions & 0 deletions be_graph_local_py/cli/arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import argparse


def Arguments() -> argparse.ArgumentParser:
"""
Create and configure the argument parser for the command-line interface.

Returns:
argparse.ArgumentParser: The configured argument parser.
"""
parser = argparse.ArgumentParser(description="Process some RDF data.")
parser.add_argument('-f', metavar="Files", type=str, required=False, help="Parse RDF or n3 file.")
parser.add_argument('files', metavar="Files", type=str, nargs='*', help='Parse RDF or n3 file.')
return parser
1 change: 1 addition & 0 deletions be_graph_local_py/rdf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .rdf import read_rdf, get_project_name
41 changes: 41 additions & 0 deletions be_graph_local_py/rdf/rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import List
from rdflib import Graph, Namespace, Literal, XSD


def read_rdf(file_path) -> Graph:
"""
Reads an RDF file and returns the parsed RDF graph.

Args:
file_path (str): The path to the RDF file.

Returns:
rdflib.Graph: The parsed RDF graph.

"""
rdfGraph = Graph()
rdfGraph.parse(file_path, format='xml')
return rdfGraph


def get_project_name(rdfGraph: Graph) -> str:
"""
Get the name of the project.

Args:
rdfGraph (Graph): The RDF graph.

Returns:
str: The name of the project.
"""
DOAP = Namespace("http://usefulinc.com/ns/doap#")
query = """
SELECT ?name WHERE {
?project a doap:Project .
?project doap:name ?name .
}
"""
result = rdfGraph.query(query, initNs={'doap': DOAP})
for row in result:
return str(row.name)
return "Project name not found"
1 change: 1 addition & 0 deletions be_graph_local_py/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import *
9 changes: 9 additions & 0 deletions be_graph_local_py/utils/argparse_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import argparse

parser = argparse.ArgumentParser(description="Process some RDF data.")
parser.add_argument('-f', metavar="Files", type=str, required=False, help="Parse RDF or n3 file.")
parser.add_argument('files', metavar="Files", type=str, nargs='*', help='Parse RDF or n3 file.')
# parser.add_argument('--sum', dest='accumulate', action='store_const', required=False,
# const=sum, default=max,
# help='sum the integers (default: find the max)')
parser.parse_args()
110 changes: 110 additions & 0 deletions be_graph_local_py/utils/rdflib_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from rdflib import Graph, URIRef, Literal, XSD
from rdflib.namespace import RDF, FOAF


def example_01():
"""
Creates an RDF graph, adds triples to the graph, and queries the graph for name and age information.

Returns:
None
"""
# Create a new RDF graph
g = Graph()

# Define some URIs
person_uri = URIRef("http://example.org/person/JohnDoe")
name_uri = FOAF.name
age_uri = FOAF.age

# Add triples to the graph
g.add((person_uri, RDF.type, FOAF.Person))
g.add((person_uri, name_uri, Literal("John Doe", datatype=XSD.string)))
g.add((person_uri, age_uri, Literal(42, datatype=XSD.integer)))

# Query the graph
query = """
SELECT ?name ?age
WHERE {
?person a foaf:Person .
?person foaf:name ?name .
?person foaf:age ?age .
}
"""
results = g.query(query)

# Print the results
for row in results:
print(f"Name: {row.name}, Age: {row.age}")


def example_02():
"""
Read a graph from a file.

This function creates a Graph object and parses an RDF file hosted on the Internet.
It then loops through each triple in the graph and checks if there is at least one triple.
Finally, it prints the number of triples in the graph and the entire graph in the RDF Turtle format.
"""
# Create a Graph
g = Graph()

# Parse in an RDF file hosted on the Internet
g.parse("http://www.w3.org/People/Berners-Lee/card")

# Loop through each triple in the graph (subj, pred, obj)
for subj, pred, obj in g:
# Check if there is at least one triple in the Graph
if (subj, pred, obj) not in g:
raise Exception("It better be!")

# Print the number of "triples" in the Graph
print(f"Graph g has {len(g)} statements.")
# Prints: Graph g has 86 statements.

# Print out the entire Graph in the RDF Turtle format
print(g.serialize(format="turtle"))


def example_03():
"""
This function demonstrates how to create a Graph using RDFLib and add triples to it.
It also shows how to iterate over the triples in the graph and print them out.
"""
# Create a Graph
g = Graph()

# Create an RDF URI node to use as the subject for multiple triples
donna = URIRef("http://example.org/donna")

# Add triples using store's add() method.
g.add((donna, RDF.type, FOAF.Person))
g.add((donna, FOAF.nick, Literal("donna", lang="en")))
g.add((donna, FOAF.name, Literal("Donna Fales")))
g.add((donna, FOAF.mbox, URIRef("mailto:donna@example.org")))

# Add another person
ed = URIRef("http://example.org/edward")

# Add triples using store's add() method.
g.add((ed, RDF.type, FOAF.Person))
g.add((ed, FOAF.nick, Literal("ed", datatype=XSD.string)))
g.add((ed, FOAF.name, Literal("Edward Scissorhands")))
g.add((ed, FOAF.mbox, Literal("e.scissorhands@example.org", datatype=XSD.anyURI)))

# Iterate over triples in store and print them out.
print("--- printing raw triples ---")
for s, p, o in g:
print((s, p, o))

# For each foaf:Person in the store, print out their mbox property's value.
print("--- printing mboxes ---")
for person in g.subjects(RDF.type, FOAF.Person):
for mbox in g.objects(person, FOAF.mbox):
print(mbox)

# Bind the FOAF namespace to a prefix for more readable output
g.bind("foaf", FOAF)

# print all the data in the Notation3 format
print("--- printing mboxes ---")
Empty file.
30 changes: 30 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
Copyright (C) 2024 Project-ACT

This file is part of be-graph-local-py.

be-graph-local-py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

be-graph-local-py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with be-graph-local-py. If not, see <https://www.gnu.org/licenses/>.
-->

# Backend Graph Local Python Documents

In order to be inclusive across language barriers, the documentation for this project is made translatable into other
languages using the [GitLocalize](https://gitlocalize.com/) tools which have, as of this writing, been made available in
the organization as an application to use for any repository.

## Project DOAP source

The general source file for the project DOAP file, if defined declaratively using languages such as Notation3 (.n3) or
Terse Triple Language (.ttl) may be found in this directory, with optional localized terms such as description, short
description, and others found in language subdirectories (en, es, etc).
30 changes: 30 additions & 0 deletions doc/en/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
Copyright (C) 2024 Project-ACT

This file is part of be-graph-local-py.

be-graph-local-py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

be-graph-local-py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with be-graph-local-py. If not, see <https://www.gnu.org/licenses/>.
-->

# Backend Graph Local Python Documents

In order to be inclusive across language barriers, the documentation for this project is made translatable into other
languages using the [GitLocalize](https://gitlocalize.com/) tools which have, as of this writing, been made available in
the organization as an application to use for any repository.

## Project DOAP source

The general source file for the project DOAP file, if defined declaratively using languages such as Notation3 (.n3) or
Terse Triple Language (.ttl) may be found in this directory, with optional localized terms such as description, short
description, and others found in language subdirectories (en, es, etc).
21 changes: 21 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from be_graph_local_py.cli.arguments import Arguments
from be_graph_local_py.rdf import read_rdf
from be_graph_local_py.rdf.rdf import get_project_name


def main():
"""
Entry point of the program.
"""
arguments = Arguments().parse_args()

# Read an RDF file from the path provided by the "f" argument.
rdf_graph = read_rdf(arguments.f)

# Print project name.
result = get_project_name(rdf_graph)
print(result)


if __name__ == "__main__":
main()
Loading