diff options
author | Matt Wilson <msw@redhat.com> | 2001-07-24 18:40:56 +0000 |
---|---|---|
committer | Matt Wilson <msw@redhat.com> | 2001-07-24 18:40:56 +0000 |
commit | 86f0d75d6bcdc465fcb3e7b7c7f923c21e48e9b7 (patch) | |
tree | 5ee905304096314b4c9ba2b249fcfd4e9fed748a | |
parent | 2aee54dba95de4050826f8c86ae4851b444727ef (diff) | |
download | anaconda-86f0d75d6bcdc465fcb3e7b7c7f923c21e48e9b7.tar.gz anaconda-86f0d75d6bcdc465fcb3e7b7c7f923c21e48e9b7.tar.xz anaconda-86f0d75d6bcdc465fcb3e7b7c7f923c21e48e9b7.zip |
code cleanup
-rw-r--r-- | iw/network_gui.py | 282 | ||||
-rw-r--r-- | network.py | 138 |
2 files changed, 218 insertions, 202 deletions
diff --git a/iw/network_gui.py b/iw/network_gui.py index d81722b2c..95e32102b 100644 --- a/iw/network_gui.py +++ b/iw/network_gui.py @@ -1,52 +1,65 @@ +# +# network_gui.py: Network configuration dialog +# +# Copyright 2001 Red Hat, Inc. +# +# This software may be freely redistributed under the terms of the GNU +# library public license. +# +# You should have received a copy of the GNU Library Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# + from gtk import * from iw_gui import * from isys import * from translate import _, N_ -class NetworkWindow (InstallWindow): +class NetworkWindow(InstallWindow): windowTitle = N_("Network Configuration") htmlTag = "netconf" - def __init__ (self, ics): - InstallWindow.__init__ (self, ics) + def __init__(self, ics): + InstallWindow.__init__(self, ics) self.calcNMHandler = None # XXX # - #for dev in self.network.available ().values (): + #for dev in self.network.available().values(): #if not dev.get('onboot'): - #dev.set (("onboot", "yes")) + #dev.set(("onboot", "yes")) - def getNext (self): + def getNext(self): if not self.__dict__.has_key("gw"): return None - self.network.gateway = self.gw.get_text () - self.network.primaryNS = self.ns.get_text () - self.network.secondaryNS = self.ns2.get_text () - self.network.ternaryNS = self.ns3.get_text () + self.network.gateway = self.gw.get_text() + self.network.primaryNS = self.ns.get_text() + self.network.secondaryNS = self.ns2.get_text() + self.network.ternaryNS = self.ns3.get_text() - if (self.hostname.get_text () != ""): - self.network.hostname = self.hostname.get_text () + if(self.hostname.get_text() != ""): + self.network.hostname = self.hostname.get_text() return None - def focusInIP (self, widget, event, (ip, nm)): + def focusInIP(self, widget, event, (ip, nm)): if nm.get_text() == "": - self.calcNetmask (None, (ip, nm)) + self.calcNetmask(None, (ip, nm)) - ip.calcNMHandler = ip.connect ("changed", self.calcNetmask, (ip, nm)) + ip.calcNMHandler = ip.connect("changed", self.calcNetmask, (ip, nm)) - def focusOutIP (self, widget, event, ip): - if (self.hostname.get_text () == "" + def focusOutIP(self, widget, event, ip): + if(self.hostname.get_text() == "" and self.network.hostname != "localhost.localdomain"): hs = self.network.hostname tmp = string.split(hs, ".") - self.hostname.set_text (tmp[0]) + self.hostname.set_text(tmp[0]) count = 0 domain = "" for token in tmp: @@ -58,213 +71,220 @@ class NetworkWindow (InstallWindow): domain = domain + "." + token count = count + 1 - self.hostname.set_text (self.network.hostname) + self.hostname.set_text(self.network.hostname) if ip.calcNMHandler != None: - ip.disconnect (ip.calcNMHandler) + ip.disconnect(ip.calcNMHandler) ip.calcNMHandler = None - def focusOutNM (self, widget, event, (dev, ip, nm, nw, bc)): + def focusOutNM(self, widget, event, (dev, ip, nm, nw, bc)): try: - network, broadcast = inet_calcNetBroad (ip.get_text (), nm.get_text ()) - if nw.get_text () == "": - nw.set_text (network) - dev.set (("network", network)) - if bc.get_text () == "": - bc.set_text (broadcast) - dev.set (("broadcast", broadcast)) + network, broadcast = inet_calcNetBroad(ip.get_text(), + nm.get_text()) + if nw.get_text() == "": + nw.set_text(network) + dev.set(("network", network)) + if bc.get_text() == "": + bc.set_text(broadcast) + dev.set(("broadcast", broadcast)) except: pass - def focusOutBC (self, widget, event, dev): - if self.gw.get_text () == "": + def focusOutBC(self, widget, event, dev): + if self.gw.get_text() == "": try: - gw = inet_calcGateway (widget.get_text ()) - self.gw.set_text (gw) + gw = inet_calcGateway(widget.get_text()) + self.gw.set_text(gw) except: pass - def focusOutNW (self, widget, event, dev): - if self.ns.get_text () == "": + def focusOutNW(self, widget, event, dev): + if self.ns.get_text() == "": try: - ns = inet_calcNS (widget.get_text ()) - self.ns.set_text (ns) + ns = inet_calcNS(widget.get_text()) + self.ns.set_text(ns) except: pass - def calcNWBC (self, widget, (dev, ip, nm, nw, bc)): - for addr in (ip, nm): + def calcNWBC(self, widget, (dev, ip, nm, nw, bc)): + for addr in(ip, nm): dots = 0 - for ch in addr.get_text (): + for ch in addr.get_text(): if ch == '.': dots = dots + 1 if dots != 3: return - dev.set (("ipaddr", ip.get_text ())) - dev.set (("netmask", nm.get_text ())) + dev.set(("ipaddr", ip.get_text())) + dev.set(("netmask", nm.get_text())) - def calcNetmask (self, widget, (ip, nm)): - ip = ip.get_text () + def calcNetmask(self, widget, (ip, nm)): + ip = ip.get_text() dots = 0 - valid_list = [ "1", "2", "3", "4", "5", "6", "7", "8" , "9", "0", "." ] + valid_list = ("1", "2", "3", "4", "5", "6", "7", "8" , "9", "0", ".") valid_ip = TRUE for x in ip: if x == '.': dots = dots + 1 - if x not in valid_list: #-if there's an invalid char in the widget, don't calculate netmask + #-if there's an invalid char in the widget, don't calculate netmask + if x not in valid_list: print "found invalid char" valid_ip = FALSE if dots != 3: return if valid_ip == TRUE: try: - new_nm = inet_calcNetmask (ip) - if (new_nm != nm.get_text ()): - nm.set_text (new_nm) + new_nm = inet_calcNetmask(ip) + if(new_nm != nm.get_text()): + nm.set_text(new_nm) except: pass - def DHCPtoggled (self, widget, (dev, table)): - active = widget.get_active () - table.set_sensitive (not active) - self.ipTable.set_sensitive (not active) + def DHCPtoggled(self, widget, (dev, table)): + active = widget.get_active() + table.set_sensitive(not active) + self.ipTable.set_sensitive(not active) bootproto = "dhcp" if not active: bootproto = "static" - dev.set (("bootproto", bootproto)) + dev.set(("bootproto", bootproto)) - def onBootToggled (self, widget, dev): - if widget.get_active (): + def onBootToggled(self, widget, dev): + if widget.get_active(): onboot = "yes" else: onboot = "no" - dev.set (("onboot", onboot)) + dev.set(("onboot", onboot)) # NetworkWindow tag="netconf" - def getScreen (self, network): - box = GtkVBox () - box.set_border_width (5) + def getScreen(self, network): + box = GtkVBox() + box.set_border_width(5) self.network = network - notebook = GtkNotebook () - devs = self.network.available () + notebook = GtkNotebook() + devs = self.network.available() if not devs: return None - devs.keys ().sort () + devs.keys().sort() num = 0 - for i in devs.keys (): - devbox = GtkVBox () - align = GtkAlignment () - DHCPcb = GtkCheckButton (_("Configure using DHCP")) - - align.add (DHCPcb) - devbox.pack_start (align, FALSE) - - align = GtkAlignment () - bootcb = GtkCheckButton (_("Activate on boot")) - onboot = devs[i].get ("onboot") - bootcb.connect ("toggled", self.onBootToggled, devs[i]) - bootcb.set_active ((num == 0 and not onboot) + for i in devs.keys(): + devbox = GtkVBox() + align = GtkAlignment() + DHCPcb = GtkCheckButton(_("Configure using DHCP")) + + align.add(DHCPcb) + devbox.pack_start(align, FALSE) + + align = GtkAlignment() + bootcb = GtkCheckButton(_("Activate on boot")) + onboot = devs[i].get("onboot") + bootcb.connect("toggled", self.onBootToggled, devs[i]) + bootcb.set_active((num == 0 and not onboot) or onboot == "yes") - align.add (bootcb) + align.add(bootcb) - devbox.pack_start (align, FALSE) + devbox.pack_start(align, FALSE) - devbox.pack_start (GtkHSeparator (), FALSE, padding=3) + devbox.pack_start(GtkHSeparator(), FALSE, padding=3) options = [(_("IP Address"), "ipaddr"), (_("Netmask"), "netmask"), (_("Network"), "network"), - (_("Broadcast"), "broadcast") ] - ipTable = GtkTable (len (options), 2) - self.ipTable = GtkTable (len (options), 2) # this is the iptable used for DNS, et. al + (_("Broadcast"), "broadcast")] + ipTable = GtkTable(len(options), 2) + # this is the iptable used for DNS, et. al + self.ipTable = GtkTable(len(options), 2) - DHCPcb.connect ("toggled", self.DHCPtoggled, (devs[i], ipTable)) - bootproto = devs[i].get ("bootproto") + DHCPcb.connect("toggled", self.DHCPtoggled, (devs[i], ipTable)) + bootproto = devs[i].get("bootproto") # go ahead and set up DHCP on the first device - DHCPcb.set_active ((num == 0 and not bootproto) - or bootproto == "dhcp") + DHCPcb.set_active((num == 0 and not bootproto) or + bootproto == "dhcp") num = num + 1 - forward = lambda widget, box=box: box.focus (DIR_TAB_FORWARD) + forward = lambda widget, box=box: box.focus(DIR_TAB_FORWARD) - for t in range (len (options)): - label = GtkLabel ("%s:" % (options[t][0],)) - label.set_alignment (0.0, 0.5) - ipTable.attach (label, 0, 1, t, t+1, FILL, 0, 10) - entry = GtkEntry (15) - # entry.set_usize (gdk_char_width (entry.get_style ().font, '0')*15, -1) - entry.set_usize (7 * 15, -1) - entry.connect ("activate", forward) + for t in range(len(options)): + label = GtkLabel("%s:" %(options[t][0],)) + label.set_alignment(0.0, 0.5) + ipTable.attach(label, 0, 1, t, t+1, FILL, 0, 10) + entry = GtkEntry(15) + # entry.set_usize(gdk_char_width(entry.get_style().font, '0')*15, -1) + entry.set_usize(7 * 15, -1) + entry.connect("activate", forward) - entry.set_text (devs[i].get (options[t][1])) + entry.set_text(devs[i].get(options[t][1])) options[t] = entry - ipTable.attach (entry, 1, 2, t, t+1, 0, FILL|EXPAND) + ipTable.attach(entry, 1, 2, t, t+1, 0, FILL|EXPAND) - for t in range (len (options)): + for t in range(len(options)): if t == 0 or t == 1: - options[t].connect ("changed", self.calcNWBC, (devs[i],) + tuple (options)) + options[t].connect("changed", self.calcNWBC, + (devs[i],) + tuple(options)) options[0].ipCalcNMHandler = None - self.focusOutNM (None, None, (devs[i],) + tuple (options)) + self.focusOutNM(None, None, (devs[i],) + tuple(options)) # add event handlers for the main IP widget to calcuate the netmask - options[0].connect ("focus_in_event", self.focusInIP, (options[0], options[1])) - options[0].connect ("focus_out_event", self.focusOutIP, options[0]) - options[1].connect ("focus_out_event", self.focusOutNM, (devs[i],) + tuple (options)) - options[2].connect ("focus_out_event", self.focusOutNW, devs[i]) - options[3].connect ("focus_out_event", self.focusOutBC, devs[i]) + options[0].connect("focus_in_event", self.focusInIP, + (options[0], options[1])) + options[0].connect("focus_out_event", self.focusOutIP, options[0]) + options[1].connect("focus_out_event", self.focusOutNM, + (devs[i],) + tuple(options)) + options[2].connect("focus_out_event", self.focusOutNW, devs[i]) + options[3].connect("focus_out_event", self.focusOutBC, devs[i]) - devbox.pack_start (ipTable, FALSE, FALSE, 5) + devbox.pack_start(ipTable, FALSE, FALSE, 5) - devbox.show_all () - notebook.append_page (devbox, GtkLabel (i)) + devbox.show_all() + notebook.append_page(devbox, GtkLabel(i)) - box.pack_start (notebook, FALSE) - box.pack_start (GtkHSeparator (), FALSE, padding=10) + box.pack_start(notebook, FALSE) + box.pack_start(GtkHSeparator(), FALSE, padding=10) - options = [_("Hostname"), - _("Gateway"), _("Primary DNS"), _("Secondary DNS"), _("Ternary DNS")] + options = [_("Hostname"), _("Gateway"), _("Primary DNS"), + _("Secondary DNS"), _("Ternary DNS")] - for i in range (len (options)): - label = GtkLabel ("%s:" % (options[i],)) - label.set_alignment (0.0, 0.0) - self.ipTable.attach (label, 0, 1, i, i+1, FILL, 0, 10) + for i in range(len(options)): + label = GtkLabel("%s:" %(options[i],)) + label.set_alignment(0.0, 0.0) + self.ipTable.attach(label, 0, 1, i, i+1, FILL, 0, 10) if i == 0: - options[i] = GtkEntry () - options[i].set_usize (7 * 30, -1) + options[i] = GtkEntry() + options[i].set_usize(7 * 30, -1) else: - options[i] = GtkEntry (15) - options[i].set_usize (7 * 15, -1) - options[i].connect ("activate", forward) - align = GtkAlignment (0, 0.5) - align.add (options[i]) - self.ipTable.attach (align, 1, 2, i, i+1, FILL, 0) - self.ipTable.set_row_spacing (0, 5) + options[i] = GtkEntry(15) + options[i].set_usize(7 * 15, -1) + options[i].connect("activate", forward) + align = GtkAlignment(0, 0.5) + align.add(options[i]) + self.ipTable.attach(align, 1, 2, i, i+1, FILL, 0) + self.ipTable.set_row_spacing(0, 5) self.hostname = options[0] # bring over the value from the loader - if (self.network.hostname != "localhost.localdomain"): - self.hostname.set_text (self.network.hostname) + if(self.network.hostname != "localhost.localdomain"): + self.hostname.set_text(self.network.hostname) self.gw = options[1] - self.gw.set_text (self.network.gateway) + self.gw.set_text(self.network.gateway) self.ns = options[2] - self.ns.set_text (self.network.primaryNS) + self.ns.set_text(self.network.primaryNS) self.ns2 = options[3] - self.ns2.set_text (self.network.secondaryNS) + self.ns2.set_text(self.network.secondaryNS) self.ns3 = options[4] - self.ns3.set_text (self.network.ternaryNS) - box.pack_start (self.ipTable, FALSE, FALSE, 5) + self.ns3.set_text(self.network.ternaryNS) + box.pack_start(self.ipTable, FALSE, FALSE, 5) + return box diff --git a/network.py b/network.py index 42d455a07..bc14a4c5c 100644 --- a/network.py +++ b/network.py @@ -22,13 +22,13 @@ import socket import os from log import log -class NetworkDevice (SimpleConfigFile): - def __str__ (self): +class NetworkDevice(SimpleConfigFile): + def __str__(self): s = "" s = s + "DEVICE=" + self.info["DEVICE"] + "\n" - keys = self.info.keys () - keys.sort () - keys.remove ("DEVICE") + keys = self.info.keys() + keys.sort() + keys.remove("DEVICE") # Don't let onboot be turned on unless we have config information # to go along with it @@ -52,11 +52,11 @@ class NetworkDevice (SimpleConfigFile): return s - def __init__ (self, dev): + def __init__(self, dev): self.info = { "DEVICE" : dev } class Network: - def __init__ (self): + def __init__(self): self.netdevices = {} self.gateway = "" self.primaryNS = "" @@ -68,43 +68,38 @@ class Network: self.hostname = "localhost.localdomain" try: - f = open ("/tmp/netinfo", "r") + f = open("/tmp/netinfo", "r") except: pass else: - lines = f.readlines () - f.close () + lines = f.readlines() + f.close() info = {} self.isConfigured = 1 for line in lines: - netinf = string.splitfields (line, '=') - info [netinf[0]] = string.strip (netinf[1]) - self.netdevices [info["DEVICE"]] = NetworkDevice (info["DEVICE"]) - if info.has_key ("IPADDR"): - self.netdevices [info["DEVICE"]].set (("IPADDR", info["IPADDR"])) - if info.has_key ("NETMASK"): - self.netdevices [info["DEVICE"]].set (("NETMASK", info["NETMASK"])) - if info.has_key ("BOOTPROTO"): - self.netdevices [info["DEVICE"]].set (("BOOTPROTO", info["BOOTPROTO"])) - if info.has_key ("ONBOOT"): - self.netdevices [info["DEVICE"]].set (("ONBOOT", info["ONBOOT"])) - if info.has_key ("GATEWAY"): + netinf = string.splitfields(line, '=') + info [netinf[0]] = string.strip(netinf[1]) + self.netdevices [info["DEVICE"]] = NetworkDevice(info["DEVICE"]) + for key in ("IPADDR", "NETMASK", "BOOTPROTO", "ONBOOT"): + if info.has_key(key): + self.netdevices [info["DEVICE"]].set((key, info[key])) + if info.has_key("GATEWAY"): self.gateway = info["GATEWAY"] - if info.has_key ("DOMAIN"): + if info.has_key("DOMAIN"): self.domains.append(info["DOMAIN"]) - if info.has_key ("HOSTNAME"): + if info.has_key("HOSTNAME"): self.hostname = info["HOSTNAME"] self.readData = 1 try: - f = open ("/etc/resolv.conf", "r") + f = open("/etc/resolv.conf", "r") except: pass else: - lines = f.readlines () - f.close () + lines = f.readlines() + f.close() for line in lines: - resolv = string.split (line) + resolv = string.split(line) if resolv and resolv[0] == 'nameserver': if self.primaryNS == "": self.primaryNS = resolv[1] @@ -116,25 +111,27 @@ class Network: def getDevice(self, device): return self.netdevices[device] - def available (self): - f = open ("/proc/net/dev") + def available(self): + f = open("/proc/net/dev") lines = f.readlines() - f.close () + f.close() # skip first two lines, they are header lines = lines[2:] for line in lines: - dev = string.strip (line[0:6]) - if dev != "lo" and not self.netdevices.has_key (dev): - self.netdevices[dev] = NetworkDevice (dev) + dev = string.strip(line[0:6]) + if dev != "lo" and not self.netdevices.has_key(dev): + self.netdevices[dev] = NetworkDevice(dev) return self.netdevices def setHostname(self, hn): self.hostname = hn - def lookupHostname (self): + def lookupHostname(self): # can't look things up if they don't exist! - if not self.hostname or self.hostname == "localhost.localdomain": return None - if not self.primaryNS: return + if not self.hostname or self.hostname == "localhost.localdomain": + return None + if not self.primaryNS: + return if not self.isConfigured: for dev in self.netdevices.values(): if dev.get('bootproto') == "dhcp": @@ -150,18 +147,18 @@ class Network: self.isConfigured = 1 break except SystemError: - log ("failed to configure network device %s when " + log("failed to configure network device %s when " "looking up host name", dev.get('device')) if not self.isConfigured: - log ("no network devices were availabe to look up host name") + log("no network devices were availabe to look up host name") return None f = open("/etc/resolv.conf", "w") f.write("nameserver %s\n" % self.primaryNS) f.close() isys.resetResolv() - isys.setResolvRetry(2) + isys.setResolvRetry(1) try: ip = socket.gethostbyname(self.hostname) @@ -170,8 +167,8 @@ class Network: return ip - def nameservers (self): - return [ self.primaryNS, self.secondaryNS, self.ternaryNS ] + def nameservers(self): + return (self.primaryNS, self.secondaryNS, self.ternaryNS) def writeKS(self, f): # XXX @@ -195,7 +192,7 @@ class Network: f.write(" --bootproto dhcp") else: f.write(" --bootproto static --ip %s --netmask %s --gateway %s" % - (dev.get('ipaddr'), dev.get('netmask'), self.gateway)) + (dev.get('ipaddr'), dev.get('netmask'), self.gateway)) if dev.get('nameserver'): f.write(" --nameserver %s" % dev.get('nameserver')) @@ -205,37 +202,37 @@ class Network: f.write("\n"); - def write (self, instPath): + def write(self, instPath): # /etc/sysconfig/network-scripts/ifcfg-* - for dev in self.netdevices.values (): - device = dev.get ("device") - fn = instPath + "/etc/sysconfig/network-scripts/ifcfg-" + device - f = open (fn, "w") + for dev in self.netdevices.values(): + device = dev.get("device") + fn = "%s/etc/sysconfig/network-scripts/ifcfg-%s" % (instPath, + device) + f = open(fn, "w") os.chmod(fn, 0600) - f.write (str (dev)) - f.close () + f.write(str(dev)) + f.close() # /etc/sysconfig/network - f = open (instPath + "/etc/sysconfig/network", "w") - f.write ("NETWORKING=yes\n" - "HOSTNAME=") - + f = open(instPath + "/etc/sysconfig/network", "w") + f.write("NETWORKING=yes\n" + "HOSTNAME=") - # use instclass hostname if set (kickstart) to override + # use instclass hostname if set(kickstart) to override if self.hostname: f.write(self.hostname + "\n") else: - f.write("localhost.localdomain" + "\n") + f.write("localhost.localdomain\n") if self.gateway: - f.write("GATEWAY=" + self.gateway + "\n") - f.close () + f.write("GATEWAY=%s\n", (self.gateway,)) + f.close() # /etc/hosts - f = open (instPath + "/etc/hosts", "w") + f = open(instPath + "/etc/hosts", "w") localline = "127.0.0.1\t\t" - log ("self.hostname = %s", self.hostname) + log("self.hostname = %s", self.hostname) ip = self.lookupHostname() @@ -249,29 +246,28 @@ class Network: localline = localline + "localhost.localdomain localhost\n" f.write("# Do not remove the following line, or various programs\n") f.write("# that require network functionality will fail.\n") - f.write (localline) + f.write(localline) if ip: - f.write ("%s\t\t%s\n" % (ip, self.hostname)) + f.write("%s\t\t%s\n" % (ip, self.hostname)) # If the hostname was not looked up, but typed in by the user, # domain might not be computed, so do it now. - if self.domains == [ "localdomain" ] or not self.domains: + if self.domains == ["localdomain"] or not self.domains: if '.' in self.hostname: # chop off everything before the leading '.' domain = self.hostname[(string.find(self.hostname, '.') + 1):] - self.domains = [ domain ] + self.domains = [domain] # /etc/resolv.conf - f = open (instPath + "/etc/resolv.conf", "w") + f = open(instPath + "/etc/resolv.conf", "w") - if self.domains != [ 'localdomain' ] and self.domains: - f.write ("search " + string.joinfields (self.domains, ' ') - + "\n") + if self.domains != ['localdomain'] and self.domains: + f.write("search %s\n", (string.joinfields(self.domains, ' '),)) - for ns in self.nameservers (): + for ns in self.nameservers(): if ns: - f.write ("nameserver " + ns + "\n") + f.write("nameserver %s\n", (ns,)) - f.close () + f.close() |