Commit eebf3c09 authored by Mark Hewitt's avatar Mark Hewitt
Browse files

search improvements

parent 279ee188
Pipeline #80877 passed with stages
in 2 minutes and 35 seconds
...@@ -28,6 +28,14 @@ For more information, specify the `-h` flag: ...@@ -28,6 +28,14 @@ For more information, specify the `-h` flag:
Change Log Change Log
========== ==========
0.0.28
------
- search kind command
- search id supports limit
- search query supports a specific query
- global query option renamed to filter
0.0.27 0.0.27
------ ------
......
...@@ -59,7 +59,7 @@ setup( ...@@ -59,7 +59,7 @@ setup(
package_dir={"": "src"}, package_dir={"": "src"},
py_modules=[splitext(basename(path))[0] for path in glob("src/*.py")], py_modules=[splitext(basename(path))[0] for path in glob("src/*.py")],
include_package_data=True, include_package_data=True,
install_requires=["click", "jmespath", "osdu-sdk==0.0.8", "requests", "tabulate", "msal"], install_requires=["click", "jmespath", "osdu-sdk==0.0.9", "requests", "tabulate", "msal"],
project_urls={ project_urls={
"Issue Tracker": "https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/-/issues", "Issue Tracker": "https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/-/issues",
}, },
......
...@@ -12,4 +12,4 @@ ...@@ -12,4 +12,4 @@
""" OSDU command line environment""" """ OSDU command line environment"""
__VERSION__ = "0.0.27" __VERSION__ = "0.0.28"
...@@ -58,7 +58,7 @@ def _format_click_options(cmd: click.Command, ctx: Context, formatter: HelpForma ...@@ -58,7 +58,7 @@ def _format_click_options(cmd: click.Command, ctx: Context, formatter: HelpForma
for param in cmd.get_params(ctx): for param in cmd.get_params(ctx):
_rv = param.get_help_record(ctx) _rv = param.get_help_record(ctx)
if _rv is not None: if _rv is not None:
if param.name in ["help", "debug", "config", "output", "query"]: if param.name in ["help", "debug", "config", "output", "filter"]:
common_opts.append(_rv) common_opts.append(_rv)
else: else:
opts.append(_rv) opts.append(_rv)
...@@ -169,8 +169,8 @@ def command_with_output(table_transformer=None): ...@@ -169,8 +169,8 @@ def command_with_output(table_transformer=None):
callback=output_callback, callback=output_callback,
) )
@click.option( @click.option(
"--query", "--filter",
help="JMESPath query string. See http://jmespath.org/ for more information and examples.", help="JMESPath string for filtering output. See http://jmespath.org/ for more information and examples.",
callback=jmes_callback, callback=jmes_callback,
) )
@functools.wraps(func) @functools.wraps(func)
...@@ -178,7 +178,7 @@ def command_with_output(table_transformer=None): ...@@ -178,7 +178,7 @@ def command_with_output(table_transformer=None):
def func_wrapper(*args, **kwargs): def func_wrapper(*args, **kwargs):
state = args[0] state = args[0]
kwargs.pop("output") kwargs.pop("output")
kwargs.pop("query") kwargs.pop("filter")
result = func(*args, **kwargs) result = func(*args, **kwargs)
if result is not None: if result is not None:
if type(result) in [dict, list]: if type(result) in [dict, list]:
......
...@@ -66,7 +66,7 @@ def handle_cli_exceptions(function): ...@@ -66,7 +66,7 @@ def handle_cli_exceptions(function):
logger.error("Error %s", ex.message) logger.error("Error %s", ex.message)
except ValueError as ex: except ValueError as ex:
logger.error(MSG_JSON_DECODE_ERROR) logger.error(MSG_JSON_DECODE_ERROR)
logger.debug(ex) logger.error(ex)
except (NoOptionError, NoSectionError) as ex: except (NoOptionError, NoSectionError) as ex:
logger.warning( logger.warning(
"Configuration missing from config ('%s'). Run 'osdu config update'", ex.args[0] "Configuration missing from config ('%s'). Run 'osdu config update'", ex.args[0]
......
...@@ -22,14 +22,26 @@ from osducli.cliclient import CliOsduClient, handle_cli_exceptions ...@@ -22,14 +22,26 @@ from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point # click entry point
@click.command(cls=CustomClickCommand) @click.command(cls=CustomClickCommand)
@click.argument("id") @click.argument("id")
@click.option(
"-l",
"--limit",
"limit",
default=10,
show_default=True,
help="maximum number of records to return.",
)
@handle_cli_exceptions @handle_cli_exceptions
@command_with_output("results[*]") @command_with_output("results[*]")
def _click_command(state: State, id: str): # noqa:W1 pylint: disable=invalid-name,redefined-builtin def _click_command(
state: State, id: str, limit: int
): # noqa:W1 pylint: disable=invalid-name,redefined-builtin
"""Search for the specified id""" """Search for the specified id"""
return query(state, id) return query(state, id, limit)
def query(state: State, id: str): # TO FIX later pylint: disable=invalid-name,redefined-builtin def query(
state: State, id: str, limit: int
): # TO FIX later pylint: disable=invalid-name,redefined-builtin
"""Search for the specified id """Search for the specified id
Args: Args:
...@@ -38,6 +50,6 @@ def query(state: State, id: str): # TO FIX later pylint: disable=invalid-name,r ...@@ -38,6 +50,6 @@ def query(state: State, id: str): # TO FIX later pylint: disable=invalid-name,r
connection = CliOsduClient(state.config) connection = CliOsduClient(state.config)
search_client = SearchClient(connection) search_client = SearchClient(connection)
json_response = search_client.query_by_id(id) json_response = search_client.query_by_id(id, limit)
return json_response return json_response
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Search service query command"""
import click
from osdu.search import SearchClient
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point
@click.command(cls=CustomClickCommand)
@click.argument("kind")
@click.option(
"-l",
"--limit",
"limit",
default=10,
show_default=True,
help="maximum number of records to return.",
)
@handle_cli_exceptions
@command_with_output("results[*]")
def _click_command(
state: State, kind: str, limit: int
): # noqa:W1 pylint: disable=invalid-name,redefined-builtin
"""Search for items of the specified kind"""
return query(state, kind, limit)
def query(
state: State, kind: str, limit: int
): # TO FIX later pylint: disable=invalid-name,redefined-builtin
"""Search for the specified kind
Args:
state (State): Global state
"""
connection = CliOsduClient(state.config)
search_client = SearchClient(connection)
json_response = search_client.query_by_kind(kind, limit)
return json_response
...@@ -23,6 +23,7 @@ from osducli.cliclient import CliOsduClient, handle_cli_exceptions ...@@ -23,6 +23,7 @@ from osducli.cliclient import CliOsduClient, handle_cli_exceptions
@click.command(cls=CustomClickCommand) @click.command(cls=CustomClickCommand)
@click.option("-k", "--kind", "kind", help="kind to search for") @click.option("-k", "--kind", "kind", help="kind to search for")
@click.option("-id", "--id", "_id", help="id to search for") @click.option("-id", "--id", "_id", help="id to search for")
@click.option("-q", "--query", "_query", help="custom search query")
@click.option( @click.option(
"-l", "-l",
"--limit", "--limit",
...@@ -33,13 +34,13 @@ from osducli.cliclient import CliOsduClient, handle_cli_exceptions ...@@ -33,13 +34,13 @@ from osducli.cliclient import CliOsduClient, handle_cli_exceptions
) )
@handle_cli_exceptions @handle_cli_exceptions
@command_with_output("results[*].{Id:id,Kind:kind,CreateTime:createTime}") @command_with_output("results[*].{Id:id,Kind:kind,CreateTime:createTime}")
def _click_command(state: State, kind: str, _id: str, limit: int): def _click_command(state: State, kind: str, _id: str, _query: str, limit: int):
"""Query search service""" """Search using more advanced query terms"""
return query(state, kind, _id, limit) return query(state, kind, _id, _query, limit)
def query( def query(
state: State, kind: str, id: str, limit: int state: State, kind: str, id: str, custom_query: str, limit: int
): # pylint: disable=invalid-name,redefined-builtin ): # pylint: disable=invalid-name,redefined-builtin
"""Query search service """Query search service
...@@ -47,8 +48,8 @@ def query( ...@@ -47,8 +48,8 @@ def query(
state (State): Global state state (State): Global state
""" """
connection = CliOsduClient(state.config) connection = CliOsduClient(state.config)
search_client = SearchClient(connection) search_client = SearchClient(connection)
json_response = search_client.query(kind, id, limit) print(custom_query)
json_response = search_client.query(kind, id, custom_query, limit)
return json_response return json_response
...@@ -478,7 +478,7 @@ class HelpTextTests(unittest.TestCase): ...@@ -478,7 +478,7 @@ class HelpTextTests(unittest.TestCase):
self.validate_output( self.validate_output(
"osdu search", "osdu search",
commands=("id", "info", "query"), commands=("id", "info", "kind", "query"),
) )
self.validate_output( self.validate_output(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment