def format_table(obj): result = obj.result try: if not obj.simple_output_query and not obj.is_query_active: raise ValueError('No query specified and no built-in query available.') if obj.simple_output_query and not obj.is_query_active: if callable(obj.simple_output_query): result = obj.simple_output_query(result) else: from jmespath import compile as compile_jmespath, search, Options result = compile_jmespath(obj.simple_output_query).search(result, Options(OrderedDict)) obj_list = result if isinstance(result, list) else [result] to = TableOutput() for item in obj_list: for item_key in item: to.cell(item_key, item[item_key]) to.end_row() return to.dump() except (ValueError, KeyError, TypeError): logger.debug(traceback.format_exc()) raise CLIError("Table output unavailable. "\ "Change output type with --output or use "\ "the --query option to specify an appropriate query. "\ "Use --debug for more info.")
def verify_property(instance, condition): from jmespath import compile as compile_jmespath result = todict(instance) jmes_query = compile_jmespath(condition) value = jmes_query.search(result) return value
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