Skip to content

Commit

Permalink
valid URI generation for saving file working
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropaulofb committed Sep 21, 2023
1 parent ea75c72 commit b5638d9
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 68 deletions.
2 changes: 1 addition & 1 deletion docs/_sources/autoapi/json2graph/decode/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Attributes

.. py:function:: decode_all_ontouml_json2graph()
Decode multiple OntoUML JSON files.
Decode multiple OntoUML JSON files in batch mode.

This function processes a directory of OntoUML JSON files and converts each file into a corresponding
knowledge graph using the specified options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.. autoapi-nested-parse::

Decoding messages to be displayed to users must be concentrated in this module whenever possible.
Warning messages generated during the decoding process to be displayed to users must be concentrated in this module whenever possible.



Expand Down
33 changes: 30 additions & 3 deletions docs/_sources/autoapi/json2graph/modules/utils_graph/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Functions
json2graph.modules.utils_graph.ontouml_ref
json2graph.modules.utils_graph.load_ontouml_vocabulary
json2graph.modules.utils_graph.load_graph_safely
json2graph.modules.utils_graph.fix_uri
json2graph.modules.utils_graph.rename_uriref_resource



Expand Down Expand Up @@ -65,15 +67,40 @@ Attributes
:rtype: Graph


.. py:function:: load_graph_safely(ontology_file, format = 'not_provided')
.. py:function:: load_graph_safely(ontology_file, out_format = 'not_provided')
Safely load graph from file to working memory using arguments provided by the user, which are the file path and (optionally) the file type.

:param ontology_file: Path to the ontology file to be loaded into the working memory.
:type ontology_file: str
:param format: Optional argument. Format of the file to be loaded.
:type format: str
:param out_format: Optional argument. Format of the file to be loaded.
:type out_format: str
:return: RDFLib graph loaded as object.
:rtype: Graph


.. py:function:: fix_uri(url_string)
Received a valid URL, but invalid URI string and converts to a valid URL and valid URI string.

This function takes a URL string, parses it, encodes non-URI characters, and reconstructs a valid URI.

:param url_string: The URL string to be fixed.
:type url_string: str
:return: The resulting valid URI string.
:rtype: str


.. py:function:: rename_uriref_resource(graph, old_resource, new_resource)
Rename a URIRef resource of an RDF graph by replacing it to a new one with a different name.

:param graph: The RDF graph that contains the resource to be renamed.
:type graph: Graph
:param old_resource: The old resource to be replaced.
:type old_resource: URIRef
:param new_resource: The new resource to replace the old one.
:type new_resource: URIRef
:return: None


4 changes: 2 additions & 2 deletions docs/autoapi/json2graph/decode/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ <h3>Functions<a class="headerlink" href="#functions" title="Link to this heading
<td><p>Save the ontology graph received as argument into a file using the syntax defined by the user.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#json2graph.decode.decode_all_ontouml_json2graph" title="json2graph.decode.decode_all_ontouml_json2graph"><code class="xref py py-obj docutils literal notranslate"><span class="pre">decode_all_ontouml_json2graph</span></code></a>()</p></td>
<td><p>Decode multiple OntoUML JSON files.</p></td>
<td><p>Decode multiple OntoUML JSON files in batch mode.</p></td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -173,7 +173,7 @@ <h3>Attributes<a class="headerlink" href="#attributes" title="Link to this headi
<dl class="py function">
<dt class="sig sig-object py" id="json2graph.decode.decode_all_ontouml_json2graph">
<span class="sig-prename descclassname"><span class="pre">json2graph.decode.</span></span><span class="sig-name descname"><span class="pre">decode_all_ontouml_json2graph</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#json2graph.decode.decode_all_ontouml_json2graph" title="Link to this definition"></a></dt>
<dd><p>Decode multiple OntoUML JSON files.</p>
<dd><p>Decode multiple OntoUML JSON files in batch mode.</p>
<p>This function processes a directory of OntoUML JSON files and converts each file into a corresponding
knowledge graph using the specified options.
The output graphs are saved in the output directory chosen by the user as argument.</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/autoapi/json2graph/modules/messages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@

<section id="module-json2graph.modules.messages">
<span id="json2graph-modules-messages"></span><h1><a class="reference internal" href="#module-json2graph.modules.messages" title="json2graph.modules.messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json2graph.modules.messages</span></code></a><a class="headerlink" href="#module-json2graph.modules.messages" title="Link to this heading"></a></h1>
<p>Decoding messages to be displayed to users must be concentrated in this module whenever possible.</p>
<p>Warning messages generated during the decoding process to be displayed to users must be concentrated in this module whenever possible.</p>
<section id="module-contents">
<h2>Module Contents<a class="headerlink" href="#module-contents" title="Link to this heading"></a></h2>
<section id="functions">
Expand Down
51 changes: 48 additions & 3 deletions docs/autoapi/json2graph/modules/utils_graph/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ <h3>Functions<a class="headerlink" href="#functions" title="Link to this heading
<tr class="row-even"><td><p><a class="reference internal" href="#json2graph.modules.utils_graph.load_ontouml_vocabulary" title="json2graph.modules.utils_graph.load_ontouml_vocabulary"><code class="xref py py-obj docutils literal notranslate"><span class="pre">load_ontouml_vocabulary</span></code></a>([enable_remote])</p></td>
<td><p>Load the OntoUML Vocabulary to the working memory.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#json2graph.modules.utils_graph.load_graph_safely" title="json2graph.modules.utils_graph.load_graph_safely"><code class="xref py py-obj docutils literal notranslate"><span class="pre">load_graph_safely</span></code></a>(ontology_file[, format])</p></td>
<tr class="row-odd"><td><p><a class="reference internal" href="#json2graph.modules.utils_graph.load_graph_safely" title="json2graph.modules.utils_graph.load_graph_safely"><code class="xref py py-obj docutils literal notranslate"><span class="pre">load_graph_safely</span></code></a>(ontology_file[, out_format])</p></td>
<td><p>Safely load graph from file to working memory using arguments provided by the user, which are the file path and (optionally) the file type.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#json2graph.modules.utils_graph.fix_uri" title="json2graph.modules.utils_graph.fix_uri"><code class="xref py py-obj docutils literal notranslate"><span class="pre">fix_uri</span></code></a>(url_string)</p></td>
<td><p>Received a valid URL, but invalid URI string and converts to a valid URL and valid URI string.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#json2graph.modules.utils_graph.rename_uriref_resource" title="json2graph.modules.utils_graph.rename_uriref_resource"><code class="xref py py-obj docutils literal notranslate"><span class="pre">rename_uriref_resource</span></code></a>(graph, old_resource, new_resource)</p></td>
<td><p>Rename a URIRef resource of an RDF graph by replacing it to a new one with a different name.</p></td>
</tr>
</tbody>
</table>
</section>
Expand Down Expand Up @@ -167,13 +173,13 @@ <h3>Attributes<a class="headerlink" href="#attributes" title="Link to this headi

<dl class="py function">
<dt class="sig sig-object py" id="json2graph.modules.utils_graph.load_graph_safely">
<span class="sig-prename descclassname"><span class="pre">json2graph.modules.utils_graph.</span></span><span class="sig-name descname"><span class="pre">load_graph_safely</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ontology_file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'not_provided'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json2graph.modules.utils_graph.load_graph_safely" title="Link to this definition"></a></dt>
<span class="sig-prename descclassname"><span class="pre">json2graph.modules.utils_graph.</span></span><span class="sig-name descname"><span class="pre">load_graph_safely</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ontology_file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">out_format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'not_provided'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json2graph.modules.utils_graph.load_graph_safely" title="Link to this definition"></a></dt>
<dd><p>Safely load graph from file to working memory using arguments provided by the user, which are the file path and (optionally) the file type.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>ontology_file</strong> (<em>str</em>) – Path to the ontology file to be loaded into the working memory.</p></li>
<li><p><strong>format</strong> (<em>str</em>) – Optional argument. Format of the file to be loaded.</p></li>
<li><p><strong>out_format</strong> (<em>str</em>) – Optional argument. Format of the file to be loaded.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
Expand All @@ -185,6 +191,45 @@ <h3>Attributes<a class="headerlink" href="#attributes" title="Link to this headi
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="json2graph.modules.utils_graph.fix_uri">
<span class="sig-prename descclassname"><span class="pre">json2graph.modules.utils_graph.</span></span><span class="sig-name descname"><span class="pre">fix_uri</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url_string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json2graph.modules.utils_graph.fix_uri" title="Link to this definition"></a></dt>
<dd><p>Received a valid URL, but invalid URI string and converts to a valid URL and valid URI string.</p>
<p>This function takes a URL string, parses it, encodes non-URI characters, and reconstructs a valid URI.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>url_string</strong> (<em>str</em>) – The URL string to be fixed.</p>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p>The resulting valid URI string.</p>
</dd>
<dt class="field-odd">Return type<span class="colon">:</span></dt>
<dd class="field-odd"><p>str</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="json2graph.modules.utils_graph.rename_uriref_resource">
<span class="sig-prename descclassname"><span class="pre">json2graph.modules.utils_graph.</span></span><span class="sig-name descname"><span class="pre">rename_uriref_resource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">graph</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">old_resource</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_resource</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json2graph.modules.utils_graph.rename_uriref_resource" title="Link to this definition"></a></dt>
<dd><p>Rename a URIRef resource of an RDF graph by replacing it to a new one with a different name.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>graph</strong> (<em>Graph</em>) – The RDF graph that contains the resource to be renamed.</p></li>
<li><p><strong>old_resource</strong> (<em>URIRef</em>) – The old resource to be replaced.</p></li>
<li><p><strong>new_resource</strong> (<em>URIRef</em>) – The new resource to replace the old one.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p>None</p>
</dd>
<dt class="field-odd">Return type<span class="colon">:</span></dt>
<dd class="field-odd"><p>None</p>
</dd>
</dl>
</dd></dl>

</section>
</section>
</section>
Expand Down
11 changes: 11 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ <h1 id="index">Index</h1>
| <a href="#C"><strong>C</strong></a>
| <a href="#D"><strong>D</strong></a>
| <a href="#E"><strong>E</strong></a>
| <a href="#F"><strong>F</strong></a>
| <a href="#G"><strong>G</strong></a>
| <a href="#I"><strong>I</strong></a>
| <a href="#J"><strong>J</strong></a>
Expand Down Expand Up @@ -177,6 +178,14 @@ <h2 id="E">E</h2>
</ul></td>
</tr></table>

<h2 id="F">F</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="autoapi/json2graph/modules/utils_graph/index.html#json2graph.modules.utils_graph.fix_uri">fix_uri() (in module json2graph.modules.utils_graph)</a>
</li>
</ul></td>
</tr></table>

<h2 id="G">G</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
Expand Down Expand Up @@ -576,6 +585,8 @@ <h2 id="P">P</h2>
<h2 id="R">R</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="autoapi/json2graph/modules/utils_graph/index.html#json2graph.modules.utils_graph.rename_uriref_resource">rename_uriref_resource() (in module json2graph.modules.utils_graph)</a>
</li>
<li><a href="autoapi/json2graph/modules/errors/index.html#json2graph.modules.errors.report_error_end_of_switch">report_error_end_of_switch() (in module json2graph.modules.errors)</a>
</li>
<li><a href="autoapi/json2graph/modules/errors/index.html#json2graph.modules.errors.report_error_invalid_parameter">report_error_invalid_parameter() (in module json2graph.modules.errors)</a>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

79 changes: 28 additions & 51 deletions json2graph/modules/input_output.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""IO functions used in diverse occasions."""
import json
import os
import urllib

from icecream import ic
from rdflib import Graph
from rdflib import Graph, URIRef

from .errors import report_error_io_read, report_error_io_write
from .logger import initialize_logger
from .utils_graph import rename_uriref_resource, fix_uri

LOGGER = initialize_logger()

Expand Down Expand Up @@ -48,34 +47,6 @@ def safe_load_json_file(json_path: str) -> dict:
return json_data


def fix_uri(url_string) -> str:
"""To do."""
# Step 1: Parse the URL
parsed_url = urllib.parse.urlparse(url_string)

# Step 2: URL Encode Non-URI Characters
# Encode non-ASCII characters and spaces in the path and query components
encoded_path = urllib.parse.quote(parsed_url.path)
encoded_query = urllib.parse.quote(parsed_url.query)
encoded_scheme = urllib.parse.quote(parsed_url.scheme)
encoded_netloc = urllib.parse.quote(parsed_url.netloc)
encoded_fragment = urllib.parse.quote(parsed_url.fragment)

# Step 3: Reconstruct the URI
valid_uri = urllib.parse.urlunparse(
(
encoded_scheme,
encoded_netloc,
encoded_path,
"", # Empty string for params
encoded_query,
encoded_fragment,
)
)

return valid_uri


def safe_write_graph_file(ontouml_graph: Graph, output_file_path: str, syntax: str) -> None:
"""Safely saves the graph into a file in the informed destination with the desired syntax.
Expand All @@ -86,26 +57,32 @@ def safe_write_graph_file(ontouml_graph: Graph, output_file_path: str, syntax: s
:param syntax: Syntax to be used for saving the ontology file.
:type syntax: str
"""
# Regular case (all URIRef resources have valid URIs)
try:
ontouml_graph.serialize(destination=output_file_path, encoding="utf-8", format=syntax)
except Exception:
for s, p, o in ontouml_graph:
if "URIRef" in str(type(s)):
old_s = s.toPython()
new_s = fix_uri(old_s)
if old_s != new_s:
ic(old_s, new_s)
if "URIRef" in str(type(p)):
old_p = p.toPython()
new_p = fix_uri(old_p)
if old_p != new_p:
ic(old_p, new_p)
if "URIRef" in str(type(o)):
old_o = o.toPython()
new_o = fix_uri(old_o)
if old_o != new_o:
ic(old_o, new_o)
# ontouml_graph.serialize(destination=output_file_path, encoding="utf-8", format=syntax)
except OSError as errorOS:
file_description = "output graph file - OSError"
report_error_io_write(output_file_path, file_description, errorOS)
# Special treatment for the cases where the graph has URIRef resources that are not valid URIs
try:
for old_s, old_p, old_o in ontouml_graph:
if "URIRef" in str(type(old_s)):
old_s_name = old_s.toPython()
new_s = URIRef(fix_uri(old_s_name))
new_s_name = new_s.toPython()
if old_s_name != new_s_name:
rename_uriref_resource(ontouml_graph, old_s, new_s)
if "URIRef" in str(type(old_p)):
old_p_name = old_p.toPython()
new_p = URIRef(fix_uri(old_p_name))
new_p_name = new_p.toPython()
if old_p_name != new_p_name:
rename_uriref_resource(ontouml_graph, old_p, new_p)
if "URIRef" in str(type(old_o)):
old_o_name = old_o.toPython()
new_o = URIRef(fix_uri(old_o_name))
new_o_name = new_o.toPython()
if old_o_name != new_o_name:
rename_uriref_resource(ontouml_graph, old_o, new_o)
ontouml_graph.serialize(destination=output_file_path, encoding="utf-8", format=syntax)
except OSError as errorOS:
file_description = "output graph file"
report_error_io_write(output_file_path, file_description, errorOS)
Loading

0 comments on commit b5638d9

Please sign in to comment.