Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_idx_types(self):
assert all(isinstance(i, int) for i in self.ip.by_index.keys())
assert all(isinstance(i, basestring) for i in self.ip.by_name.keys())
def test_reprs(self):
assert isinstance(repr(self.ip.lo.ipaddr), basestring)
assert isinstance(repr(self.ip.lo), basestring)
def __getitem__(self, key):
if isinstance(key, (tuple, list)):
return self.raw[key]
elif isinstance(key, int):
return self.raw[tuple(self.raw.keys())[key]]
elif isinstance(key, basestring):
key = key.split('/')
key = (key[0], int(key[1]))
return self.raw[key]
else:
TypeError('wrong key type')
def adjust_spec(cls, spec):
if isinstance(spec, basestring):
ret = {'target': 'localhost'}
ret['address'], prefixlen = spec.split('/')
ret['prefixlen'] = int(prefixlen)
return ret
return spec
if header['type'] == 'seg6local':
# Init step
ret = {}
table = None
nh4 = None
nh6 = None
iif = None # Actually not used
oif = None
srh = {}
segs = []
hmac = None
# Parse segs
if srh:
segs = header['srh']['segs']
# If they are in the form in_addr6,in_addr6
if isinstance(segs, basestring):
# Create an array with the splitted values
temp = segs.split(',')
# Init segs
segs = []
# Iterate over the values
for seg in temp:
# Discard empty string
if seg != '':
# Add seg to segs
segs.append(seg)
# hmac is optional and contains the hmac key
hmac = header.get('hmac', None)
# Retrieve action
action = header['action']
if action == 'End.X':
# Retrieve nh6
constraint=lambda k, v: isinstance(k, basestring))
self.by_index = View(src=self.interfaces,
reverse=True)
# 8<--------------------------------------
for i in rip:
# When you remove a primary IP addr, all the
# subnetwork can be removed. In this case you
# will fail, but it is OK, no need to roll back
try:
run(nl.addr, 'delete', self['index'], i[0], i[1])
except NetlinkError as x:
# bypass only errno 99,
# 'Cannot assign address'
if x.code != errno.EADDRNOTAVAIL:
raise
except socket.error as x:
# bypass illegal IP requests
if isinstance(x.args[0], basestring) and \
x.args[0].startswith('illegal IP'):
continue
raise
###
# Add addresses
# 8<--------------------------------------
for i in ip2add:
# Try to fetch additional address attributes
try:
kwarg = dict([k for k
in transaction['ipaddr'][i].items()
if k[0] in ('broadcast',
'anycast',
'scope')])
except KeyError:
kwarg = None
def parse(self):
if hasattr(self.stream, 'readlines'):
for text in self.stream.readlines():
self.parse_string(text)
elif isinstance(self.stream, basestring):
self.parse_string(self.stream)
else:
raise ValueError('unsupported stream')
self.parsed = True
def decode(self):
self.data_length = struct.unpack('B', self.buf[self.offset + 1:
self.offset + 2])[0]
if self.policy is not None:
if self.policy['format'] == 'string':
fmt = '%is' % self.data_length
else:
fmt = self.policy['format']
value = struct.unpack(fmt, self.buf[self.offset + 2:
self.offset + 2 +
self.data_length])
if len(value) == 1:
value = value[0]
value = self.policy.get('decode', lambda x: x)(value)
if isinstance(value, basestring) and \
self.policy['format'] == 'string':
value = value[:value.find('\x00')]
self.value = value
else:
# remember current offset as msg.decode() will advance it
offset = self.offset
# move past the code and option length bytes so that msg.decode()
# starts parsing at the right location
self.offset += 2
msg.decode(self)
# restore offset so that dhcpmsg.decode() advances it correctly
self.offset = offset
return self
def apply(self, rollback=False, fallback=False):
# translate string link references into numbers
for key in ('link', ):
if key in self and isinstance(self[key], basestring):
self[key] = self.ndb.interfaces[self[key]]['index']
try:
super(Interface, self).apply(rollback)
except NetlinkError as e:
if e.code == 95 and \
'master' in self and \
self.state == 'invalid':
key = dict(self)
key['create'] = True
del key['master']
fb = type(self)(self.view, key)
fb.register()
fb.apply(rollback)
fb.set('master', self['master'])
fb.apply(rollback)
del fb