refactored usages of iteritems, itervalues, and iterkeys to be 2/3 compliant

This commit is contained in:
bharnden 2019-05-05 21:23:43 -07:00
parent 8d6bf54a16
commit 1e98175e35
15 changed files with 116 additions and 66 deletions

View file

@ -466,7 +466,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
node = self.get_node(session, request.id, context)
interfaces = []
for interface_id, interface in node._netif.iteritems():
for interface_id in node._netif:
interface = node._netif[interface_id]
net_id = None
if interface.net:
net_id = interface.net.id
@ -628,7 +629,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
logging.debug("get hooks: %s", request)
session = self.get_session(request.session, context)
hooks = []
for state, state_hooks in session._hooks.iteritems():
for state in session._hooks:
state_hooks = session._hooks[state]
for file_name, file_data in state_hooks:
hook = core_pb2.Hook(state=state, file=file_name, data=file_data)
hooks.append(hook)
@ -645,10 +647,11 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
logging.debug("get mobility configs: %s", request)
session = self.get_session(request.session, context)
response = core_pb2.GetMobilityConfigsResponse()
for node_id, model_config in session.mobility.node_configurations.iteritems():
for node_id in session.mobility.node_configurations:
model_config = session.mobility.node_configurations[node_id]
if node_id == -1:
continue
for model_name in model_config.iterkeys():
for model_name in model_config:
if model_name != Ns2ScriptedMobility.name:
continue
config = session.mobility.get_model_config(node_id, model_name)
@ -687,7 +690,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
def GetServices(self, request, context):
logging.debug("get services: %s", request)
services = []
for service in ServiceManager.services.itervalues():
for name in ServiceManager.services:
service = ServiceManager.services[name]
service_proto = core_pb2.Service(group=service.group, name=service.name)
services.append(service_proto)
return core_pb2.GetServicesResponse(services=services)
@ -846,11 +850,12 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
logging.debug("get emane model configs: %s", request)
session = self.get_session(request.session, context)
response = core_pb2.GetEmaneModelConfigsResponse()
for node_id, model_config in session.emane.node_configurations.iteritems():
for node_id in session.emane.node_configurations:
model_config = session.emane.node_configurations[node_id]
if node_id == -1:
continue
for model_name in model_config.iterkeys():
for model_name in model_config:
model = session.emane.models[model_name]
config = session.emane.get_model_config(node_id, model_name)
config_groups = get_config_groups(config, model)

View file

@ -160,7 +160,8 @@ class CoreBroker(object):
logging.info("clearing state")
self.nodemap_lock.acquire()
self.nodemap.clear()
for server, count in self.nodecounts.iteritems():
for server in self.nodecounts:
count = self.nodecounts[server]
if count < 1:
self.delserver(server)
self.nodecounts.clear()
@ -200,7 +201,8 @@ class CoreBroker(object):
rlist = []
with self.servers_lock:
# build a socket list for select call
for server in self.servers.itervalues():
for name in self.servers:
server = self.servers[name]
if server.sock is not None:
rlist.append(server.sock)
r, _w, _x = select.select(rlist, [], [], 1.0)
@ -349,7 +351,8 @@ class CoreBroker(object):
:rtype: CoreDistributedServer
"""
with self.servers_lock:
for server in self.servers.itervalues():
for name in self.servers:
server = self.servers[name]
if server.sock == sock:
return server
return None
@ -1041,7 +1044,8 @@ class CoreBroker(object):
:rtype: bool
"""
with self.servers_lock:
for server in self.servers.itervalues():
for name in self.servers:
server = self.servers[name]
if not server.instantiation_complete:
return False
return True

View file

@ -811,7 +811,8 @@ class CoreMessage(object):
"""
result = "%s <msgtype = %s, flags = %s>" % (self.__class__.__name__, self.type_str(), self.flag_str())
for key, value in self.tlv_data.iteritems():
for key in self.tlv_data:
value = self.tlv_data[key]
try:
tlv_type = self.tlv_class.tlv_type_map(key).name
except ValueError:

View file

@ -158,7 +158,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
num_sessions = 0
with self._sessions_lock:
for _id, session in self.coreemu.sessions.iteritems():
for _id in self.coreemu.sessions:
session = self.coreemu.sessions[_id]
num_sessions += 1
id_list.append(str(_id))
@ -167,10 +168,10 @@ class CoreHandler(SocketServer.BaseRequestHandler):
name = ""
name_list.append(name)
file = session.file_name
if not file:
file = ""
file_list.append(file)
file_name = session.file_name
if not file_name:
file_name = ""
file_list.append(file_name)
node_count_list.append(str(session.get_node_count()))
@ -378,11 +379,13 @@ class CoreHandler(SocketServer.BaseRequestHandler):
tlv_data += coreapi.CoreRegisterTlv.pack(self.session.broker.config_type, self.session.broker.name)
tlv_data += coreapi.CoreRegisterTlv.pack(self.session.location.config_type, self.session.location.name)
tlv_data += coreapi.CoreRegisterTlv.pack(self.session.mobility.config_type, self.session.mobility.name)
for model_class in self.session.mobility.models.itervalues():
for model_name in self.session.mobility.models:
model_class = self.session.mobility.models[model_name]
tlv_data += coreapi.CoreRegisterTlv.pack(model_class.config_type, model_class.name)
tlv_data += coreapi.CoreRegisterTlv.pack(self.session.services.config_type, self.session.services.name)
tlv_data += coreapi.CoreRegisterTlv.pack(self.session.emane.config_type, self.session.emane.name)
for model_class in self.session.emane.models.itervalues():
for model_name in self.session.emane.models:
model_class = self.session.emane.models[model_name]
tlv_data += coreapi.CoreRegisterTlv.pack(model_class.config_type, model_class.name)
tlv_data += coreapi.CoreRegisterTlv.pack(self.session.options.config_type, self.session.options.name)
tlv_data += coreapi.CoreRegisterTlv.pack(self.session.metadata.config_type, self.session.metadata.name)
@ -904,7 +907,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
self.master = True
# find the session containing this client and set the session to master
for session in self.coreemu.sessions.itervalues():
for _id in self.coreemu.sessions:
session = self.coreemu.sessions[_id]
if self in session.broker.session_clients:
logging.debug("setting session to master: %s", session.id)
session.master = True
@ -995,7 +999,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
replies.append(config_response)
elif message_type != ConfigFlags.RESET and config_data.data_values:
values = ConfigShim.str_to_dict(config_data.data_values)
for key, value in values.iteritems():
for key in values:
value = values[key]
self.session.options.set_config(key, value)
return replies
@ -1025,7 +1030,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
replies = []
if message_type == ConfigFlags.REQUEST:
node_id = config_data.node
data_values = "|".join(["%s=%s" % item for item in self.session.metadata.get_configs().iteritems()])
metadata_configs = self.session.metadata.get_configs()
data_values = "|".join(["%s=%s" % (x, metadata_configs[x]) for x in metadata_configs])
data_types = tuple(ConfigDataTypes.STRING.value for _ in self.session.metadata.get_configs())
config_response = ConfigData(
message_type=0,
@ -1038,7 +1044,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
replies.append(config_response)
elif message_type != ConfigFlags.RESET and config_data.data_values:
values = ConfigShim.str_to_dict(config_data.data_values)
for key, value in values.iteritems():
for key in values:
value = values[key]
self.session.metadata.set_config(key, value)
return replies
@ -1097,7 +1104,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
# sort groups by name and map services to groups
groups = set()
group_map = {}
for service_name in ServiceManager.services.itervalues():
for name in ServiceManager.services:
service_name = ServiceManager.services[name]
group = service_name.group
groups.add(group)
group_map.setdefault(group, []).append(service_name)
@ -1212,7 +1220,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
raise ValueError("custom service(%s) for node(%s) does not exist", service_name, node_id)
values = ConfigShim.str_to_dict(values)
for name, value in values.iteritems():
for name in values:
value = values[name]
ServiceShim.setvalue(service, name, value)
return replies
@ -1679,7 +1688,8 @@ class CoreHandler(SocketServer.BaseRequestHandler):
nodes_data = []
links_data = []
with self.session._nodes_lock:
for node in self.session.nodes.itervalues():
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
node_data = node.data(message_type=MessageFlags.ADD.value)
if node_data:
nodes_data.append(node_data)
@ -1697,7 +1707,9 @@ class CoreHandler(SocketServer.BaseRequestHandler):
# send mobility model info
for node_id in self.session.mobility.nodes():
for model_name, config in self.session.mobility.get_all_configs(node_id).iteritems():
mobility_configs = self.session.mobility.get_all_configs(node_id)
for model_name in mobility_configs:
config = mobility_configs[model_name]
model_class = self.session.mobility.models[model_name]
logging.debug("mobility config: node(%s) class(%s) values(%s)", node_id, model_class, config)
config_data = ConfigShim.config_data(0, node_id, ConfigFlags.UPDATE.value, model_class, config)
@ -1705,7 +1717,9 @@ class CoreHandler(SocketServer.BaseRequestHandler):
# send emane model info
for node_id in self.session.emane.nodes():
for model_name, config in self.session.emane.get_all_configs(node_id).iteritems():
emane_configs = self.session.emane.get_all_configs(node_id)
for model_name in emane_configs:
config = emane_configs[model_name]
model_class = self.session.emane.models[model_name]
logging.debug("emane config: node(%s) class(%s) values(%s)", node_id, model_class, config)
config_data = ConfigShim.config_data(0, node_id, ConfigFlags.UPDATE.value, model_class, config)
@ -1761,7 +1775,7 @@ class CoreHandler(SocketServer.BaseRequestHandler):
# send session metadata
metadata_configs = self.session.metadata.get_configs()
if metadata_configs:
data_values = "|".join(["%s=%s" % item for item in metadata_configs.iteritems()])
data_values = "|".join(["%s=%s" % (x, metadata_configs[x]) for x in metadata_configs])
data_types = tuple(ConfigDataTypes.STRING.value for _ in self.session.metadata.get_configs())
config_data = ConfigData(
message_type=0,

View file

@ -150,7 +150,7 @@ class ConfigurableManager(object):
:return: list of node ids
:rtype: list
"""
return [node_id for node_id in self.node_configurations.iterkeys() if node_id != self._default_node]
return [x for x in self.node_configurations if x != self._default_node]
def config_reset(self, node_id=None):
"""
@ -329,7 +329,8 @@ class ModelManager(ConfigurableManager):
model_config = self.get_model_config(node_id, model_name)
if not config:
config = {}
for key, value in config.iteritems():
for key in config:
value = config[key]
model_config[key] = value
# set as node model for startup
@ -388,7 +389,8 @@ class ModelManager(ConfigurableManager):
all_configs = {}
models = []
for model_name, config in all_configs.iteritems():
for model_name in all_configs:
config = all_configs[model_name]
if model_name == ModelManager._default_node:
continue
model_class = self.models[model_name]

View file

@ -250,7 +250,8 @@ class EmaneManager(ModelManager):
# TODO: drive this from the session object
with self.session._nodes_lock:
for node in self.session.nodes.itervalues():
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
if nodeutils.is_node(node, NodeTypes.EMANE):
logging.debug("adding emane node: id(%s) name(%s)", node.id, node.name)
self.add_node(node)
@ -318,7 +319,8 @@ class EmaneManager(ModelManager):
self.startdaemons()
self.installnetifs()
for emane_node in self._emane_nodes.itervalues():
for node_id in self._emane_nodes:
emane_node = self._emane_nodes[node_id]
for netif in emane_node.netifs():
nems.append((netif.node.name, netif.name, emane_node.getnemid(netif)))
@ -552,7 +554,8 @@ class EmaneManager(ModelManager):
Return the number of NEMs emulated locally.
"""
count = 0
for emane_node in self._emane_nodes.itervalues():
for node_id in self._emane_nodes:
emane_node = self._emane_nodes[node_id]
count += len(emane_node.netifs())
return count

View file

@ -100,7 +100,8 @@ class Session(object):
self.options = SessionConfig()
if not config:
config = {}
for key, value in config.iteritems():
for key in config:
value = config[key]
self.options.set_config(key, value)
self.metadata = SessionMetaData()
@ -1244,12 +1245,15 @@ class Session(object):
"""
with self._nodes_lock:
count = len([x for x in self.nodes.itervalues()
if not nodeutils.is_node(x, (NodeTypes.PEER_TO_PEER, NodeTypes.CONTROL_NET))])
count = 0
for node_id in self.nodes:
node = self.nodes[node_id]
is_p2p_ctrlnet = nodeutils.is_node(node, (NodeTypes.PEER_TO_PEER, NodeTypes.CONTROL_NET))
is_tap = nodeutils.is_node(node, NodeTypes.TAP_BRIDGE) and not nodeutils.is_node(x, NodeTypes.TUNNEL)
if is_p2p_ctrlnet or is_tap:
continue
# on Linux, GreTapBridges are auto-created, not part of GUI's node count
count -= len([x for x in self.nodes.itervalues()
if nodeutils.is_node(x, NodeTypes.TAP_BRIDGE) and not nodeutils.is_node(x, NodeTypes.TUNNEL)])
count += 1
return count
@ -1286,10 +1290,11 @@ class Session(object):
# stop node services
with self._nodes_lock:
for obj in self.nodes.itervalues():
for node_id in self.nodes:
node = self.nodes[node_id]
# TODO: determine if checking for CoreNode alone is ok
if isinstance(obj, core.nodes.base.CoreNodeBase):
self.services.stop_services(obj)
if isinstance(node, core.nodes.base.CoreNodeBase):
self.services.stop_services(node)
# shutdown emane
self.emane.shutdown()

View file

@ -73,7 +73,7 @@ class MobilityManager(ModelManager):
logging.warn("skipping mobility configuration for unknown node: %s", node_id)
continue
for model_name in self.models.iterkeys():
for model_name in self.models:
config = self.get_configs(node_id, model_name)
if not config:
continue

View file

@ -120,9 +120,9 @@ class NodeBase(object):
:rtype: list
"""
if sort:
return map(lambda k: self._netif[k], sorted(self._netif.keys()))
return [self._netif[x] for x in sorted(self._netif)]
else:
return self._netif.itervalues()
return self._netif.values()
def numnetif(self):
"""

View file

@ -9,7 +9,8 @@ _NODE_MAP = None
def _log_map():
global _NODE_MAP
for key, value in _NODE_MAP.iteritems():
for key in _NODE_MAP:
value = _NODE_MAP[key]
name = None
if value:
name = value.__name__
@ -72,7 +73,7 @@ def get_node_type(node_class):
:rtype: core.enumerations.NodeTypes
"""
global _NODE_MAP
node_type_map = {v: k for k, v in _NODE_MAP.iteritems()}
node_type_map = {_NODE_MAP[x]: x for x in _NODE_MAP}
return node_type_map.get(node_class)

View file

@ -321,7 +321,8 @@ class Sdt(object):
"""
nets = []
with self.session._nodes_lock:
for node in self.session.nodes.itervalues():
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
if isinstance(node, CoreNetworkBase):
nets.append(node)
if not isinstance(node, NodeBase):

View file

@ -60,7 +60,8 @@ class ServiceDependencies(object):
:rtype: list[core.coreservices.CoreService]
"""
paths = []
for service in self.node_services.itervalues():
for name in self.node_services:
service = self.node_services[name]
if service.name in self.booted:
logging.debug("skipping service that will already be booted: %s", service.name)
continue
@ -69,7 +70,7 @@ class ServiceDependencies(object):
if path:
paths.append(path)
if self.booted != set(self.node_services.iterkeys()):
if self.booted != set(self.node_services):
raise ValueError("failure to boot all services: %s != %s" % (self.booted, self.node_services.keys()))
return paths
@ -389,8 +390,10 @@ class CoreServices(object):
:rtype: list[tuple]
"""
configs = []
for node_id in self.custom_services.iterkeys():
for service in self.custom_services[node_id].itervalues():
for node_id in self.custom_services:
custom_services = self.custom_services[node_id]
for name in custom_services:
service = custom_services[name]
configs.append((node_id, service))
return configs

View file

@ -252,8 +252,7 @@ def hex_dump(s, bytes_per_word=2, words_per_line=8):
while s:
line = s[:total_bytes]
s = s[total_bytes:]
tmp = map(lambda x: ("%02x" * bytes_per_word) % x,
zip(*[iter(map(ord, line))] * bytes_per_word))
tmp = map(lambda x: ("%02x" * bytes_per_word) % x, zip(*[iter(map(ord, line))] * bytes_per_word))
if len(line) % 2:
tmp.append("%x" % ord(line[-1]))
dump += "0x%08x: %s\n" % (count, " ".join(tmp))

View file

@ -148,7 +148,8 @@ class ServiceElement(object):
def add_files(self):
# get custom files
file_elements = etree.Element("files")
for file_name, data in self.service.config_data.iteritems():
for file_name in self.service.config_data:
data = self.service.config_data[file_name]
file_element = etree.SubElement(file_elements, "file")
add_attribute(file_element, "name", file_name)
file_element.text = data
@ -291,7 +292,9 @@ class CoreXmlWriter(object):
if not options_config:
return
for _id, default_value in self.session.options.default_values().iteritems():
default_options = self.session.options.default_values()
for _id in default_options:
default_value = default_options[_id]
# TODO: should we just save the current config regardless, since it may change?
value = options_config[_id]
if value != default_value:
@ -307,7 +310,8 @@ class CoreXmlWriter(object):
if not config:
return
for _id, value in config.iteritems():
for _id in config:
value = config[_id]
add_configuration(metadata_elements, _id, value)
if metadata_elements.getchildren():
@ -320,7 +324,8 @@ class CoreXmlWriter(object):
if not all_configs:
continue
for model_name, config in all_configs.iteritems():
for model_name in all_configs:
config = all_configs[model_name]
logging.info("writing emane config node(%s) model(%s)", node_id, model_name)
if model_name == -1:
emane_configuration = create_emane_config(node_id, self.session.emane.emane_config, config)
@ -339,12 +344,14 @@ class CoreXmlWriter(object):
if not all_configs:
continue
for model_name, config in all_configs.iteritems():
for model_name in all_configs:
config = all_configs[model_name]
logging.info("writing mobility config node(%s) model(%s)", node_id, model_name)
mobility_configuration = etree.SubElement(mobility_configurations, "mobility_configuration")
add_attribute(mobility_configuration, "node", node_id)
add_attribute(mobility_configuration, "model", model_name)
for name, value in config.iteritems():
for name in config:
value = config[name]
add_configuration(mobility_configuration, name, value)
if mobility_configurations.getchildren():
@ -363,7 +370,8 @@ class CoreXmlWriter(object):
def write_default_services(self):
node_types = etree.Element("default_services")
for node_type, services in self.session.services.default_services.iteritems():
for node_type in self.session.services.default_services:
services = self.session.services.default_services[node_type]
node_type = etree.SubElement(node_types, "node", type=node_type)
for service in services:
etree.SubElement(node_type, "service", name=service)
@ -376,9 +384,12 @@ class CoreXmlWriter(object):
self.devices = etree.SubElement(self.scenario, "devices")
links = []
for node in self.session.nodes.itervalues():
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
# network node
if isinstance(node, (core.nodes.base.CoreNetworkBase, core.nodes.physical.Rj45Node)) and not nodeutils.is_node(node, NodeTypes.CONTROL_NET):
is_network_or_rj45 = isinstance(node, (core.nodes.base.CoreNetworkBase, core.nodes.physical.Rj45Node))
is_controlnet = nodeutils.is_node(node, NodeTypes.CONTROL_NET)
if is_network_or_rj45 and not is_controlnet:
self.write_network(node)
# device node
elif isinstance(node, core.nodes.base.CoreNodeBase):

View file

@ -100,7 +100,8 @@ class CoreXmlDeployment(object):
# servers = self.session.broker.getservernames()
# servers.remove("localhost")
for node in self.session.nodes.itervalues():
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
if isinstance(node, CoreNodeBase):
self.add_virtual_host(physical_host, node)