def handle_jmespath_query(self, args):
        """ handles the jmespath query for injection or printing """
        continue_flag = False
        query_symbol = SELECT_SYMBOL['query']
        symbol_len = len(query_symbol)
        try:
            if len(args) == 1:
                # if arguments start with query_symbol, just print query result
                if args[0] == query_symbol:
                    result = self.last.result
                elif args[0].startswith(query_symbol):
                    result = jmespath.search(args[0][symbol_len:], self.last.result)
                print(json.dumps(result, sort_keys=True, indent=2), file=self.output)
            elif args[0].startswith(query_symbol):
                # print error message, user unsure of query shortcut usage
                print(("Usage Error: " + os.linesep +
                       "1. Use {0} stand-alone to display previous result with optional filtering "
                       "(Ex: {0}[jmespath query])" +
                       os.linesep + "OR:" + os.linesep +
                       "2. Use {0} to query the previous result for argument values "
                       "(Ex: group show --name {0}[jmespath query])").format(query_symbol), file=self.output)
            else:
                # query, inject into cmd
                def jmespath_query(match):
                    if match.group(0) == query_symbol:
                        return str(self.last.result)
                    query_result = jmespath.search(match.group(0)[symbol_len:], self.last.result)
                    return str(query_result)

                def sub_result(arg):
                    escaped_symbol = re.escape(query_symbol)
                    # regex captures query symbol and all characters following it in the argument
                    return json.dumps(re.sub(r'%s.*' % escaped_symbol, jmespath_query, arg))
                cmd_base = ' '.join(map(sub_result, args))
                self.cli_execute(cmd_base)
            continue_flag = True
        except (jmespath.exceptions.ParseError, CLIError) as e:
            print("Invalid Query Input: " + str(e), file=self.output)
            continue_flag = True
        return continue_flag
def handle_jmespath_query ( self , args ) : continue_flag = False query_symbol = SELECT_SYMBOL [ 'query' ] symbol_len = len ( query_symbol ) try : if len ( args ) == 1 : if args [ 0 ] == query_symbol : result = self . last . result elif args [ 0 ] . startswith ( query_symbol ) : result = jmespath . search ( args [ 0 ] [ symbol_len : ] , self . last . result ) print ( json . dumps ( result , sort_keys = True , indent = 2 ) , file = self . output ) elif args [ 0 ] . startswith ( query_symbol ) : print ( ( "Usage Error: " + os . linesep + "1. Use {0} stand-alone to display previous result with optional filtering " "(Ex: {0}[jmespath query])" + os . linesep + "OR:" + os . linesep + "2. Use {0} to query the previous result for argument values " "(Ex: group show --name {0}[jmespath query])" ) . format ( query_symbol ) , file = self . output ) else : def jmespath_query ( match ) : if match . group ( 0 ) == query_symbol : return str ( self . last . result ) query_result = jmespath . search ( match . group ( 0 ) [ symbol_len : ] , self . last . result ) return str ( query_result ) def sub_result ( arg ) : escaped_symbol = re . escape ( query_symbol ) return json . dumps ( re . sub ( r'%s.*' % escaped_symbol , jmespath_query , arg ) ) cmd_base = ' ' . join ( map ( sub_result , args ) ) self . cli_execute ( cmd_base ) continue_flag = True except ( jmespath . exceptions . ParseError , CLIError ) as e : print ( "Invalid Query Input: " + str ( e ) , file = self . output ) continue_flag = True return continue_flag
handles the jmespath query for injection or printing