-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
64 lines (52 loc) · 3.49 KB
/
Copy path__init__.py
File metadata and controls
64 lines (52 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Python3 tool for RDF triples extraction."""
from pathlib import Path
def rdfization(*, project_path: Path = None, project_pkgs: Path = None, project_deps: Path = None,
pypi_target: str = None, output_dir: Path = None, download_dir: Path = None, python3_exec: Path = None,
python3_src: Path = None):
"""The function to be called to start the RDF triples extraction for a Python3 project.
Args:
project_path (Path): the path to the folder containing the project source.
project_pkgs (Path): the path to the folder containing the top level packages paths of the project distribution.
project_deps (Path): the path to the folder containing the top level packages paths of the project dependencies.
pypi_target (str): the name of the project in the index, eventually with the specific version, such as in
'<project_name>' or '<project_name>==<version>' (using the 'version matching' clause, '==').
output_dir (Path): the path to the folder in which to store the output RDF triples.
download_dir (Path): the path to the folder in which to download the needed files.
python3_exec (Path): the path to a locally installed 'Python3 executable', used to run 'pip' functionalities.
python3_src (Path): the path to the folder containing the Python3 standard library source code.
"""
# NOTE in-library imports should stay here inside the function, or they will be executed before calling the
# function, for example, triggering the loading of the base ontology before even running the command parser
from codeontology import LOGGER
from codeontology.ontology import ontology
from codeontology.rdfization.python3.explore import Project
from codeontology.rdfization.python3.extract.serializer import Serializer
from codeontology.rdfization.python3.explore.utils import ProjectHandler, PySourceHandler
# Retrieve project files
assert output_dir and download_dir and python3_exec
project_handler = ProjectHandler(python3_exec)
py_source_handler = PySourceHandler()
# If a project on PyPI has been specified, download it locally
if pypi_target:
assert not project_path and not project_pkgs and not project_deps
project_path = project_handler.download_source_from_pypi(pypi_target, download_dir)
# Install the local project, if necessary
if not project_pkgs and not project_deps:
install_dir = download_dir.joinpath("install")
project_name, project_pkgs, project_deps = project_handler.install_local_project(project_path, install_dir)
else:
project_name = project_handler.get_local_project_name(project_path)
# Get the Python source if necessary
if not python3_src:
py_version = py_source_handler.get_py_executable_version(python3_exec)
python3_src = py_source_handler.download_python_source(py_version, download_dir)
# Reconstruct the project structure
project = Project(project_name, project_path, project_pkgs, project_deps, python3_src)
# Extract the triples from the project structure
# !!! The ontology is global, triples will be stored there automatically! That's why there is no need for a object
# return or ontology parameter. This should be solved, since I don't like having a global ontology!
Serializer(project)
# Store everything
save_file = str(output_dir.joinpath(project_name+".nt"))
LOGGER.info(f"Saving triples at '{save_file}'.")
ontology.save(save_file, format="ntriples")