From aa91bb67a1566f8172f77f8de5e8e72664755332 Mon Sep 17 00:00:00 2001 From: bharnden Date: Thu, 11 Oct 2018 13:28:02 -0700 Subject: [PATCH] cleanup of files in root core module --- .gitignore | 3 ++ daemon/core/corehandlers.py | 8 ++--- daemon/core/coreobj.py | 60 ++++++++++++++++--------------------- daemon/core/location.py | 16 +++++----- daemon/core/sdt.py | 32 ++++++++++---------- daemon/core/service.py | 4 +-- daemon/core/session.py | 17 +++++------ 7 files changed, 67 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index 9fb9d6ae..7415be63 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,9 @@ stamp-h1 # python build directory dist +# vscode +.vscode + # intellij *.iml .idea diff --git a/daemon/core/corehandlers.py b/daemon/core/corehandlers.py index e85369a5..24847aa3 100644 --- a/daemon/core/corehandlers.py +++ b/daemon/core/corehandlers.py @@ -1005,17 +1005,17 @@ class CoreHandler(SocketServer.BaseRequestHandler): if not config_data.data_values: logger.warn("location data missing") else: - values = config_data.data_values.split("|") + values = [float(x) for x in config_data.data_values.split("|")] # Cartesian coordinate reference point - refx, refy = map(lambda x: float(x), values[0:2]) + refx, refy = values[0], values[1] refz = 0.0 - lat, lon, alt = map(lambda x: float(x), values[2:5]) + lat, lon, alt = values[2], values[3], values[4] # xyz point self.session.location.refxyz = (refx, refy, refz) # geographic reference point self.session.location.setrefgeo(lat, lon, alt) - self.session.location.refscale = float(values[5]) + self.session.location.refscale = values[5] logger.info("location configured: %s = %s scale=%s", self.session.location.refxyz, self.session.location.refgeo, self.session.location.refscale) logger.info("location configured: UTM%s", self.session.location.refutm) diff --git a/daemon/core/coreobj.py b/daemon/core/coreobj.py index 9751a0e4..2b19ba00 100644 --- a/daemon/core/coreobj.py +++ b/daemon/core/coreobj.py @@ -85,6 +85,9 @@ class PyCoreObj(object): if name is None: name = "o%s" % self.objid self.name = name + self.type = None + self.server = None + self.services = None # ifindex is key, PyCoreNetIf instance is value self._netif = {} self.ifindex = 0 @@ -205,21 +208,12 @@ class PyCoreObj(object): return None x, y, _ = self.getposition() + model = self.type + emulation_server = self.server - model = None - if hasattr(self, "type"): - model = self.type - - emulation_server = None - if hasattr(self, "server"): - emulation_server = self.server - - services = None - if hasattr(self, "services") and len(self.services) != 0: - nodeservices = [] - for s in self.services: - nodeservices.append(s.name) - services = "|".join(nodeservices) + services = self.services + if services is not None: + services = "|".join([x.name for x in services]) node_data = NodeData( message_type=message_type, @@ -269,10 +263,8 @@ class PyCoreNode(PyCoreObj): :param str name: object name :param bool start: boolean for starting """ - PyCoreObj.__init__(self, session, objid, name, start=start) + super(PyCoreNode, self).__init__(session, objid, name, start=start) self.services = [] - if not hasattr(self, "type"): - self.type = None self.nodedir = None self.tmpnodedir = False @@ -460,6 +452,19 @@ class PyCoreNet(PyCoreObj): """ linktype = LinkTypes.WIRED.value + def __init__(self, session, objid, name, start=True): + """ + Create a PyCoreNet instance. + + :param core.session.Session session: CORE session object + :param int objid: object id + :param str name: object name + :param bool start: should object start + """ + super(PyCoreNet, self).__init__(session, objid, name, start=start) + self._linked = {} + self._linked_lock = threading.Lock() + def startup(self): """ Each object implements its own startup method. @@ -476,19 +481,6 @@ class PyCoreNet(PyCoreObj): """ raise NotImplementedError - def __init__(self, session, objid, name, start=True): - """ - Create a PyCoreNet instance. - - :param core.session.Session session: CORE session object - :param int objid: object id - :param str name: object name - :param bool start: should object start - """ - PyCoreObj.__init__(self, session, objid, name, start=start) - self._linked = {} - self._linked_lock = threading.Lock() - def attach(self, netif): """ Attach network interface. @@ -550,7 +542,7 @@ class PyCoreNet(PyCoreObj): interface2_ip6 = None interface2_ip6_mask = None for address in netif.addrlist: - ip, sep, mask = address.partition('/') + ip, _sep, mask = address.partition("/") mask = int(mask) if ipaddress.is_ipv4_address(ip): family = AF_INET @@ -627,7 +619,8 @@ class PyCoreNetIf(object): self._params = {} self.addrlist = [] self.hwaddr = None - self.poshook = None + # placeholder position hook + self.poshook = lambda a, b, c, d: None # used with EMANE self.transport_type = None # interface index on the network @@ -760,5 +753,4 @@ class PyCoreNetIf(object): :param z: z position :return: nothing """ - if self.poshook is not None: - self.poshook(self, x, y, z) + self.poshook(self, x, y, z) diff --git a/daemon/core/location.py b/daemon/core/location.py index 6525d10e..6a64ad41 100644 --- a/daemon/core/location.py +++ b/daemon/core/location.py @@ -139,7 +139,7 @@ class CoreLocation(object): """ # convert lat/lon to UTM coordinates in meters e, n, zonen, zonel = utm.from_latlon(lat, lon) - rlat, rlon, ralt = self.refgeo + _rlat, _rlon, ralt = self.refgeo xshift = self.geteastingshift(zonen, zonel) if xshift is None: xm = e - self.refutm[1] @@ -180,11 +180,11 @@ class CoreLocation(object): if z in self.zoneshifts and self.zoneshifts[z][0] is not None: return self.zoneshifts[z][0] - rlat, rlon, ralt = self.refgeo + rlat, rlon, _ralt = self.refgeo # ea. zone is 6deg band lon2 = rlon + 6 * (zonen - rzonen) # ignore northing - e2, n2, zonen2, zonel2 = utm.from_latlon(rlat, lon2) + e2, _n2, _zonen2, _zonel2 = utm.from_latlon(rlat, lon2) # NOTE: great circle distance used here, not reference ellipsoid! xshift = utm.haversine(rlon, rlat, lon2, rlat) - e2 # cache the return value @@ -216,12 +216,12 @@ class CoreLocation(object): if z in self.zoneshifts and self.zoneshifts[z][1] is not None: return self.zoneshifts[z][1] - rlat, rlon, ralt = self.refgeo + rlat, rlon, _ralt = self.refgeo # zonemap is used to calculate degrees difference between zone letters latshift = self.zonemap[zonel] - self.zonemap[rzonel] # ea. latitude band is 8deg high lat2 = rlat + latshift - e2, n2, zonen2, zonel2 = utm.from_latlon(lat2, rlon) + _e2, n2, _zonen2, _zonel2 = utm.from_latlon(lat2, rlon) # NOTE: great circle distance used here, not reference ellipsoid yshift = -(utm.haversine(rlon, rlat, rlon, lat2) + n2) # cache the return value @@ -244,12 +244,12 @@ class CoreLocation(object): :rtype: tuple """ zone = self.refutm[0] - rlat, rlon, ralt = self.refgeo + rlat, rlon, _ralt = self.refgeo if e > 834000 or e < 166000: num_zones = (int(e) - 166000) / (utm.R / 10) # estimate number of zones to shift, E (positive) or W (negative) rlon2 = self.refgeo[1] + (num_zones * 6) - e2, n2, zonen2, zonel2 = utm.from_latlon(rlat, rlon2) + _e2, _n2, zonen2, zonel2 = utm.from_latlon(rlat, rlon2) xshift = utm.haversine(rlon, rlat, rlon2, rlat) # after >3 zones away from refpt, the above estimate won't work # (the above estimate could be improved) @@ -257,7 +257,7 @@ class CoreLocation(object): # move one more zone away num_zones = (abs(num_zones) + 1) * (abs(num_zones) / num_zones) rlon2 = self.refgeo[1] + (num_zones * 6) - e2, n2, zonen2, zonel2 = utm.from_latlon(rlat, rlon2) + _e2, _n2, zonen2, zonel2 = utm.from_latlon(rlat, rlon2) xshift = utm.haversine(rlon, rlat, rlon2, rlat) e = e - xshift zone = (zonen2, zonel2) diff --git a/daemon/core/sdt.py b/daemon/core/sdt.py index 5f5da01e..73bbc997 100644 --- a/daemon/core/sdt.py +++ b/daemon/core/sdt.py @@ -183,10 +183,10 @@ class Sdt(object): if not self.cmd("path \"%s/icons/normal\"" % constants.CORE_DATA_DIR): return False # send node type to icon mappings - for type, icon in self.DEFAULT_SPRITES: - if not self.cmd("sprite %s image %s" % (type, icon)): + for node_type, icon in self.DEFAULT_SPRITES: + if not self.cmd("sprite %s image %s" % (node_type, icon)): return False - (lat, long) = self.session.location.refgeo[:2] + lat, long = self.session.location.refgeo[:2] return self.cmd("flyto %.6f,%.6f,%d" % (long, lat, self.DEFAULT_ALT)) def disconnect(self): @@ -238,7 +238,7 @@ class Sdt(object): self.connected = False return False - def updatenode(self, nodenum, flags, x, y, z, name=None, type=None, icon=None): + def updatenode(self, nodenum, flags, x, y, z, name=None, node_type=None, icon=None): """ Node is updated from a Node Message or mobility script. @@ -248,7 +248,7 @@ class Sdt(object): :param y: y position :param z: z position :param str name: node name - :param type: node type + :param node_type: node type :param icon: node icon :return: nothing """ @@ -263,11 +263,11 @@ class Sdt(object): pos = "pos %.6f,%.6f,%.6f" % (lon, lat, alt) if flags & MessageFlags.ADD.value: if icon is not None: - type = name + node_type = name icon = icon.replace("$CORE_DATA_DIR", constants.CORE_DATA_DIR) icon = icon.replace("$CORE_CONF_DIR", constants.CORE_CONF_DIR) self.cmd("sprite %s image %s" % (type, icon)) - self.cmd("node %d type %s label on,\"%s\" %s" % (nodenum, type, name, pos)) + self.cmd("node %d type %s label on,\"%s\" %s" % (nodenum, node_type, name, pos)) else: self.cmd("node %d %s" % (nodenum, pos)) @@ -403,12 +403,12 @@ class Sdt(object): nodetype == NodeTypes.PHYSICAL.value: if model is None: model = "router" - type = model + nodetype = model elif nodetype is not None: - type = nodeutils.get_node_class(NodeTypes(nodetype)).type + nodetype = nodeutils.get_node_class(NodeTypes(nodetype)).type net = True else: - type = None + nodetype = None try: node = self.session.get_object(nodenum) @@ -421,15 +421,15 @@ class Sdt(object): remote = self.remotes[nodenum] if name is None: name = remote.name - if type is None: - type = remote.type + if nodetype is None: + nodetype = remote.type if icon is None: icon = remote.icon else: - remote = Bunch(objid=nodenum, type=type, icon=icon, name=name, net=net, links=set()) + remote = Bunch(objid=nodenum, type=nodetype, icon=icon, name=name, net=net, links=set()) self.remotes[nodenum] = remote remote.pos = (x, y, z) - self.updatenode(nodenum, msg.flags, x, y, z, name, type, icon) + self.updatenode(nodenum, msg.flags, x, y, z, name, nodetype, icon) def handlelinkmsg(self, msg): """ @@ -467,8 +467,8 @@ class Sdt(object): :rtype: bool """ if nodenum in self.remotes: - type = self.remotes[nodenum].type - if type in ("wlan", "emane"): + node_type = self.remotes[nodenum].type + if node_type in ("wlan", "emane"): return True else: try: diff --git a/daemon/core/service.py b/daemon/core/service.py index 3be36b4d..fa4f3ba4 100644 --- a/daemon/core/service.py +++ b/daemon/core/service.py @@ -138,7 +138,7 @@ class ServiceShim(object): if not service.custom: valmap[1] = service.get_configs(node) valmap[3] = service.get_startup(node) - vals = map(lambda a, b: "%s=%s" % (a, str(b)), cls.keys, valmap) + vals = ["%s=%s" % (x, y) for x, y in zip(cls.keys, valmap)] return "|".join(vals) @classmethod @@ -469,7 +469,7 @@ class CoreServices(object): try: node.privatedir(directory) except (CoreCommandError, ValueError) as e: - logger.warn("error mounting private dir '%s' for service '%s': %s", + logger.warn("error mounting private dir '%s' for service '%s': %s", directory, service.name, e) # create service files diff --git a/daemon/core/session.py b/daemon/core/session.py index 49fcd0b0..3f52997c 100644 --- a/daemon/core/session.py +++ b/daemon/core/session.py @@ -252,8 +252,9 @@ class Session(object): hooks = self._hooks.get(state, []) # execute all state hooks - for hook in hooks: - self.run_hook(hook) + if hooks: + for hook in hooks: + self.run_hook(hook) else: logger.info("no state hooks for %s", state) @@ -269,7 +270,7 @@ class Session(object): """ logger.info("setting state hook: %s - %s from %s", hook_type, file_name, source_name) - hook_id, state = hook_type.split(':')[:2] + _hook_id, state = hook_type.split(':')[:2] if not state.isdigit(): logger.error("error setting hook having state '%s'", state) return @@ -352,7 +353,8 @@ class Session(object): :return: nothing """ hooks = self._state_hooks.setdefault(state, []) - assert hook not in hooks + if hook in hooks: + raise ValueError("attempting to add duplicate state hook") hooks.append(hook) if self.state == state: @@ -629,13 +631,10 @@ class Session(object): """ with self._objects_lock: - count = len(filter(lambda x: not nodeutils.is_node(x, (NodeTypes.PEER_TO_PEER, NodeTypes.CONTROL_NET)), - self.objects)) + count = len([x for x in self.objects if not nodeutils.is_node(x, (NodeTypes.PEER_TO_PEER, NodeTypes.CONTROL_NET))]) # on Linux, GreTapBridges are auto-created, not part of GUI's node count - count -= len(filter( - lambda (x): nodeutils.is_node(x, NodeTypes.TAP_BRIDGE) and not nodeutils.is_node(x, NodeTypes.TUNNEL), - self.objects)) + count -= len([x for x in self.objects if nodeutils.is_node(x, NodeTypes.TAP_BRIDGE) and not nodeutils.is_node(x, NodeTypes.TUNNEL)]) return count