nodes: 0, module; 1, function_definition; 2, function_name:sort_flavor_list; 3, parameters; 4, block; 5, identifier:request; 6, identifier:flavors; 7, default_parameter; 8, expression_statement; 9, function_definition; 10, try_statement; 11, identifier:with_menu_label; 12, True; 13, comment:"""Utility method to sort a list of flavors.
  
      By default, returns the available flavors, sorted by RAM usage (ascending).
      Override these behaviours with a ``CREATE_INSTANCE_FLAVOR_SORT`` dict
      in ``local_settings.py``.
      """; 14, function_name:get_key; 15, parameters; 16, block; 17, block; 18, except_clause; 19, identifier:flavor; 20, identifier:sort_key; 21, try_statement; 22, expression_statement; 23, expression_statement; 24, expression_statement; 25, if_statement; 26, if_statement; 27, return_statement; 28, identifier:Exception; 29, block; 30, block; 31, except_clause; 32, assignment; 33, assignment; 34, assignment; 35, not_operator; 36, block; 37, else_clause; 38, identifier:with_menu_label; 39, block; 40, else_clause; 41, identifier:flavor_list; 42, expression_statement; 43, return_statement; 44, return_statement; 45, identifier:AttributeError; 46, block; 47, identifier:flavor_sort; 48, call; 49, identifier:sort_key; 50, call; 51, identifier:rev; 52, call; 53, call; 54, function_definition; 55, block; 56, expression_statement; 57, block; 58, call; 59, list; 60, call; 61, expression_statement; 62, return_statement; 63, identifier:getattr; 64, argument_list; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, identifier:callable; 70, argument_list; 71, function_name:key; 72, parameters; 73, block; 74, expression_statement; 75, assignment; 76, expression_statement; 77, attribute; 78, argument_list; 79, identifier:getattr; 80, argument_list; 81, call; 82, call; 83, identifier:settings; 84, string; 85, dictionary; 86, identifier:flavor_sort; 87, identifier:get; 88, string; 89, string; 90, identifier:flavor_sort; 91, identifier:get; 92, string; 93, False; 94, identifier:sort_key; 95, identifier:flavor; 96, return_statement; 97, assignment; 98, identifier:flavor_list; 99, list_comprehension; 100, assignment; 101, identifier:exceptions; 102, identifier:handle; 103, identifier:request; 104, call; 105, identifier:flavor; 106, identifier:sort_key; 107, attribute; 108, argument_list; 109, identifier:getattr; 110, argument_list; 111, string_content:CREATE_INSTANCE_FLAVOR_SORT; 112, string_content:key; 113, string_content:ram; 114, string_content:reverse; 115, call; 116, identifier:key; 117, identifier:sort_key; 118, tuple; 119, for_in_clause; 120, identifier:flavor_list; 121, call; 122, identifier:_; 123, argument_list; 124, identifier:LOG; 125, identifier:warning; 126, concatenated_string; 127, identifier:sort_key; 128, identifier:flavor; 129, string; 130, identifier:get_key; 131, argument_list; 132, attribute; 133, binary_operator:'%s' % flavor.name; 134, identifier:flavor; 135, call; 136, identifier:sorted; 137, argument_list; 138, string; 139, string:'Could not find sort key "%s". Using the default '; 140, string:'"ram" instead.'; 141, string_content:ram; 142, identifier:flavor; 143, identifier:sort_key; 144, identifier:flavor; 145, identifier:id; 146, string; 147, attribute; 148, identifier:sorted; 149, argument_list; 150, identifier:flavors; 151, keyword_argument; 152, keyword_argument; 153, string_content:Unable to sort instance flavors.; 154, string_content:%s; 155, identifier:flavor; 156, identifier:name; 157, identifier:flavors; 158, keyword_argument; 159, keyword_argument; 160, identifier:key; 161, identifier:key; 162, identifier:reverse; 163, identifier:rev; 164, identifier:key; 165, identifier:key; 166, identifier:reverse; 167, identifier:rev
code: def sort_flavor_list(request, flavors, with_menu_label=True):
      """Utility method to sort a list of flavors.
  
      By default, returns the available flavors, sorted by RAM usage (ascending).
      Override these behaviours with a ``CREATE_INSTANCE_FLAVOR_SORT`` dict
      in ``local_settings.py``.
      """
      def get_key(flavor, sort_key):
          try:
              return getattr(flavor, sort_key)
          except AttributeError:
              LOG.warning('Could not find sort key "%s". Using the default '
                          '"ram" instead.', sort_key)
              return getattr(flavor, 'ram')
      try:
          flavor_sort = getattr(settings, 'CREATE_INSTANCE_FLAVOR_SORT', {})
          sort_key = flavor_sort.get('key', 'ram')
          rev = flavor_sort.get('reverse', False)
          if not callable(sort_key):
              def key(flavor):
                  return get_key(flavor, sort_key)
          else:
              key = sort_key
  
          if with_menu_label:
              flavor_list = [(flavor.id, '%s' % flavor.name)
                             for flavor in sorted(flavors, key=key, reverse=rev)]
          else:
              flavor_list = sorted(flavors, key=key, reverse=rev)
          return flavor_list
      except Exception:
          exceptions.handle(request,
                            _('Unable to sort instance flavors.'))
          return []