Move get_query to query.py

Refactor api and autocomplete to use it and properly handle errors
related to invalid project names.
This commit is contained in:
Franciszek Stachura 2024-08-07 13:21:02 +02:00
parent 1ace9f746f
commit 584c98686d
4 changed files with 29 additions and 35 deletions

View file

@ -29,19 +29,14 @@ ELIXIR_DIR = os.path.dirname(os.path.realpath(__file__)) + '/..'
if ELIXIR_DIR not in sys.path:
sys.path = [ ELIXIR_DIR ] + sys.path
import query
from query import get_query
class ApiIdentGetterResource:
def on_get(self, req, resp, project, ident):
try:
basedir = req.env['LXR_PROJ_DIR']
except KeyError:
basedir = os.environ['LXR_PROJ_DIR']
data_dir = basedir + '/' + project + '/data'
repo_dir = basedir + '/' + project + '/repo'
q = query.Query(data_dir, repo_dir)
query = get_query(req.context.config.project_dir, project)
if not query:
resp.status = falcon.HTTP_NOT_FOUND
return
if 'version' in req.params:
version = req.params['version']
@ -49,14 +44,14 @@ class ApiIdentGetterResource:
raise falcon.HTTPMissingParam('version')
if version == 'latest':
version = q.query('latest')
version = query.query('latest')
if 'family' in req.params:
family = req.params['family']
else:
family = 'C'
symbol_definitions, symbol_references, symbol_doccomments = q.query('ident', version, ident, family)
symbol_definitions, symbol_references, symbol_doccomments = query.query('ident', version, ident, family)
resp.status = falcon.HTTP_200
resp.content_type = falcon.MEDIA_JSON
@ -66,5 +61,5 @@ class ApiIdentGetterResource:
'documentations': [sym.__dict__ for sym in symbol_doccomments]
}
q.close()
query.close()

View file

@ -30,8 +30,8 @@ ELIXIR_DIR = os.path.dirname(os.path.realpath(__file__)) + '/..'
if ELIXIR_DIR not in sys.path:
sys.path = [ ELIXIR_DIR ] + sys.path
import query
from lib import autoBytes
from query import get_query
class AutocompleteResource:
def on_get(self, req, resp):
@ -40,22 +40,20 @@ class AutocompleteResource:
query_family = req.get_param('f')
query_project = req.get_param('p')
# Get project dirs
basedir = req.env['LXR_PROJ_DIR']
datadir = basedir + '/' + query_project + '/data'
repodir = basedir + '/' + query_project + '/repo'
query = get_query(req.context.config.project_dir, query_project)
if not query:
resp.status = falcon.HTTP_NOT_FOUND
return
q = query.Query(datadir, repodir)
latest = q.query('latest')
latest = query.query('latest')
if query_family == 'B':
# DTS identifiers are stored quoted
process = lambda x: parse.unquote(x)
db = q.db.comps
db = query.db.comps
else:
process = lambda x: x
db = q.db.defs
db = query.db.defs
response = []

View file

@ -44,18 +44,7 @@ from filters import get_filters
from filters.utils import FilterContext
from autocomplete import AutocompleteResource
from api import ApiIdentGetterResource
# Returns a Query class instance or None if project data directory does not exist
# basedir: absolute path to parent directory of all project data directories, ex. "/srv/elixir-data/"
# project: name of the project, directory in basedir, ex. "linux"
def get_query(basedir, project):
datadir = basedir + '/' + project + '/data'
repodir = basedir + '/' + project + '/repo'
if not(os.path.exists(datadir)) or not(os.path.exists(repodir)):
return None
return Query(datadir, repodir)
from query import get_query
# Generated a Elixir error page
def get_error_page(ctx, title, details=None):

View file

@ -43,6 +43,18 @@ class SymbolInstance(object):
def __str__(self):
return self.__repr__()
# Returns a Query class instance or None if project data directory does not exist
# basedir: absolute path to parent directory of all project data directories, ex. "/srv/elixir-data/"
# project: name of the project, directory in basedir, ex. "linux"
def get_query(basedir, project):
datadir = basedir + '/' + project + '/data'
repodir = basedir + '/' + project + '/repo'
if not os.path.exists(datadir) or not os.path.exists(repodir):
return None
return Query(datadir, repodir)
class Query:
def __init__(self, data_dir, repo_dir):
self.repo_dir = repo_dir