From eb62069ee64c2cb9f745970844a5799234171853 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 3 Aug 2011 11:47:41 +0200
Subject: [PATCH] Add two more compat functions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

operator.itemgetter(0) β†’ fst
operator.itemgetter(1) β†’ snd

snd is not used yet, but it makes sense to add both.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/build/sphinx_ext.py | 6 +++---
 lib/compat.py           | 8 +++++++-
 lib/locking.py          | 4 ++--
 lib/opcodes.py          | 3 ++-
 lib/query.py            | 3 +--
 5 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/lib/build/sphinx_ext.py b/lib/build/sphinx_ext.py
index 7273a7509..a336695a7 100644
--- a/lib/build/sphinx_ext.py
+++ b/lib/build/sphinx_ext.py
@@ -44,7 +44,7 @@ from ganeti import rapi
 import ganeti.rapi.rlib2 # pylint: disable-msg=W0611
 
 
-COMMON_PARAM_NAMES = map(operator.itemgetter(0), opcodes.OpCode.OP_PARAMS)
+COMMON_PARAM_NAMES = map(compat.fst, opcodes.OpCode.OP_PARAMS)
 
 #: Namespace for evaluating expressions
 EVAL_NS = dict(compat=compat, constants=constants, utils=utils, errors=errors,
@@ -98,7 +98,7 @@ def _BuildOpcodeParams(op_id, include, exclude, alias):
   params_with_alias = \
     utils.NiceSort([(alias.get(name, name), name, default, test, doc)
                     for (name, default, test, doc) in op_cls.GetAllParams()],
-                   key=operator.itemgetter(0))
+                   key=compat.fst)
 
   for (rapi_name, name, default, test, doc) in params_with_alias:
     # Hide common parameters if not explicitely included
@@ -227,7 +227,7 @@ def BuildQueryFields(fields):
 
   """
   for (_, (fdef, _, _, _)) in utils.NiceSort(fields.items(),
-                                             key=operator.itemgetter(0)):
+                                             key=compat.fst):
     assert len(fdef.doc.splitlines()) == 1
     yield "``%s``" % fdef.name
     yield "  %s" % fdef.doc
diff --git a/lib/compat.py b/lib/compat.py
index 1d75c37de..a1d35da55 100644
--- a/lib/compat.py
+++ b/lib/compat.py
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2010 Google Inc.
+# Copyright (C) 2010, 2011 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@
 """
 
 import itertools
+import operator
 
 try:
   # pylint: disable-msg=F0401
@@ -143,3 +144,8 @@ def TryToRoman(val, convert=True):
   else:
     return val
 
+#: returns the first element of a list-like value
+fst = operator.itemgetter(0)
+
+#: returns the second element of a list-like value
+snd = operator.itemgetter(1)
diff --git a/lib/locking.py b/lib/locking.py
index 1b46e5f54..df366583e 100644
--- a/lib/locking.py
+++ b/lib/locking.py
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
+# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -1729,7 +1729,7 @@ class LockMonitor(object):
                       key=_MonitorSortKey)
 
     # Extract lock information and build query data
-    return (qobj, query.LockQueryData(map(operator.itemgetter(0), lockinfo)))
+    return (qobj, query.LockQueryData(map(compat.fst, lockinfo)))
 
   def QueryLocks(self, fields):
     """Queries information from all locks.
diff --git a/lib/opcodes.py b/lib/opcodes.py
index e00d651c4..cb55d6e46 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -37,6 +37,7 @@ import logging
 import re
 import operator
 
+from ganeti import compat
 from ganeti import constants
 from ganeti import errors
 from ganeti import ht
@@ -135,7 +136,7 @@ _OPID_RE = re.compile("([a-z])([A-Z])")
 #: Utility function for L{OpClusterSetParams}
 _TestClusterOsList = ht.TOr(ht.TNone,
   ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
-    ht.TMap(ht.WithDesc("GetFirstItem")(operator.itemgetter(0)),
+    ht.TMap(ht.WithDesc("GetFirstItem")(compat.fst),
             ht.TElemOf(constants.DDMS_VALUES)))))
 
 
diff --git a/lib/query.py b/lib/query.py
index 3b8133eb2..ae7da5aa6 100644
--- a/lib/query.py
+++ b/lib/query.py
@@ -2018,8 +2018,7 @@ def _BuildOsFields():
      None, 0, _ConvWrap(sorted, _GetItemAttr("api_versions"))),
     (_MakeField("parameters", "Parameters", QFT_OTHER,
                 "Operating system parameters"),
-     None, 0, _ConvWrap(compat.partial(utils.NiceSort,
-                                       key=operator.itemgetter(0)),
+     None, 0, _ConvWrap(compat.partial(utils.NiceSort, key=compat.fst),
                         _GetItemAttr("parameters"))),
     (_MakeField("node_status", "NodeStatus", QFT_OTHER,
                 "Status from node"),
-- 
GitLab