Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled JMESPath Community assets and dependencies. #272

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/query.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Query
=====

Query support is provided through [JMESPath](http://jmespath.org).
Query support is provided through [JMESPath](http://jmespath.site/main).

This allows filter and project of command output.
2 changes: 1 addition & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TestMyScenarios(ScenarioTest):
```
Note:

1. What is JMESPath? [JMESPath is a query language for JSON](http://jmespath.org/)
1. What is JMESPath? [JMESPath is a query language for JSON](http://jmespath.site/main/)
2. If a command is return value in JSON, multiple JMESPath based check can be added to the checks list to validate the result.
3. In addition to the `JMESPatchCheck`, there are other checks list `NoneCheck` which validate the output is `None`. The check mechanism is extensible. Any callable accept `ExecutionResult` can act as a check.

Expand Down
3 changes: 2 additions & 1 deletion knack/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def format_table(obj):
if obj.table_transformer and not obj.is_query_active:
if isinstance(obj.table_transformer, str):
from jmespath import compile as compile_jmes, Options
result = compile_jmes(obj.table_transformer).search(result, Options(OrderedDict))
options = Options(dict_cls=OrderedDict, enable_legacy_literals=True)
result = compile_jmes(obj.table_transformer, options).search(result, options)
else:
result = obj.table_transformer(result)
result_list = result if isinstance(result, list) else [result]
Expand Down
9 changes: 6 additions & 3 deletions knack/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def jmespath_type(raw_query):
In addition though, JMESPath can raise a KeyError.
ValueErrors are caught by argparse so argument errors can be generated.
"""
from jmespath import Options
from jmespath import compile as compile_jmespath
try:
return compile_jmespath(raw_query)
return compile_jmespath(raw_query, Options(enable_legacy_literals=True))
except KeyError as ex:
# Raise a ValueError which argparse can handle
raise ValueError from ex
Expand All @@ -30,7 +31,7 @@ def jmespath_type(raw_query):
def on_global_arguments(_, **kwargs):
arg_group = kwargs.get('arg_group')
arg_group.add_argument('--query', dest='_jmespath_query', metavar='JMESPATH',
help='JMESPath query string. See http://jmespath.org/ for more'
help='JMESPath query string. See http://jmespath.site/main/ for more'
' information and examples.',
type=CLIQuery.jmespath_type)

Expand All @@ -43,7 +44,9 @@ def handle_query_parameter(cli_ctx, **kwargs):
def filter_output(cli_ctx, **kwargs):
from jmespath import Options
kwargs['event_data']['result'] = query_expression.search(
kwargs['event_data']['result'], Options(collections.OrderedDict))
kwargs['event_data']['result'], Options(
dict_cls=collections.OrderedDict,
enable_legacy_literals=True))
cli_ctx.unregister_event(EVENT_INVOKER_FILTER_RESULT, filter_output)
cli_ctx.register_event(EVENT_INVOKER_FILTER_RESULT, filter_output)
cli_ctx.invocation.data['query_active'] = True
Expand Down
12 changes: 9 additions & 3 deletions knack/testsdk/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def __init__(self, query, expected_result):
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
jmespath.Options(
dict_cls=collections.OrderedDict,
enable_legacy_literals=True))
if not actual_result == self._expected_result:
if actual_result:
raise JMESPathCheckAssertionError(self._query, self._expected_result, actual_result,
Expand All @@ -32,7 +34,9 @@ def __init__(self, query):
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
jmespath.Options(
dict_cls=collections.OrderedDict,
enable_legacy_literals=True))
if not actual_result:
raise JMESPathCheckAssertionError(self._query, 'some value', actual_result,
execution_result.output)
Expand All @@ -46,7 +50,9 @@ def __init__(self, query, expected_result):
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
jmespath.Options(
dict_cls=collections.OrderedDict,
enable_legacy_literals=True))
if not actual_result > self._expected_result:
expected_result_format = "> {}".format(self._expected_result)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
argcomplete==1.12.2
flake8==4.0.1
jmespath==0.10.0
jmespath-community==1.1.0
Pygments==2.8.1
pylint==2.11.1
pytest==6.2.5
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

DEPENDENCIES = [
'argcomplete',
'jmespath',
'jmespath-community',
'pygments',
'pyyaml',
'tabulate'
Expand Down
15 changes: 9 additions & 6 deletions tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def example_handler(arg1, arg2=None, arg3=None):


class TestHelpArgumentGroupRegistry(unittest.TestCase):

unittest.TestCase.maxDiff = None

def test_help_argument_group_registry(self):
groups = [
'Z Arguments',
Expand Down Expand Up @@ -279,8 +282,8 @@ def test_help_full_documentations(self):
--only-show-errors : Only show errors, suppressing warnings.
--output -o : Output format. Allowed values: json, jsonc, none, table, tsv, yaml,
yamlc. Default: json.
--query : JMESPath query string. See http://jmespath.org/ for more information and
examples.
--query : JMESPath query string. See http://jmespath.site/main/ for more
information and examples.
--verbose : Increase logging verbosity. Use --debug for full debug logs.

Examples
Expand Down Expand Up @@ -314,8 +317,8 @@ def test_help_with_param_specified(self):
--only-show-errors : Only show errors, suppressing warnings.
--output -o : Output format. Allowed values: json, jsonc, none, table, tsv, yaml, yamlc.
Default: json.
--query : JMESPath query string. See http://jmespath.org/ for more information and
examples.
--query : JMESPath query string. See http://jmespath.site/main/ for more information
and examples.
--verbose : Increase logging verbosity. Use --debug for full debug logs.

"""
Expand Down Expand Up @@ -431,8 +434,8 @@ def register_globals(_, **kwargs):
--only-show-errors : Only show errors, suppressing warnings.
--output -o : Output format. Allowed values: json, jsonc, none, table, tsv, yaml, yamlc.
Default: json.
--query : JMESPath query string. See http://jmespath.org/ for more information and
examples.
--query : JMESPath query string. See http://jmespath.site/main/ for more information
and examples.
--verbose : Increase logging verbosity. Use --debug for full debug logs.

"""
Expand Down