3 # Author: Martin Matusiak <numerodix@gmail.com>
4 # Licensed under the GNU Public License, version 3.
9 from sdict
import sdict
, sdictlist
19 self
.interfaces
= sdictlist(sort_key
="priority")
20 self
.interface_media
= ["wired", "wireless"]
21 self
.interface_fields
= ["interface", "priority"]
23 self
.networks
= sdictlist(sort_key
="priority")
24 self
.network_fields
= ["essid", "key", "priority", "vpn_exec"]
27 self
.live_networks
= sdictlist(sort_key
="priority")
30 self
.max_priortiy
= 255
34 self
.program_name
= "dumbwifi"
35 self
.configfile
= "/etc/%s.conf" % self
.program_name
36 self
.pidfile
= "/var/run/%s.pids" % self
.program_name
39 self
.logfile
= "/var/log/%s" % self
.program_name
40 self
.logfile_size
= 10*(1024**2)
46 # External executables
59 for cmd_name
in self
.tools
:
60 var_name
= cmd_name
.replace("-", "_")
61 value
= system
.os_which(cmd_name
)
62 self
.__dict
__[var_name
] = value
64 def init_config(self
):
65 # reset program_name to match the executable that is running
66 # without disturbing the already instantiated vars using program_name
67 self
.program_name
= os
.path
.basename(sys
.argv
[0])
69 lines
= system
.getfile(self
.configfile
)
71 raise Exception, "Failed to read configfile %s" % self
.configfile
72 lines
= regex
.replaceall("\s*;.*", "", lines
).strip() # strip comments
74 self
.interfaces
.clear()
77 for medium
in self
.interface_media
:
79 self
.read_config_block(lines
, medium
, self
.interface_fields
)
82 self
.interfaces
.extend(ifaces
)
84 nets
= self
.read_config_block(lines
, "wireless_network", self
.network_fields
)
85 self
.networks
.extend(nets
)
87 def read_config_block(self
, s
, block_name
, fields
):
88 blocks
= regex
.findall("\[%s\]\s+((?:.+=.+\s*)+)" % (block_name
), s
)
93 value
= regex
.find1("\s*%s\s*=(.*)" % field
, block
)
94 if value
: entry
[field
] = value
.strip()
95 # if len(entry) < fieldcount:
96 # msg = "In configuration file %s:\n"
97 # msg += "Failed to read config for '%s' block: "
98 # msg += "got %d field(s), expected %d"
99 # err_fg(msg % (configfile, block_name, len(entry), fieldcount))
101 entries
.append(entry
)