Commit 898485cd authored by Naoki Kambe's avatar Naoki Kambe
Browse files

[trac930] rename the function name

 - rename the name of 'parse_spec' to 'get_spec_defaults' in the
   result of consideration of what it is doing

 - modify the description of the function as docstring

 - fix unitttests for the stats module depending on the function name
parent 30f48561
......@@ -68,12 +68,14 @@ def get_datetime(gmt=None):
if not gmt: gmt = gmtime()
return strftime("%Y-%m-%dT%H:%M:%SZ", gmt)
def parse_spec(spec):
def get_spec_defaults(spec):
"""
parse spec type data
extracts the default values of the items from spec specified in
arg, and returns the dict-type variable which is a set of the item
names and the default values
"""
if type(spec) is not list: return {}
def _parse_spec(spec):
def _get_spec_defaults(spec):
item_type = spec['item_type']
if item_type == "integer":
return int(spec.get('item_default', 0))
......@@ -86,14 +88,14 @@ def parse_spec(spec):
elif item_type == "list":
return spec.get(
"item_default",
[ _parse_spec(s) for s in spec["list_item_spec"] ])
[ _get_spec_defaults(s) for s in spec["list_item_spec"] ])
elif item_type == "map":
return spec.get(
"item_default",
dict([ (s["item_name"], _parse_spec(s)) for s in spec["map_item_spec"] ]) )
dict([ (s["item_name"], _get_spec_defaults(s)) for s in spec["map_item_spec"] ]) )
else:
return spec.get("item_default", None)
return dict([ (s['item_name'], _parse_spec(s)) for s in spec ])
return dict([ (s['item_name'], _get_spec_defaults(s)) for s in spec ])
class Callback():
"""
......@@ -137,7 +139,7 @@ class Stats:
name = "command_" + cmd["command_name"]
try:
callback = getattr(self, name)
kwargs = parse_spec(cmd["command_args"])
kwargs = get_spec_defaults(cmd["command_args"])
self.callbacks[name] = Callback(command=callback, kwargs=kwargs)
except AttributeError:
raise StatsError(STATS_UNKNOWN_COMMAND_IN_SPEC, cmd["command_name"])
......@@ -248,7 +250,7 @@ class Stats:
self.update_modules()
statistics_data = {}
for (name, module) in self.modules.items():
value = parse_spec(module.get_statistics_spec())
value = get_spec_defaults(module.get_statistics_spec())
if module.validate_statistics(True, value):
statistics_data[name] = value
for (name, value) in self.statistics_data.items():
......
......@@ -56,9 +56,9 @@ class TestUtilties(unittest.TestCase):
{ 'item_name': 'test_none', 'item_type': 'none' }
]
def test_parse_spec(self):
def test_get_spec_defaults(self):
self.assertEqual(
stats.parse_spec(self.items), {
stats.get_spec_defaults(self.items), {
'test_int1' : 12345 ,
'test_real1' : 12345.6789 ,
'test_bool1' : True ,
......@@ -72,8 +72,8 @@ class TestUtilties(unittest.TestCase):
'test_list2' : [0,0,0],
'test_map2' : { 'A' : 0, 'B' : 0, 'C' : 0 },
'test_none' : None })
self.assertEqual(stats.parse_spec(None), {})
self.assertRaises(KeyError, stats.parse_spec, [{'item_name':'Foo'}])
self.assertEqual(stats.get_spec_defaults(None), {})
self.assertRaises(KeyError, stats.get_spec_defaults, [{'item_name':'Foo'}])
def test_get_timestamp(self):
self.assertEqual(stats.get_timestamp(), 1308730448.965706)
......@@ -280,7 +280,7 @@ class TestStats(unittest.TestCase):
self.assertTrue('Stats' in self.stats.modules)
self.assertTrue('Boss' in self.stats.modules)
self.assertFalse('Dummy' in self.stats.modules)
my_statistics_data = stats.parse_spec(self.stats.modules['Stats'].get_statistics_spec())
my_statistics_data = stats.get_spec_defaults(self.stats.modules['Stats'].get_statistics_spec())
self.assertTrue('report_time' in my_statistics_data)
self.assertTrue('boot_time' in my_statistics_data)
self.assertTrue('last_update_time' in my_statistics_data)
......@@ -291,7 +291,7 @@ class TestStats(unittest.TestCase):
self.assertEqual(my_statistics_data['last_update_time'], "1970-01-01T00:00:00Z")
self.assertEqual(my_statistics_data['timestamp'], 0.0)
self.assertEqual(my_statistics_data['lname'], "")
my_statistics_data = stats.parse_spec(self.stats.modules['Boss'].get_statistics_spec())
my_statistics_data = stats.get_spec_defaults(self.stats.modules['Boss'].get_statistics_spec())
self.assertTrue('boot_time' in my_statistics_data)
self.assertEqual(my_statistics_data['boot_time'], "1970-01-01T00:00:00Z")
......
Supports Markdown
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