update
[dumbwifi.git] / opts.py
blob2915c32c6d487a50790a6e86636b58b2ec91e95c
1 #!/usr/bin/env python
3 # Author: Martin Matusiak <numerodix@gmail.com>
4 # Licensed under the GNU Public License, version 3.
6 import optparse
7 import sys
9 from conf import config
10 from output import logger
11 import regex
12 from sdict import sdict, sdictlist
15 # Read command line options
17 def create_parser():
18 return optparse.OptionParser(add_help_option=None)
20 def read_opts(parser):
21 def opts_help(option, opt_str, value, parser):
22 print "Usage: %s [options]" % config.program_name
23 for o in parser.option_list:
24 var = o.metavar
25 if not o.metavar: var = ""
26 argument = "%s %s %s" % (o._short_opts[0], o._long_opts[0], var)
27 print " %s %s" % (argument.strip().ljust(25), o.help)
28 sys.exit(0)
30 parser.add_option("-h", "--help", action="callback", callback=opts_help,
31 help="Display this message")
32 opts = parser.parse_args()[0]
33 config.opts.update( [(k, opts.__dict__[k]) for k in opts.__dict__] )
35 # merge new nets info global list
36 if config.opts.network:
37 for (i, n) in enumerate(config.opts.network):
38 pair = n.split(",")
39 name = pair[0] ; pri = config.max_priortiy - i
40 d = sdict({ 'essid': name, 'priority': pri })
41 if len(pair) == 2: d['key'] = pair[1]
42 config.networks = config.networks.merge1(d, 'essid')
43 config.networks = negated_opts(config.networks, 'essid')
45 # merge new interfaces into global list
46 if config.opts.interface:
47 for (i, n) in enumerate(config.opts.interface):
48 pri = config.max_priortiy - i
49 d = sdict({'interface': n, 'priority': pri})
50 config.interfaces = config.interfaces.merge1(d, 'interface')
51 config.interfaces = negated_opts(config.interfaces, 'interface')
53 def negated_opts(dicts, key):
54 new_dicts = dicts
55 for (i, d) in enumerate(dicts):
56 val = regex.find1("^\^(.*)", d[key])
57 if val:
58 new_dicts.remove(i)
59 new_dicts = sdictlist(init_list=new_dicts.get_all(\
60 pred=lambda x: x[key] != val))
61 return new_dicts
63 def post_negate_nets(nets, key):
64 if not config.opts.network: return nets
65 new_nets = nets
66 for (i, n) in enumerate(config.opts.network):
67 key_value = regex.find1("^\^(.*)[,]?.*", n)
68 if key_value:
69 new_nets = sdictlist(init_list=\
70 new_nets.get_all(pred=lambda x: x[key] != key_value))
71 return new_nets