Python jmespath.compile() Examples
The following are 30
code examples of jmespath.compile().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
jmespath
, or try the search function
.
Example #1
Source File: cli_core.py From aliyun-log-cli with MIT License | 6 votes |
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)
Example #2
Source File: paginate.py From faces with GNU General Public License v2.0 | 6 votes |
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
Example #3
Source File: waiter.py From faces with GNU General Public License v2.0 | 6 votes |
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
Example #4
Source File: policy.py From cloud-custodian with Apache License 2.0 | 6 votes |
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))
Example #5
Source File: waiter.py From faces with GNU General Public License v2.0 | 6 votes |
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
Example #6
Source File: waiter.py From bash-lambda-layer with MIT License | 6 votes |
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
Example #7
Source File: waiter.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
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
Example #8
Source File: utils.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
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
Example #9
Source File: _format.py From azure-cli-extensions with MIT License | 6 votes |
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)
Example #10
Source File: _format.py From azure-cli-extensions with MIT License | 6 votes |
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)))
Example #11
Source File: paginate.py From aws-extender with MIT License | 6 votes |
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
Example #12
Source File: waiter.py From bash-lambda-layer with MIT License | 6 votes |
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
Example #13
Source File: utils.py From faces with GNU General Public License v2.0 | 6 votes |
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
Example #14
Source File: paginate.py From bash-lambda-layer with MIT License | 6 votes |
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
Example #15
Source File: utils.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
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
Example #16
Source File: waiter.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
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
Example #17
Source File: waiter.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
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
Example #18
Source File: waiter.py From faces with GNU General Public License v2.0 | 6 votes |
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
Example #19
Source File: waiter.py From faces with GNU General Public License v2.0 | 6 votes |
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
Example #20
Source File: output.py From knack with MIT License | 6 votes |
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.")
Example #21
Source File: cwe.py From cloud-custodian with Apache License 2.0 | 6 votes |
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
Example #22
Source File: paginate.py From deepWordBug with Apache License 2.0 | 6 votes |
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
Example #23
Source File: utils.py From ibm-cos-sdk-python with Apache License 2.0 | 6 votes |
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
Example #24
Source File: paginate.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
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
Example #25
Source File: core.py From cloud-custodian with Apache License 2.0 | 6 votes |
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
Example #26
Source File: waiter.py From deepWordBug with Apache License 2.0 | 6 votes |
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
Example #27
Source File: waiter.py From deepWordBug with Apache License 2.0 | 6 votes |
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
Example #28
Source File: query.py From cloud-custodian with Apache License 2.0 | 6 votes |
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
Example #29
Source File: paginate.py From faces with GNU General Public License v2.0 | 6 votes |
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
Example #30
Source File: jp.py From mxnet-lambda with Apache License 2.0 | 5 votes |
def main(): parser = argparse.ArgumentParser() parser.add_argument('expression') parser.add_argument('-f', '--filename', help=('The filename containing the input data. ' 'If a filename is not given then data is ' 'read from stdin.')) parser.add_argument('--ast', action='store_true', help=('Pretty print the AST, do not search the data.')) args = parser.parse_args() expression = args.expression if args.ast: # Only print the AST expression = jmespath.compile(args.expression) sys.stdout.write(pformat(expression.parsed)) sys.stdout.write('\n') return 0 if args.filename: with open(args.filename, 'r') as f: data = json.load(f) else: data = sys.stdin.read() data = json.loads(data) try: sys.stdout.write(json.dumps( jmespath.search(expression, data), indent=4)) sys.stdout.write('\n') except exceptions.ArityError as e: sys.stderr.write("invalid-arity: %s\n" % e) return 1 except exceptions.JMESPathTypeError as e: sys.stderr.write("invalid-type: %s\n" % e) return 1 except exceptions.UnknownFunctionError as e: sys.stderr.write("unknown-function: %s\n" % e) return 1 except exceptions.ParseError as e: sys.stderr.write("syntax-error: %s\n" % e) return 1