nodes: 0, module; 1, function_definition; 2, function_name:_get_pool_results; 3, parameters; 4, block; 5, list_splat_pattern; 6, dictionary_splat_pattern; 7, expression_statement; 8, comment:# TODO: the option "survey.sort=" would be preferred for namespace; 9, comment:# separation but the kwargs parser for the salt-run command seems to; 10, comment:# improperly pass the options containing a "." in them for later modules to; 11, comment:# process. The "_" is used here instead.; 12, import_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, if_statement; 20, expression_statement; 21, expression_statement; 22, try_statement; 23, comment:# hash minion return values as a string; 24, for_statement; 25, expression_statement; 26, for_statement; 27, return_statement; 28, identifier:args; 29, identifier:kwargs; 30, string:'''
      A helper function which returns a dictionary of minion pools along with
      their matching result sets.
      Useful for developing other "survey style" functions.
      Optionally accepts a "survey_sort=up" or "survey_sort=down" kwargs for
      specifying sort order.
      Because the kwargs namespace of the "salt" and "survey" command are shared,
      the name "survey_sort" was chosen to help avoid option conflicts.
      '''; 31, dotted_name; 32, assignment; 33, assignment; 34, assignment; 35, assignment; 36, assignment; 37, assignment; 38, comparison_operator:tgt_type not in ['compound', 'pcre']; 39, block; 40, assignment; 41, assignment; 42, block; 43, except_clause; 44, identifier:minion; 45, call; 46, block; 47, assignment; 48, identifier:k; 49, call; 50, comment:# return aggregated results, sorted by size of the hash pool; 51, block; 52, identifier:sorted_ret; 53, identifier:hashlib; 54, identifier:tgt; 55, subscript; 56, identifier:cmd; 57, subscript; 58, identifier:ret; 59, dictionary; 60, identifier:sort; 61, call; 62, identifier:direction; 63, comparison_operator:sort != 'up'; 64, identifier:tgt_type; 65, call; 66, identifier:tgt_type; 67, list; 68, expression_statement; 69, identifier:kwargs_passthru; 70, call; 71, identifier:client; 72, call; 73, expression_statement; 74, as_pattern; 75, block; 76, identifier:sorted; 77, argument_list; 78, expression_statement; 79, if_statement; 80, expression_statement; 81, identifier:sorted_ret; 82, list; 83, identifier:sorted; 84, argument_list; 85, expression_statement; 86, identifier:args; 87, integer:0; 88, identifier:args; 89, integer:1; 90, attribute; 91, argument_list; 92, identifier:sort; 93, string; 94, attribute; 95, argument_list; 96, string; 97, string; 98, assignment; 99, identifier:dict; 100, generator_expression; 101, attribute; 102, argument_list; 103, assignment; 104, identifier:SaltClientError; 105, as_pattern_target; 106, expression_statement; 107, return_statement; 108, identifier:minions; 109, assignment; 110, comparison_operator:digest not in ret; 111, block; 112, call; 113, identifier:ret; 114, keyword_argument; 115, keyword_argument; 116, call; 117, identifier:kwargs; 118, identifier:pop; 119, string; 120, string; 121, string_content:up; 122, identifier:kwargs; 123, identifier:pop; 124, string; 125, string; 126, string_content:compound; 127, string_content:pcre; 128, identifier:tgt_type; 129, string; 130, tuple; 131, for_in_clause; 132, if_clause; 133, attribute; 134, identifier:get_local_client; 135, subscript; 136, identifier:minions; 137, call; 138, identifier:client_error; 139, call; 140, identifier:ret; 141, identifier:digest; 142, call; 143, identifier:digest; 144, identifier:ret; 145, expression_statement; 146, expression_statement; 147, expression_statement; 148, attribute; 149, argument_list; 150, identifier:key; 151, lambda; 152, identifier:reverse; 153, identifier:direction; 154, attribute; 155, argument_list; 156, string_content:survey_sort; 157, string_content:down; 158, string_content:tgt_type; 159, string_content:compound; 160, string_content:compound; 161, identifier:k; 162, subscript; 163, identifier:k; 164, call; 165, not_operator; 166, identifier:salt; 167, identifier:client; 168, identifier:__opts__; 169, string; 170, attribute; 171, argument_list; 172, identifier:print; 173, argument_list; 174, attribute; 175, argument_list; 176, assignment; 177, assignment; 178, assignment; 179, subscript; 180, identifier:append; 181, identifier:minion; 182, lambda_parameters; 183, call; 184, identifier:sorted_ret; 185, identifier:append; 186, subscript; 187, identifier:kwargs; 188, identifier:k; 189, attribute; 190, argument_list; 191, call; 192, string_content:conf_file; 193, identifier:client; 194, identifier:cmd; 195, identifier:tgt; 196, identifier:cmd; 197, subscript; 198, keyword_argument; 199, keyword_argument; 200, keyword_argument; 201, identifier:client_error; 202, call; 203, identifier:hexdigest; 204, subscript; 205, dictionary; 206, subscript; 207, list; 208, subscript; 209, call; 210, subscript; 211, string; 212, identifier:k; 213, identifier:len; 214, argument_list; 215, identifier:ret; 216, identifier:k; 217, identifier:six; 218, identifier:iterkeys; 219, identifier:kwargs; 220, attribute; 221, argument_list; 222, identifier:args; 223, slice; 224, identifier:timeout; 225, subscript; 226, identifier:tgt_type; 227, identifier:tgt_type; 228, identifier:kwarg; 229, identifier:kwargs_passthru; 230, attribute; 231, argument_list; 232, identifier:ret; 233, identifier:digest; 234, subscript; 235, string; 236, subscript; 237, string; 238, attribute; 239, argument_list; 240, identifier:ret; 241, identifier:digest; 242, string_content:pool; 243, subscript; 244, identifier:k; 245, identifier:startswith; 246, string; 247, integer:2; 248, identifier:__opts__; 249, string; 250, identifier:hashlib; 251, identifier:sha256; 252, call; 253, identifier:ret; 254, identifier:digest; 255, string_content:pool; 256, identifier:ret; 257, identifier:digest; 258, string_content:result; 259, identifier:six; 260, identifier:text_type; 261, subscript; 262, subscript; 263, string; 264, string_content:_; 265, string_content:timeout; 266, attribute; 267, argument_list; 268, identifier:minions; 269, identifier:minion; 270, identifier:ret; 271, identifier:k; 272, string_content:pool; 273, call; 274, identifier:encode; 275, identifier:__salt_system_encoding__; 276, attribute; 277, argument_list; 278, identifier:six; 279, identifier:text_type; 280, subscript; 281, identifier:minions; 282, identifier:minion
code: def _get_pool_results(*args, **kwargs):
      '''
      A helper function which returns a dictionary of minion pools along with
      their matching result sets.
      Useful for developing other "survey style" functions.
      Optionally accepts a "survey_sort=up" or "survey_sort=down" kwargs for
      specifying sort order.
      Because the kwargs namespace of the "salt" and "survey" command are shared,
      the name "survey_sort" was chosen to help avoid option conflicts.
      '''
      # TODO: the option "survey.sort=" would be preferred for namespace
      # separation but the kwargs parser for the salt-run command seems to
      # improperly pass the options containing a "." in them for later modules to
      # process. The "_" is used here instead.
  
      import hashlib
  
      tgt = args[0]
      cmd = args[1]
      ret = {}
  
      sort = kwargs.pop('survey_sort', 'down')
      direction = sort != 'up'
  
      tgt_type = kwargs.pop('tgt_type', 'compound')
      if tgt_type not in ['compound', 'pcre']:
          tgt_type = 'compound'
  
      kwargs_passthru = dict((k, kwargs[k]) for k in six.iterkeys(kwargs) if not k.startswith('_'))
  
      client = salt.client.get_local_client(__opts__['conf_file'])
      try:
          minions = client.cmd(tgt, cmd, args[2:], timeout=__opts__['timeout'], tgt_type=tgt_type, kwarg=kwargs_passthru)
      except SaltClientError as client_error:
          print(client_error)
          return ret
  
      # hash minion return values as a string
      for minion in sorted(minions):
          digest = hashlib.sha256(six.text_type(minions[minion]).encode(__salt_system_encoding__)).hexdigest()
          if digest not in ret:
              ret[digest] = {}
              ret[digest]['pool'] = []
              ret[digest]['result'] = six.text_type(minions[minion])
  
          ret[digest]['pool'].append(minion)
  
      sorted_ret = []
      for k in sorted(ret, key=lambda k: len(ret[k]['pool']), reverse=direction):
          # return aggregated results, sorted by size of the hash pool
  
          sorted_ret.append(ret[k])
  
      return sorted_ret