From 731b57f326894a155783e19b82ebfeb31b4c2fc6 Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Sat, 6 Feb 2021 11:13:53 +0530 Subject: [PATCH 1/6] Added sphinx documentation --- docs/Makefile | 20 ++++ docs/make.bat | 35 +++++++ docs/source/agent.rst | 6 ++ docs/source/collection_paginator.rst | 5 + docs/source/conf.py | 55 +++++++++++ docs/source/helpers.rst | 6 ++ docs/source/index.rst | 140 +++++++++++++++++++++++++++ docs/source/querying_mechanism.rst | 6 ++ 8 files changed, 273 insertions(+) create mode 100644 docs/Makefile create mode 100644 docs/make.bat create mode 100644 docs/source/agent.rst create mode 100644 docs/source/collection_paginator.rst create mode 100644 docs/source/conf.py create mode 100644 docs/source/helpers.rst create mode 100644 docs/source/index.rst create mode 100644 docs/source/querying_mechanism.rst diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..d0c3cbf --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = source +BUILDDIR = build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..6247f7e --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=source +set BUILDDIR=build + +if "%1" == "" goto help + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/docs/source/agent.rst b/docs/source/agent.rst new file mode 100644 index 0000000..27e9017 --- /dev/null +++ b/docs/source/agent.rst @@ -0,0 +1,6 @@ +agent +============================================= + +.. automodule:: hydra_agent.agent + :members: + diff --git a/docs/source/collection_paginator.rst b/docs/source/collection_paginator.rst new file mode 100644 index 0000000..179911b --- /dev/null +++ b/docs/source/collection_paginator.rst @@ -0,0 +1,5 @@ +collection_paginator +============================================= +.. automodule:: hydra_agent.collection_paginator + :members: + diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..dc148f8 --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,55 @@ +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +import os +import sys +sys.path.insert(0, os.path.abspath('../..')) + + +# -- Project information ----------------------------------------------------- + +project = 'hydra-python-agent' +copyright = '2021, Ashish Rao' +author = 'Ashish Rao' + +# The full version, including alpha/beta/rc tags +release = '0.0.1' + + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = ['sphinx.ext.autodoc','sphinx.ext.coverage', +'sphinx.ext.napoleon'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = [] + + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = 'sphinx_rtd_theme' + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] \ No newline at end of file diff --git a/docs/source/helpers.rst b/docs/source/helpers.rst new file mode 100644 index 0000000..a3d723e --- /dev/null +++ b/docs/source/helpers.rst @@ -0,0 +1,6 @@ +helpers +============================================= + +.. automodule:: hydra_agent.helpers + :members: + diff --git a/docs/source/index.rst b/docs/source/index.rst new file mode 100644 index 0000000..33bcdb4 --- /dev/null +++ b/docs/source/index.rst @@ -0,0 +1,140 @@ +.. hydra-python-agent documentation master file, created by + sphinx-quickstart on Mon Feb 1 16:42:29 2021. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to hydra-python-agent's documentation! +============================================== + +hydra-python-agent is a `smart Hydra client `__ implemented in +Python which works with hydrus. Reference implementation is +`Heracles.ts `__. Smart clients are generic automated clients +that establish resilient connected data networks leveraging +knowledge graphs. + +agent module +------------ +The agent is used in order to: + 1. Provide a seamless Client that can be used to communicate with Hydra APIs. + 2. Cache metadata from the Hydra server it connects to, to allow querying on the client-side. + 3. Maintain a syncrhonization mechanism which makes sure cached resources are consistent. + 4. The graph can be queried using OpenCypher. + +The agent module contains a main Agent class. The agent module uses a redis caching layer to store +the generated apidoc and allow efficient querying. +This class inherits classes from the socketio module and the Session class from the requests module. +This class is first initialized with the main entrypoint url. +(Example) In this case, the entrypoint_url is "http://localhost:8080/serverapi". +A connection to the redis server is initialized using the redis modules (present in the redis_core files) +and a connection with the hydrus server is established at the entrypoint url provided using socketio. + +The Agent class has various functions. Some commonly used functions are explained below: + +1) fetch_apidoc() + This function is used to generate the apidoc for the entrypoint_url initialized and the name of the api + which is extracted from the entrypoint_url. This apidoc is generated by passing the apidoc present in hydrus + through the doc_maker module from hydra-python-core. +2) get() + This function is used to handle a GET request to the url mentioned as a parameter. The function returns an apidoc + for the respective resource from the redis graph in a JSON format. + +3) post() + This function is used to handle a POST request to the url mentioned as a parameter. It is used to update the resource at the specified + url. + +4) put() + This function is used to handle a PUT request to the url mentioned as a parameter. It is used to add a resource at the specified url. + +The final goal is to create a Client that can connected to multiple hydrus servers and operate between them while caching information in +a graph-based database(Redis). +This should enable the client to have an "aggregated view" of the connected network (the network of all the servers it connects to) and +make complex sematic queries to it. + +Usage +----- + +For the installation process, click `here `__ + +1. Initializing the Agent: + +.. code-block:: python + + from hydra_agent.agent import Agent + agent = Agent("http://localhost:8080/serverapi") + +2. To *GET* a resource: + +a) To get a collection, the syntax is + +.. code-block:: python + + agent.get("http://localhost:8080/serverapi//") + +Example: + +.. code-block:: python + + agent.get("http://localhost:8080/serverapi/MovieCollection") + +Output: + Returns the ApiDoc for the resource MovieCollection + +b) To get members of a specific collection, the syntax is + +.. code-block:: python + + agent.get("http://localhost:8080/serverapi//") + +c) To get members of a specific resource, the syntax is + +.. code-block:: python + + agent.get("http://localhost:8080/serverapi//") + +3. To add a new resource using a *PUT* request: + + Syntax + +.. code-block:: python + + agent.post("http://localhost:8080/serverapi//,") + +.. code-block:: python + + new_resource={ + "@type" : "Movie", + "movie_name" : "The Shawshank Redemption", + } + agent.put("http://localhost:8080/serverapi/Movie/", new_resource) + +4. To update a resource using a *POST* request: + + Syntax + +.. code-block:: python + + agent.post("http://localhost:8080/serverapi//,") + +.. code-block:: python + + existing_resource["movie_name"] = "The Green Mile" + agent.post("http://localhost:8080/serverapi/Movie/", existing_resource) + +.. toctree:: + :maxdepth: 2 + :caption: Contents: + + agent + collection_paginator + helpers + querying_mechanism + + + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/docs/source/querying_mechanism.rst b/docs/source/querying_mechanism.rst new file mode 100644 index 0000000..3eb0cef --- /dev/null +++ b/docs/source/querying_mechanism.rst @@ -0,0 +1,6 @@ +querying_mechanism +============================================= + +.. automodule:: hydra_agent.querying_mechanism + :members: + From 5501fb6cf62dcb0b2c46d390e97f215ee0082303 Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Sun, 7 Feb 2021 18:58:52 +0530 Subject: [PATCH 2/6] Corrected syntax --- docs/source/index.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index 33bcdb4..1e4b703 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -91,13 +91,15 @@ c) To get members of a specific resource, the syntax is agent.get("http://localhost:8080/serverapi//") +Note: Collection-IDs and Resource-IDs are present in the database file generated after running hydrus. + 3. To add a new resource using a *PUT* request: Syntax .. code-block:: python - agent.post("http://localhost:8080/serverapi//,") + agent.put("http://localhost:8080/serverapi//,") .. code-block:: python @@ -118,7 +120,7 @@ c) To get members of a specific resource, the syntax is .. code-block:: python existing_resource["movie_name"] = "The Green Mile" - agent.post("http://localhost:8080/serverapi/Movie/", existing_resource) + agent.post("http://localhost:8080/serverapi/Movie/", existing_resource) .. toctree:: :maxdepth: 2 From 2f92dabff97bf148ea8a7523257ed9164ad7a711 Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Mon, 8 Feb 2021 13:18:20 +0530 Subject: [PATCH 3/6] Added steps to run docs locally --- docs/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..6c5c747 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,18 @@ +# How to run the docs locally + +1. (Skip this step if you have already installed hydrus)
+ i) Clone the hydrus repository: ```git clone https://github.com/HTTP-APIs/hydrus.git```
+ ii) Install requirements (It is *recommended* to use a virtual environment)
+ ```cd hydrus```
+ ```pip install -r requirements.txt```
+ iii) Install hydrus
+ ```python setup.py install```
+2. Build the docs
+ ```cd docs```
+ ```sphinx-build -b html ./source/ ./build/html/```
+HTML files are generated in the build/html folder which can be used to check the pages. +

+In order to make a clean build:
+ ```make clean```
+ ```sphinx-build -b html ./source/ ./build/html/``` + From 4c3e1a4ecf02a4c8b0a2cf202039bfb6c227922f Mon Sep 17 00:00:00 2001 From: Ashish Rao <54390491+raoashish10@users.noreply.github.com> Date: Mon, 8 Feb 2021 13:20:25 +0530 Subject: [PATCH 4/6] Update README.md --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 6c5c747..421c3c4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@  ```sphinx-build -b html ./source/ ./build/html/```
HTML files are generated in the build/html folder which can be used to check the pages.

-In order to make a clean build:
+In order to make a clean build:
 ```make clean```
 ```sphinx-build -b html ./source/ ./build/html/``` From cdf6ddc680b74d29b2add5619c6a382c6f51e90a Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Mon, 15 Feb 2021 13:14:07 +0530 Subject: [PATCH 5/6] Fixed and added new examples --- docs/source/index.rst | 97 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 17 deletions(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index 1e4b703..f1fc8b6 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -62,7 +62,66 @@ For the installation process, click `here ,") + +Example: + +.. code-block:: python + + new_resource={ + "@type" : "Movie", + "movie_name" : "The Shining", + "movie_director" : "Stanley Kubrick" + } + agent.put("http://localhost:8080/serverapi/Movie/", new_resource) + +Example output: + +.. code-block:: python + + ({'@context': 'http://www.w3.org/ns/hydra/context.jsonld', '@type': 'Status', 'description': 'Object with ID 9b1a5cb1-aaa3-411c-a4a6-93d0c4446fed successfully added', 'statusCode': 201, 'title': 'Object successfully added'}, 'http://localhost:8080/serverapi/Movie/9b1a5cb1-aaa3-411c-a4a6-93d0c4446fed') + +3. To add new members to a collection: + +Syntax: + +.. code-block:: python + + request_body = { + "@type": "", + "members": [ + { + "@id": "", + "@type": "" + }, + { + "@id": "", + "@type": "" + }, + ] + } + agent.put("http://localhost:8080/serverapi/", request_body) + +Example: + +.. code-block:: python + + request_body = {"@type" : "MovieCollection","members": [{"@id" : "9b1a5cb1-aaa3-411c-a4a6-93d0c4446fed","@type" : "Movie"},]} + agent.put("http://localhost:8080/serverapi/MovieCollection", request_body) + +Example output: + +.. code-block:: python + + ({'@context': 'http://www.w3.org/ns/hydra/context.jsonld', '@type': 'Status', 'description': 'Object with ID 2d2bdaa9-8f9c-46fe-ac16-6d69b7e52bf3 successfully added', 'statusCode': 201, 'title': 'Object successfully added'}, 'http://localhost:8080/serverapi/MovieCollection/2d2bdaa9-8f9c-46fe-ac16-6d69b7e52bf3') + +4. To *GET* a resource: a) To get a collection, the syntax is @@ -85,42 +144,46 @@ b) To get members of a specific collection, the syntax is agent.get("http://localhost:8080/serverapi//") -c) To get members of a specific resource, the syntax is +Example: .. code-block:: python - agent.get("http://localhost:8080/serverapi//") + agent.get("http://localhost:8080/serverapi/MovieCollection/2d2bdaa9-8f9c-46fe-ac16-6d69b7e52bf3") -Note: Collection-IDs and Resource-IDs are present in the database file generated after running hydrus. +c) To get members of a specific resource, the syntax is -3. To add a new resource using a *PUT* request: +.. code-block:: python - Syntax + agent.get("http://localhost:8080/serverapi//") + +Example: .. code-block:: python - agent.put("http://localhost:8080/serverapi//,") + agent.get("http://localhost:8080/serverapi/Movie/9b1a5cb1-aaa3-411c-a4a6-93d0c4446fed") + +Example output: .. code-block:: python - - new_resource={ - "@type" : "Movie", - "movie_name" : "The Shawshank Redemption", - } - agent.put("http://localhost:8080/serverapi/Movie/", new_resource) -4. To update a resource using a *POST* request: + {'@context': '/serverapi/contexts/Movie.jsonld', '@id': '/serverapi/Movie/9b1a5cb1-aaa3-411c-a4a6-93d0c4446fed', '@type': 'Movie', 'movie_director': 'Stanley Kubrick', 'movie_name': 'The Shining'} + +Note: Collection-IDs and Resource-IDs are present in the database file generated after running hydrus. + +5. To update a resource using a *POST* request: - Syntax +Syntax: .. code-block:: python agent.post("http://localhost:8080/serverapi//,") +Example: + .. code-block:: python - existing_resource["movie_name"] = "The Green Mile" - agent.post("http://localhost:8080/serverapi/Movie/", existing_resource) + existing_resource["movie_name"] = "A Clockwork Orange" + agent.post("http://localhost:8080/serverapi/Movie/9b1a5cb1-aaa3-411c-a4a6-93d0c4446fed", existing_resource) .. toctree:: :maxdepth: 2 From ad426403c5a3f6b5d35f6de8d1b7e407d4d19b93 Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Mon, 15 Feb 2021 22:47:34 +0530 Subject: [PATCH 6/6] Added requirements and made changes to files --- docs/README.md | 12 ++++++------ docs/source/conf.py | 4 ++-- requirements.txt | 2 ++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/README.md b/docs/README.md index 421c3c4..b2a5914 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,12 +1,12 @@ # How to run the docs locally -1. (Skip this step if you have already installed hydrus)
- i) Clone the hydrus repository: ```git clone https://github.com/HTTP-APIs/hydrus.git```
- ii) Install requirements (It is *recommended* to use a virtual environment)
- ```cd hydrus```
+1. (Skip this step if you have already installed hydra-python-agent)
+ i) Install requirements (It is *recommended* to use a virtual environment)
+ In the hydra-python-agent folder:
 ```pip install -r requirements.txt```
- iii) Install hydrus
- ```python setup.py install```
+ ii) Install hydra-python-agent
+ In the hydra-python-agent folder:
+ ```python setup.py install```

2. Build the docs
 ```cd docs```
 ```sphinx-build -b html ./source/ ./build/html/```
diff --git a/docs/source/conf.py b/docs/source/conf.py index dc148f8..2305beb 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -18,8 +18,8 @@ # -- Project information ----------------------------------------------------- project = 'hydra-python-agent' -copyright = '2021, Ashish Rao' -author = 'Ashish Rao' +copyright = '2021, Hydra Ecosystem' +author = 'Hydra Ecosystem' # The full version, including alpha/beta/rc tags release = '0.0.1' diff --git a/requirements.txt b/requirements.txt index f29b676..5254bdd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,5 @@ requests python-socketio -e git+https://github.com/RedisGraph/redisgraph-py@d790e35b7017510c4baad85e3c572249a9ed7279#egg=redisgraph -e git+https://github.com/http-apis/hydra-python-core@master#egg=hydra_python_core +Sphinx==3.1.2 +sphinx-rtd-theme==0.5.1 \ No newline at end of file