Commit 6396164f authored by Guido Trotter's avatar Guido Trotter
Browse files

Move roman conversion to compat

The new TryToRoman function provides optional easy to use roman
conversion. Nunc cum demonstrationi unitati.
Signed-off-by: default avatarGuido Trotter <>
Reviewed-by: default avatarIustin Pop <>
parent b9b5abcb
......@@ -30,6 +30,11 @@ try:
except ImportError:
functools = None
import roman
except ImportError:
roman = None
def all(seq, pred=bool): # pylint: disable-msg=W0622
"""Returns True if pred(x) is True for every element in the iterable.
......@@ -82,6 +87,29 @@ def _partial(func, *args, **keywords): # pylint: disable-msg=W0622
return newfunc
def TryToRoman(val, convert=True):
"""Try to convert a value to roman numerals
If the roman module could be loaded convert the given value to a roman
numeral. Gracefully fail back to leaving the value untouched.
@type val: integer
@param val: value to convert
@type convert: boolean
@param convert: if False, don't try conversion at all
@rtype: string or typeof(val)
@return: roman numeral for val, or val if conversion didn't succeed
if roman is not None and convert:
return roman.toRoman(val)
except roman.RomanError:
return val
return val
if functools is None:
partial = _partial
......@@ -28,15 +28,11 @@
import sys
import roman
except ImportError:
roman = None
from ganeti.cli import *
from ganeti import opcodes
from ganeti import utils
from ganeti import constants
from ganeti import compat
from ganeti import errors
from ganeti import bootstrap
......@@ -232,12 +228,8 @@ def ListNodes(opts, args):
val = utils.FormatTime(val)
elif val is None:
val = "?"
elif (roman is not None and opts.roman_integers
and field in latinfriendlyfields):
val = roman.toRoman(val)
except roman.RomanError:
elif opts.roman_integers and field in latinfriendlyfields:
val = compat.TryToRoman(val)
row[idx] = str(val)
data = GenerateTable(separator=opts.separator, headers=headers,
......@@ -58,5 +58,26 @@ class TestPartial(testutils.GanetiTestCase):
(("Foo", ), {"xyz": 999,}))
class TestTryToRoman(testutils.GanetiTestCase):
"""test the compat.TryToRoman function"""
def testAFewIntegers(self):
self.assertEquals(compat.TryToRoman(0), 0)
self.assertEquals(compat.TryToRoman(1), "I")
self.assertEquals(compat.TryToRoman(4), "IV")
self.assertEquals(compat.TryToRoman(5), "V")
def testStrings(self):
self.assertEquals(compat.TryToRoman("astring"), "astring")
self.assertEquals(compat.TryToRoman("5"), "5")
def testDontConvert(self):
self.assertEquals(compat.TryToRoman(0, convert=False), 0)
self.assertEquals(compat.TryToRoman(1, convert=False), 1)
self.assertEquals(compat.TryToRoman(7, convert=False), 7)
self.assertEquals(compat.TryToRoman("astring", convert=False), "astring")
self.assertEquals(compat.TryToRoman("19", convert=False), "19")
if __name__ == "__main__":
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment