本文整理汇总了Python中pyroute2.ipdb.transactional.Transactional.__setitem__方法的典型用法代码示例。如果您正苦于以下问题:Python Transactional.__setitem__方法的具体用法?Python Transactional.__setitem__怎么用?Python Transactional.__setitem__使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.ipdb.transactional.Transactional
的用法示例。
在下文中一共展示了Transactional.__setitem__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __setitem__
# 需要导入模块: from pyroute2.ipdb.transactional import Transactional [as 别名]
# 或者: from pyroute2.ipdb.transactional.Transactional import __setitem__ [as 别名]
def __setitem__(self, key, value):
ret = value
if (key in ('encap', 'metrics')) and isinstance(value, dict):
# transactionals attach as is
if type(value) in (Encap, Metrics):
with self._direct_state:
return Transactional.__setitem__(self, key, value)
# check, if it exists already
ret = Transactional.__getitem__(self, key)
# it doesn't
# (plain dict can be safely discarded)
if (type(ret) == dict) or not ret:
# bake transactionals in place
if key == 'encap':
ret = Encap(parent=self)
elif key == 'metrics':
ret = Metrics(parent=self)
# attach transactional to the route
with self._direct_state:
Transactional.__setitem__(self, key, ret)
# begin() works only if the transactional is attached
if any(value.values()):
if self._mode in ('implicit', 'explicit'):
ret._begin(tid=self.current_tx.uid)
[ret.__setitem__(k, v) for k, v
in value.items() if v is not None]
# corresponding transactional exists
else:
# set fields
for k in ret:
ret[k] = value.get(k, None)
return
elif key == 'multipath':
cur = Transactional.__getitem__(self, key)
if isinstance(cur, NextHopSet):
# load entries
vs = NextHopSet(value)
for key in vs - cur:
cur.add(key)
for key in cur - vs:
cur.remove(key)
else:
# drop any result of `update()`
Transactional.__setitem__(self, key, NextHopSet(value))
return
elif key == 'encap_type' and not isinstance(value, int):
ret = encap_type.get(value, value)
elif key == 'type' and not isinstance(value, int):
ret = rt_type.get(value, value)
elif key == 'proto' and not isinstance(value, int):
ret = rt_proto.get(value, value)
elif key == 'dst' and \
isinstance(value, basestring) and \
value in ('0.0.0.0/0', '::/0'):
ret = 'default'
Transactional.__setitem__(self, key, ret)