You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kibble.apache.org by tu...@apache.org on 2020/10/24 12:06:57 UTC

[kibble] branch master updated: Refactor api to be a package (#51)

This is an automated email from the ASF dual-hosted git repository.

turbaszek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kibble.git


The following commit(s) were added to refs/heads/master by this push:
     new 5e18332  Refactor api to be a package (#51)
5e18332 is described below

commit 5e18332502d80992c093254adc61f6d52aea7568
Author: Tomek Urbaszek <tu...@gmail.com>
AuthorDate: Sat Oct 24 14:06:48 2020 +0200

    Refactor api to be a package (#51)
    
    Closes: #49
---
 api/{pages => }/__init__.py | 31 -------------------------------
 api/handler.py              | 35 ++++++++++++++++++-----------------
 api/pages/__init__.py       |  5 ++---
 api/plugins/database.py     | 11 ++++-------
 api/plugins/openapi.py      |  2 ++
 api/plugins/session.py      |  7 +------
 docker-compose-dev.yaml     |  2 +-
 7 files changed, 28 insertions(+), 65 deletions(-)

diff --git a/api/pages/__init__.py b/api/__init__.py
similarity index 50%
copy from api/pages/__init__.py
copy to api/__init__.py
index af0f582..13a8339 100644
--- a/api/pages/__init__.py
+++ b/api/__init__.py
@@ -14,34 +14,3 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-
-"""
-Kibble API scripts library:
-
-    oauth:          oauth manager
-
-"""
-
-import importlib
-import os
-# Define all the submodules we have
-
-rootpath = os.path.dirname(__file__)
-print("Reading pages from %s" % rootpath)
-
-# Import each submodule into a hash called 'handlers'
-handlers = {}
-
-def loadPage(path):
-    for el in os.listdir(path):
-        filepath = os.path.join(path, el)
-        if el.find("__") == -1:
-            if os.path.isdir(filepath):
-                loadPage(filepath)
-            else:
-                p = filepath.replace(rootpath, "")[1:].replace('/', '.')[:-3]
-                xp = p.replace('.', '/')
-                print("Loading endpoint pages.%s as %s" % (p, xp))
-                handlers[xp] = importlib.import_module("pages.%s" % p)
-
-loadPage(rootpath)
diff --git a/api/handler.py b/api/handler.py
index 124252c..466d4b5 100644
--- a/api/handler.py
+++ b/api/handler.py
@@ -23,36 +23,39 @@ It compiles a list of valid URLs from the 'pages' library folder,
 and if a URL matches it runs the specific submodule's run() function. It
 also handles CGI parsing and exceptions in the applications.
 """
-
-
-# Main imports
-import cgi
+import os
 import re
 import sys
 import traceback
 import yaml
 import json
-import plugins.session
-import plugins.database
-import plugins.openapi
+
+from api.plugins import openapi
+from api.plugins.database import KibbleDatabase
+from api.plugins.session import KibbleSession
+
 
 # Compile valid API URLs from the pages library
 # Allow backwards compatibility by also accepting .lua URLs
 urls = []
 if __name__ != '__main__':
-    import pages
-    for page in pages.handlers:
-        urls.append((r"^(/api/%s)(/.+)?$" % page, pages.handlers[page].run))
+    from api.pages import handlers
+    for page, handler in handlers.items():
+        urls.append((r"^(/api/%s)(/.+)?$" % page, handler.run))
 
 
 # Load Kibble master configuration
-config = yaml.load(open("yaml/kibble.yaml"))
+config_yaml = os.path.join(os.path.dirname(os.path.realpath(__file__)), "yaml", "kibble.yaml")
+with open(config_yaml, "r") as f:
+    config = yaml.load(f)
 
 # Instantiate database connections
 DB = None
 
 # Load Open API specifications
-KibbleOpenAPI = plugins.openapi.OpenAPI("yaml/openapi.yaml")
+openapi_yaml = os.path.join(os.path.dirname(os.path.realpath(__file__)), "yaml", "openapi.yaml")
+KibbleOpenAPI = openapi.OpenAPI(openapi_yaml)
+
 
 class KibbleHTTPError(Exception):
     def __init__(self, code, message):
@@ -72,7 +75,6 @@ class KibbleAPIWrapper:
 
     def __call__(self, environ, start_response, session):
         """Run the function, return response OR return stacktrace"""
-        response = None
         try:
             # Read JSON client data if any
             try:
@@ -96,7 +98,7 @@ class KibbleAPIWrapper:
             # Validate URL against OpenAPI specs
             try:
                 self.API.validate(environ['REQUEST_METHOD'], self.path, formdata)
-            except plugins.openapi.OpenAPIException as err:
+            except openapi.OpenAPIException as err:
                 start_response('400 Invalid request', [
                             ('Content-Type', 'application/json')])
                 yield json.dumps({
@@ -161,13 +163,13 @@ def application(environ, start_response):
     Checks against the pages library, and if submod found, runs
     it and returns the output.
     """
-    DB = plugins.database.KibbleDatabase(config)
+    db = KibbleDatabase(config)
     path = environ.get('PATH_INFO', '')
     for regex, function in urls:
         m = re.match(regex, path)
         if m:
             callback = KibbleAPIWrapper(path, function)
-            session = plugins.session.KibbleSession(DB, environ, config)
+            session = KibbleSession(db, environ, config)
             a = 0
             for bucket in callback(environ, start_response, session):
                 if a == 0:
@@ -188,6 +190,5 @@ def application(environ, start_response):
         yield bytes(bucket, encoding = 'utf-8')
 
 
-
 if __name__ == '__main__':
     KibbleOpenAPI.toHTML()
diff --git a/api/pages/__init__.py b/api/pages/__init__.py
index af0f582..f3ba204 100644
--- a/api/pages/__init__.py
+++ b/api/pages/__init__.py
@@ -26,7 +26,7 @@ import importlib
 import os
 # Define all the submodules we have
 
-rootpath = os.path.dirname(__file__)
+rootpath = os.path.join(os.path.dirname(os.path.realpath(__file__)))
 print("Reading pages from %s" % rootpath)
 
 # Import each submodule into a hash called 'handlers'
@@ -42,6 +42,5 @@ def loadPage(path):
                 p = filepath.replace(rootpath, "")[1:].replace('/', '.')[:-3]
                 xp = p.replace('.', '/')
                 print("Loading endpoint pages.%s as %s" % (p, xp))
-                handlers[xp] = importlib.import_module("pages.%s" % p)
-
+                handlers[xp] = importlib.import_module("api.pages.%s" % p)
 loadPage(rootpath)
diff --git a/api/plugins/database.py b/api/plugins/database.py
index 913f41d..c36001d 100644
--- a/api/plugins/database.py
+++ b/api/plugins/database.py
@@ -20,17 +20,13 @@ This is the ES library for Apache Kibble.
 It stores the elasticsearch handler and config options.
 """
 
-
-# Main imports
-import cgi
-import re
-#import aaa
 import elasticsearch
 
+
 class KibbleESWrapper(object):
     """
-       Class for rewriting old-style queries to the new ones,
-       where doc_type is an integral part of the DB name
+    Class for rewriting old-style queries to the new ones,
+    where doc_type is an integral part of the DB name
     """
     def __init__(self, ES):
         self.ES = ES
@@ -65,6 +61,7 @@ class KibbleESWrapper(object):
             body = body
             )
 
+
 class KibbleESWrapperSeven(object):
     """
        Class for rewriting old-style queries to the >= 7.x ones,
diff --git a/api/plugins/openapi.py b/api/plugins/openapi.py
index 044ff88..f289470 100644
--- a/api/plugins/openapi.py
+++ b/api/plugins/openapi.py
@@ -27,10 +27,12 @@ import functools
 import operator
 import re
 
+
 class OpenAPIException(Exception):
     def __init__(self, message):
         self.message = message
 
+
 # Python type names to JSON type names
 py2JSON = {
     'int':      'integer',
diff --git a/api/plugins/session.py b/api/plugins/session.py
index 56fb87c..6c212f5 100644
--- a/api/plugins/session.py
+++ b/api/plugins/session.py
@@ -20,17 +20,12 @@ This is the session library for Apache Kibble.
 It handles setting/getting cookies and user prefs
 """
 
-
-# Main imports
-import cgi
 import re
-import sys
-import traceback
 import http.cookies
 import uuid
-import elasticsearch
 import time
 
+
 class KibbleSession(object):
 
     def getView(self, viewID):
diff --git a/docker-compose-dev.yaml b/docker-compose-dev.yaml
index bb7f1d2..c53e990 100644
--- a/docker-compose-dev.yaml
+++ b/docker-compose-dev.yaml
@@ -16,7 +16,7 @@ services:
   # Apache Kibble API server
   kibble:
     image: *img
-    command: bash -c "cd api && gunicorn --reload -w 1 -b 0.0.0.0:8001 handler:application"
+    command: bash -c "gunicorn --reload -w 1 -b 0.0.0.0:8001 api.handler:application"
     expose:
       - 8001
     ports: