add additional argument to the configElement constructor to make configElements,...
[enigma2.git] / lib / python / Components / Network.py
1 from config import *
2
3 import os
4
5 class Network:
6         def __init__(self):
7                 pass
8                 
9         def writeNetworkConfig(self):
10                 # fixme restarting and updating the network too often. possible fix: check current config and execute only if changed :/
11                 # fixme using interfaces.tmp instead of interfaces for now
12                 fp = file('/etc/network/interfaces.tmp', 'w')
13                 fp.write("auto eth0\n")
14                 if (config.network.dhcp.value == "yes"):
15                         fp.write("iface eth0 inet dhcp\n")
16                 else:
17                         fp.write("iface eth0 inet static\n")
18                         fp.write("      address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
19                         fp.write("      netmask %d.%d.%d.%d\n" % tuple(config.network.netmask.value))
20                         fp.write("      gateway %d.%d.%d.%d\n" % tuple(config.network.gateway.value))
21                 fp.close()
22
23         def activateNetworkConfig(self):
24                 import os
25                 #os.system("/etc/init.d/networking restart")
26                 
27         def setDHCP(self, useDHCP):
28                 if (useDHCP):
29                         print "Using DHCP"
30                         config.network.ip.enabled = False
31                         config.network.netmask.enabled = False
32                         config.network.gateway.enabled = False
33                         config.network.dns.enabled = False
34                 else:
35                         print "NOT using DHCP"
36                         config.network.ip.enabled = True
37                         config.network.netmask.enabled = True
38                         config.network.gateway.enabled = True
39                         config.network.dns.enabled = True
40                                         
41         def setIPNameserver(self, ip):
42                 return
43                 resolvconf = file('/etc/resolv.conf', 'w')
44                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
45                 resolvconf.close()
46                 
47         def setMACAddress(self, mac):
48                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
49                 pass
50                 
51         def setIPAddress(self, ip):
52                 os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
53                 self.writeNetworkConfig()
54
55         def setGateway(self, ip):
56                 os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
57                 self.writeNetworkConfig()
58                 
59         def setNetmask(self, ip):
60                 os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))         
61                 self.writeNetworkConfig()               
62
63 def InitNetwork():
64         config.network = ConfigSubsection()
65         config.network.dhcp = configElement("config.network.dhcp", configSelection, 0, ("no", "yes"))
66         config.network.ip = configElement("config.network.ip", configSequence, [192,168,1,45], (("."), (1,255)))
67         config.network.netmask = configElement("config.network.netmask", configSequence, [255,255,255,0], (("."), (1,255)))
68         config.network.gateway = configElement("config.network.gateway", configSequence, [192,168,1,3], (("."), (1,255)))
69         config.network.dns = configElement("config.network.dns", configSequence, [192,168,1,3], (("."), (1,255)))
70         config.network.mac = configElement("config.network.mac", configSequence, [00,11,22,33,44,55], ((":"), (1,255)))
71         
72         #FIXME using this till other concept for this is implemented
73         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
74         config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
75
76         iNetwork = Network()
77
78         def writeNetworkConfig(configElement):
79                 iNetwork.writeNetworkConfig()
80                 
81         def setIPAddress(configElement):
82                 iNetwork.setIPAddress(configElement.value)
83
84         def setGateway(configElement):
85                 iNetwork.setGateway(configElement.value)
86
87         def setNetmask(configElement):
88                 iNetwork.setNetmask(configElement.value)                
89
90         def setDHCP(configElement):
91                 iNetwork.setDHCP(configElement.value)
92
93         def setIPNameserver(configElement):
94                 iNetwork.setIPNameserver(configElement.value)
95
96         def setMACAddress(configElement):
97                 iNetwork.setMACAddress(configElement.value)
98
99         def activateNetworkConfig(configElement):
100                 iNetwork.activateNetworkConfig()
101
102
103         # this will call the "setup-val" initial
104         config.network.dhcp.addNotifier(setDHCP)
105         config.network.ip.addNotifier(setIPAddress)
106         config.network.netmask.addNotifier(setNetmask)  
107         config.network.gateway.addNotifier(setGateway)
108         config.network.dns.addNotifier(setIPNameserver)
109         config.network.mac.addNotifier(setMACAddress)
110         config.network.activate.addNotifier(activateNetworkConfig)