本文整理匯總了Python中jmespath.compile方法的典型用法代碼示例。如果您正苦於以下問題:Python jmespath.compile方法的具體用法?Python jmespath.compile怎麽用?Python jmespath.compile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jmespath
的用法示例。
在下文中一共展示了jmespath.compile方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: format_table
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def format_table(obj):
result = obj.result
try:
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))
else:
result = obj.table_transformer(result)
result_list = result if isinstance(result, list) else [result]
should_sort_keys = not obj.is_query_active and not obj.table_transformer
to = _TableOutput(should_sort_keys)
return to.dump(result_list)
except:
logger.debug(traceback.format_exc())
raise CLIError("Table output unavailable. "
"Use the --query option to specify an appropriate query. "
"Use --debug for more info.")
示例2: aks_upgrades_table_format
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def aks_upgrades_table_format(result):
"""Format get-upgrades results as a summary for display with "-o table"."""
preview = {}
def find_preview_versions(versions_bag):
for upgrade in versions_bag.get('upgrades', []):
if upgrade.get('isPreview', False):
preview[upgrade['kubernetesVersion']] = True
find_preview_versions(result.get('controlPlaneProfile', {}))
find_preview_versions(result.get('agentPoolProfiles', [{}])[0])
# This expression assumes there is one node pool, and that the master and nodes upgrade in lockstep.
parsed = compile_jmes("""{
name: name,
resourceGroup: resourceGroup,
masterVersion: controlPlaneProfile.kubernetesVersion || `unknown` | set_preview(@),
nodePoolVersion: agentPoolProfiles[0].kubernetesVersion || `unknown` | set_preview(@),
upgrades: controlPlaneProfile.upgrades[].kubernetesVersion || [`None available`] | sort_versions(@) | set_preview_array(@) | join(`, `, @)
}""")
# use ordered dicts so headers are predictable
return parsed.search(result, Options(dict_cls=OrderedDict, custom_functions=_custom_functions(preview)))
示例3: aks_versions_table_format
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def aks_versions_table_format(result):
"""Format get-versions results as a summary for display with "-o table"."""
# get preview orchestrator version
preview = {}
def find_preview_versions():
for orchestrator in result.get('orchestrators', []):
if orchestrator.get('isPreview', False):
preview[orchestrator['orchestratorVersion']] = True
find_preview_versions()
parsed = compile_jmes("""orchestrators[].{
kubernetesVersion: orchestratorVersion | set_preview(@),
upgrades: upgrades[].orchestratorVersion || [`None available`] | sort_versions(@) | set_preview_array(@) | join(`, `, @)
}""")
# use ordered dicts so headers are predictable
results = parsed.search(result, Options(dict_cls=OrderedDict, custom_functions=_custom_functions(preview)))
return sorted(results, key=lambda x: version_to_tuple(x.get('kubernetesVersion')), reverse=True)
示例4: search
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def search(self, expression):
"""Applies a JMESPath expression to a paginator
Each page of results is searched using the provided JMESPath
expression. If the result is not a list, it is yielded
directly. If the result is a list, each element in the result
is yielded individually (essentially implementing a flatmap in
which the JMESPath search is the mapping function).
:type expression: str
:param expression: JMESPath expression to apply to each page.
:return: Returns an iterator that yields the individual
elements of applying a JMESPath expression to each page of
results.
"""
compiled = jmespath.compile(expression)
for page in self:
results = compiled.search(page)
if isinstance(results, list):
for element in results:
yield element
else:
# Yield result directly if it is not a list.
yield results
示例5: _create_path_all_matcher
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def _create_path_all_matcher(self):
expression = jmespath.compile(self.argument)
expected = self.expected
def acceptor_matches(response):
if 'Error' in response:
return
result = expression.search(response)
if not isinstance(result, list) or not result:
# pathAll matcher must result in a list.
# Also we require at least one element in the list,
# that is, an empty list should not result in this
# acceptor match.
return False
for element in result:
if element != expected:
return False
return True
return acceptor_matches
示例6: _create_path_any_matcher
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def _create_path_any_matcher(self):
expression = jmespath.compile(self.argument)
expected = self.expected
def acceptor_matches(response):
if 'Error' in response:
return
result = expression.search(response)
if not isinstance(result, list) or not result:
# pathAny matcher must result in a list.
# Also we require at least one element in the list,
# that is, an empty list should not result in this
# acceptor match.
return False
for element in result:
if element == expected:
return True
return False
return acceptor_matches
示例7: get_resource_ignore_params
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def get_resource_ignore_params(params):
"""Helper method to determine which parameters to ignore for actions
:returns: A list of the parameter names that does not need to be
included in a resource's method call for documentation purposes.
"""
ignore_params = []
for param in params:
result = jmespath.compile(param.target)
current = result.parsed
# Use JMESPath to find the left most element in the target expression
# which will be the parameter to ignore in the action call.
while current['children']:
current = current['children'][0]
# Make sure the parameter we are about to ignore is a field.
# If it is not, we should ignore the result to avoid false positives.
if current['type'] == 'field':
ignore_params.append(current['value'])
return ignore_params
示例8: _process_response_data
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def _process_response_data(data, jmes_filter, format_output, decode_output):
if data is not None:
if jmes_filter:
# filter with jmes
try:
if 'no_escape' in format_output.strip().lower():
with monkey_patch(json, 'dumps', partial(json.dumps, ensure_ascii=False)):
result = jmespath.compile(jmes_filter).search(data)
else:
result = jmespath.compile(jmes_filter).search(data)
show_result(result, format_output, decode_output)
except jmespath.exceptions.ParseError as ex:
logger.error("fail to parse with JMES path, original data: %s", ex)
show_result(data, format_output, decode_output)
exit(1)
else:
show_result(data, format_output, decode_output)
示例9: validate
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def validate(self):
super(CloudTrailMode, self).validate()
from c7n import query
events = self.policy.data['mode'].get('events')
assert events, "cloud trail mode requires specifiying events to subscribe"
for e in events:
if isinstance(e, str):
assert e in CloudWatchEvents.trail_events, "event shortcut not defined: %s" % e
if isinstance(e, dict):
jmespath.compile(e['ids'])
if isinstance(self.policy.resource_manager, query.ChildResourceManager):
if not getattr(self.policy.resource_manager.resource_type,
'supports_trailevents', False):
raise ValueError(
"resource:%s does not support cloudtrail mode policies" % (
self.policy.resource_type))
示例10: match
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def match(cls, event):
"""Match a given cwe event as cloudtrail with an api call
That has its information filled out.
"""
if 'detail' not in event:
return False
if 'eventName' not in event['detail']:
return False
k = event['detail']['eventName']
# We want callers to use a compiled expression, but want to avoid
# initialization cost of doing it without cause. Not thread safe,
# but usage context is lambda entry.
if k in cls.trail_events:
v = dict(cls.trail_events[k])
if isinstance(v['ids'], str):
v['ids'] = e = jmespath.compile('detail.%s' % v['ids'])
cls.trail_events[k]['ids'] = e
return v
return False
示例11: _validate_value_regex
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def _validate_value_regex(self):
"""Specific validation for `value_regex` type
The `value_regex` type works a little differently. In
particular it doesn't support OPERATORS that perform
operations on a list of values, specifically 'intersect',
'contains', 'difference', 'in' and 'not-in'
"""
# Sanity check that we can compile
try:
pattern = re.compile(self.data['value_regex'])
if pattern.groups != 1:
raise PolicyValidationError(
"value_regex must have a single capturing group: %s" %
self.data)
except re.error as e:
raise PolicyValidationError(
"Invalid value_regex: %s %s" % (e, self.data))
return self
示例12: _invoke_client_enum
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def _invoke_client_enum(self, client, enum_op, params, path, retry=None):
if client.can_paginate(enum_op):
p = client.get_paginator(enum_op)
if retry:
p.PAGE_ITERATOR_CLS = RetryPageIterator
results = p.paginate(**params)
data = results.build_full_result()
else:
op = getattr(client, enum_op)
data = op(**params)
if path:
path = jmespath.compile(path)
data = path.search(data)
return data
示例13: jmespath_type
# 需要導入模塊: import jmespath [as 別名]
# 或者: from jmespath import compile [as 別名]
def jmespath_type(raw_query):
"""Compile the query with JMESPath and return the compiled result.
JMESPath raises exceptions which subclass from ValueError.
In addition though, JMESPath can raise a KeyError.
ValueErrors are caught by argparse so argument errors can be generated.
"""
from jmespath import compile as compile_jmespath
try:
return compile_jmespath(raw_query)
except KeyError:
# Raise a ValueError which argparse can handle
raise ValueError