本文整理汇总了Python中Shine.Configuration.ModelFile.ModelFile.add_custom方法的典型用法代码示例。如果您正苦于以下问题:Python ModelFile.add_custom方法的具体用法?Python ModelFile.add_custom怎么用?Python ModelFile.add_custom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Shine.Configuration.ModelFile.ModelFile
的用法示例。
在下文中一共展示了ModelFile.add_custom方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCompoundElement
# 需要导入模块: from Shine.Configuration.ModelFile import ModelFile [as 别名]
# 或者: from Shine.Configuration.ModelFile.ModelFile import add_custom [as 别名]
def testCompoundElement(self):
"""test a ModelFile with a compound element"""
class MyCompound(ModelFile):
def __init__(self, sep='=', linesep=' '):
ModelFile.__init__(self, sep, linesep)
self.add_element('dev', check='path')
self.add_element('index', check='digit')
self.add_element('jdev', check='path')
self.add_element('mode', check='enum', default='managed',
values=['managed', 'external'])
elem = MyCompound()
elem.parse("dev=/dev/sdb jdev=/dev/sdc index=4")
self.assertEqual(elem.get('dev'), '/dev/sdb')
self.assertEqual(elem.get('jdev'), '/dev/sdc')
self.assertEqual(elem.get('index'), 4)
self.assertEqual(elem.get('mode'), 'managed')
model = ModelFile()
model.add_element('fsname', check='string')
model.add_custom('mgt', MyCompound(), multiple=True)
model.add_custom('mdt', MyCompound(), multiple=True)
model.add_custom('ost', MyCompound(), multiple=True)
model.parse("ost: dev=/dev/sdb index=4")
self.assertEqual(len(model.get('ost')), 1)
self.assertEqual(model.get('ost')[0].get('dev'), '/dev/sdb')
self.assertEqual(model.get('ost')[0].get('index'), 4)
model.parse("ost: dev=/dev/sdd index=5")
self.assertEqual(len(model.get('ost')), 2)
self.assertEqual(model.get('ost')[1].get('dev'), '/dev/sdd')
self.assertEqual(model.get('ost')[1].get('index'), 5)
self.assertEqual(str(model), "ost:index=4 dev=/dev/sdb\n"
"ost:index=5 dev=/dev/sdd")
示例2: testDiffDictMultipleElement
# 需要导入模块: from Shine.Configuration.ModelFile import ModelFile [as 别名]
# 或者: from Shine.Configuration.ModelFile.ModelFile import add_custom [as 别名]
def testDiffDictMultipleElement(self):
"""diff between 2 modelfiles with a multiple subelement"""
model = ModelFile()
element = ModelFile(sep="=", linesep=" ")
element.add_element('dev', check='string')
element.add_element('index', check='digit')
model.add_custom('target', element, multiple=True)
model2 = model.emptycopy()
# Empty models have no difference
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(), {})
self.assertEqual(removed.as_dict(), {})
self.assertEqual(changed.as_dict(), {})
# Same model have no difference
model.parse('target: dev=/dev/sda index=1')
model2.parse('target: dev=/dev/sda index=1')
self.assertEqual(len(model['target']), 1)
self.assertEqual(len(model2['target']), 1)
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(), {})
self.assertEqual(removed.as_dict(), {})
self.assertEqual(changed.as_dict(), {})
# Add a new element
model2.parse('target: dev=/dev/sdb index=2')
self.assertEqual(len(model['target']), 1)
self.assertEqual(len(model2['target']), 2)
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(),
{'target': [{'index': 2, 'dev': '/dev/sdb'}]})
self.assertEqual(removed.as_dict(), {})
self.assertEqual(changed.as_dict(), {})
# Change an element
del model2['target']
model2.parse('target: dev=/dev/sdb index=1')
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(),
{'target': [{'index': 1, 'dev': '/dev/sdb'}]})
self.assertEqual(removed.as_dict(),
{'target': [{'index': 1, 'dev': '/dev/sda'}]})
self.assertEqual(changed.as_dict(), {})
示例3: testAddCustomElement
# 需要导入模块: from Shine.Configuration.ModelFile import ModelFile [as 别名]
# 或者: from Shine.Configuration.ModelFile.ModelFile import add_custom [as 别名]
def testAddCustomElement(self):
"""model file uses a user-defined Element"""
class ElemNodeSet(SimpleElement):
def __init__(self, check='string', default=None, values=None):
SimpleElement.__init__(self, 'string', default, values)
def _validate(self, value):
try:
return NodeSet(value)
except:
raise ModelFileValueError
model = ModelFile()
model.add_custom('nodes', ElemNodeSet(), multiple=True)
model.add_custom('nids', ElemNodeSet(), multiple=True)
model.add('nodes', 'foo[1-5]')
self.assertEqual(str(model), "nodes:foo[1-5]")
self.assertEqual([str(item) for item in model.get('nodes')],
[str(NodeSet('foo[1-5]'))])
self.assertRaises(ModelFileValueError, model.add, 'nodes', 'bad[15')
示例4: testDiffDictElement
# 需要导入模块: from Shine.Configuration.ModelFile import ModelFile [as 别名]
# 或者: from Shine.Configuration.ModelFile.ModelFile import add_custom [as 别名]
def testDiffDictElement(self):
"""diff between 2 modelfiles with a subelement"""
model = ModelFile()
element = ModelFile(sep="=", linesep=" ")
element.add_element('size', check='digit')
element.add_element('count', check='digit')
model.add_custom('stripe', element)
model2 = model.emptycopy()
# Empty models have no difference
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(), {})
self.assertEqual(removed.as_dict(), {})
self.assertEqual(changed.as_dict(), {})
# Same model have no difference
model.parse('stripe: count=1 size=1000000')
model2.parse('stripe: count=1 size=1000000')
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(), {})
self.assertEqual(removed.as_dict(), {})
self.assertEqual(changed.as_dict(), {})
# Remove an attribute
del model2.get('stripe')['count']
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(), {})
self.assertEqual(removed.as_dict(), {'stripe': {'count': 1}})
self.assertEqual(changed.as_dict(), {})
# Change an attribute
model2.get('stripe').add('count', 2)
added, changed, removed = model.diff(model2)
self.assertEqual(added.as_dict(), {})
self.assertEqual(removed.as_dict(), {})
self.assertEqual(changed.as_dict(), {'stripe': {'count': 2}})