Revision 6396164f

b/lib/compat.py
30 30
except ImportError:
31 31
  functools = None
32 32

  
33
try:
34
  import roman
35
except ImportError:
36
  roman = None
37

  
33 38

  
34 39
def all(seq, pred=bool): # pylint: disable-msg=W0622
35 40
  """Returns True if pred(x) is True for every element in the iterable.
......
82 87
  return newfunc
83 88

  
84 89

  
90
def TryToRoman(val, convert=True):
91
  """Try to convert a value to roman numerals
92

  
93
  If the roman module could be loaded convert the given value to a roman
94
  numeral. Gracefully fail back to leaving the value untouched.
95

  
96
  @type val: integer
97
  @param val: value to convert
98
  @type convert: boolean
99
  @param convert: if False, don't try conversion at all
100
  @rtype: string or typeof(val)
101
  @return: roman numeral for val, or val if conversion didn't succeed
102

  
103
  """
104
  if roman is not None and convert:
105
    try:
106
      return roman.toRoman(val)
107
    except roman.RomanError:
108
      return val
109
  else:
110
    return val
111

  
112

  
85 113
if functools is None:
86 114
  partial = _partial
87 115
else:
b/scripts/gnt-node
28 28

  
29 29
import sys
30 30

  
31
try:
32
  import roman
33
except ImportError:
34
  roman = None
35

  
36 31
from ganeti.cli import *
37 32
from ganeti import opcodes
38 33
from ganeti import utils
39 34
from ganeti import constants
35
from ganeti import compat
40 36
from ganeti import errors
41 37
from ganeti import bootstrap
42 38

  
......
232 228
        val = utils.FormatTime(val)
233 229
      elif val is None:
234 230
        val = "?"
235
      elif (roman is not None and opts.roman_integers
236
            and field in latinfriendlyfields):
237
        try:
238
          val = roman.toRoman(val)
239
        except roman.RomanError:
240
          pass
231
      elif opts.roman_integers and field in latinfriendlyfields:
232
        val = compat.TryToRoman(val)
241 233
      row[idx] = str(val)
242 234

  
243 235
  data = GenerateTable(separator=opts.separator, headers=headers,
b/test/ganeti.compat_unittest.py
58 58
                           (("Foo", ), {"xyz": 999,}))
59 59

  
60 60

  
61
class TestTryToRoman(testutils.GanetiTestCase):
62
  """test the compat.TryToRoman function"""
63

  
64
  def testAFewIntegers(self):
65
    self.assertEquals(compat.TryToRoman(0), 0)
66
    self.assertEquals(compat.TryToRoman(1), "I")
67
    self.assertEquals(compat.TryToRoman(4), "IV")
68
    self.assertEquals(compat.TryToRoman(5), "V")
69

  
70
  def testStrings(self):
71
    self.assertEquals(compat.TryToRoman("astring"), "astring")
72
    self.assertEquals(compat.TryToRoman("5"), "5")
73

  
74
  def testDontConvert(self):
75
    self.assertEquals(compat.TryToRoman(0, convert=False), 0)
76
    self.assertEquals(compat.TryToRoman(1, convert=False), 1)
77
    self.assertEquals(compat.TryToRoman(7, convert=False), 7)
78
    self.assertEquals(compat.TryToRoman("astring", convert=False), "astring")
79
    self.assertEquals(compat.TryToRoman("19", convert=False), "19")
80

  
81

  
61 82
if __name__ == "__main__":
62 83
  testutils.GanetiTestProgram()

Also available in: Unified diff