Use Python's auto-concatenation of strings within parenthesis
[wifi-radar.git] / wifi-radar
blobfab521a6390b4362a3413849c1bc14faa6d50c4f
1 #!/usr/bin/python -OO
2 # -*- coding: utf-8 -*-
4 # A utility for managing WiFi profiles on GNU/Linux.
6 # Copyright (C) 2004-2005 Ahmad Baitalmal <ahmad@baitalmal.com>
7 # Copyright (C) 2005 Nicolas Brouard <nicolas.brouard@mandrake.org>
8 # Copyright (C) 2005-2009 Brian Elliott Finley <brian@thefinleys.com>
9 # Copyright (C) 2006 David Decotigny <com.d2@free.fr>
10 # Copyright (C) 2006 Simon Gerber <gesimu@gmail.com>
11 # Copyright (C) 2006-2007 Joey Hurst <jhurst@lucubrate.org>
12 # Copyright (C) 2006, 2009 Ante Karamatic <ivoks@ubuntu.com>
13 # Copyright (C) 2009-2010,2014 Sean Robinson <seankrobinson@gmail.com>
14 # Copyright (C) 2010 Prokhor Shuchalov <p@shuchalov.ru>
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; version 2 of the License.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License in LICENSE.GPL for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 # http://wifi-radar.tuxfamily.org/
31 # See CREDITS file for more contributors.
32 # See HISTORY file for, well, changes.
34 # NOTE: Remove the '-OO' from '#!/usr/bin/python -OO' in the first line to
35 # turn on console debugging.
38 from __future__ import print_function
40 import logging
41 import logging.handlers
42 import os
43 import sys
45 import wifiradar
46 import wifiradar.config
47 import wifiradar.misc as misc
48 import wifiradar.gui.g2 as ui
49 import wifiradar.gui.g2.transients as transients
52 # Where the conf file should live could be different for your distro. Please change
53 # at install time with "make install sysconfdir=/etc/wifi-radar" or similar. -BEF-
55 CONF_FILE = "/etc/wifi-radar/wifi-radar.conf"
57 os.environ['LC_MESSAGES'] = 'C'
59 # Set up a logging framework.
60 logger = logging.getLogger("wifiradar")
62 generic_formatter = logging.Formatter(
63 fmt='%(asctime)s.%(msecs)d %(name)s: %(message)s',
64 datefmt='%Y-%m-%d %H:%M:%S')
66 consoleLogHandler = logging.StreamHandler()
67 consoleLogHandler.setFormatter(generic_formatter)
68 logger.addHandler(consoleLogHandler)
70 # Load our conf file and known profiles
71 # Defaults, these may get overridden by values found in the conf file.
72 config_defaults = { # The network interface you use.
73 # Specify "auto_detect" as the interface to make wifi-radar automatically detect your wireless card.
74 'interface': "auto_detect",
75 # How long should the scan for access points last?
76 #'scan_timeout': '5',
77 # You may set this to true for cards that require a "commit" command with iwconfig
78 'commit_required': 'False',
79 # You may set this to true for cards that require the interface to be brought up first
80 'ifup_required': 'False',
81 # set the location and verbosity of the log file
82 'logfile': '/var/log/wifi-radar.log',
83 'loglevel': '50',
84 # Set the location of several important programs
85 'iwlist_command': '/sbin/iwlist',
86 'iwconfig_command': '/sbin/iwconfig',
87 'ifconfig_command': '/sbin/ifconfig',
88 'route_command': '/sbin/route',
89 'auto_profile_order': '[]',
90 'version': misc.WIFI_RADAR_VERSION }
92 config_dhcp = { # DHCP client
93 'command': '/sbin/dhcpcd',
94 # How long to wait for an IP addr from DHCP server
95 'timeout': '30',
96 # Arguments to use with DHCP client on connect
97 'args': '-D -o -i dhcp_client -t %(timeout)s',
98 # Argument to use with DHCP client on disconnect
99 'kill_args': '-k',
100 # The file where DHCP client PID is written
101 'pidfile': '/etc/dhcpc/dhcpcd-%(interface)s.pid' }
103 config_wpa = { # WPA Supplicant
104 'command': '/usr/sbin/wpa_supplicant',
105 # Arguments to use with WPA Supplicant on connect
106 'args': '-B -i %(interface)s -c %(configuration)s -D %(driver)s -P %(pidfile)s',
107 # Arguments to use with WPA Supplicant on disconnect
108 'kill_command': '',
109 # Where the WPA Supplicant config file can be found
110 'configuration': '/etc/wpa_supplicant.conf',
111 # Driver to use with WPA Supplicant
112 'driver': 'wext',
113 # The file where WPA Supplicant PID is written
114 'pidfile': '/var/run/wpa_supplicant.pid' }
116 # initialize config, with defaults
117 config = wifiradar.config.ConfigFileManager(CONF_FILE, config_defaults)
118 config.set_section("DHCP", config_dhcp)
119 config.set_section("WPA", config_wpa)
121 if not os.path.isfile(CONF_FILE):
122 config.set_bool_opt('DEFAULT', 'new_file', True)
123 else:
124 if not os.access(CONF_FILE, os.R_OK):
125 print("Can't open {FILE}".format(FILE=CONF_FILE))
126 print("Are you root?")
127 sys.exit()
128 try:
129 config.read()
130 except (NameError, SyntaxError) as e:
131 error_message = ('A configuration file from a pre-2.0 version '
132 'of WiFi Radar was found at {FILE}.\n\nWiFi Radar v2.0.x does '
133 'not read configuration files from previous versions. ')
134 if isinstance(e, NameError):
135 error_message += ('Because {FILE} may contain information '
136 'that you might wish to use when configuring WiFi Radar '
137 '{VERSION}, rename this file and run the program again.')
138 elif isinstance(e, SyntaxError):
139 error_message += ('The old configuration file is probably '
140 'empty and can be removed. Rename {FILE} if you want '
141 'to be very careful. After removing or renaming '
142 '{FILE}, run this program again.')
143 error_message = error_message.format(FILE=CONF_FILE,
144 VERSION=misc.WIFI_RADAR_VERSION)
145 error_dlg = transients.ErrorDialog(None, error_message)
146 del error_dlg
147 logger.critical(error_message)
148 sys.exit()
152 ####################################################################################################
153 # Make so we can be imported
154 if __name__ == "__main__":
155 if len(sys.argv) > 1 and (sys.argv[1] == '--version' or sys.argv[1] == '-v'):
156 print('WiFi Radar version {VERSION}'.format(VERSION=misc.WIFI_RADAR_VERSION))
157 elif len(sys.argv) > 1 and (sys.argv[1] == '--help' or sys.argv[1] == '-h'):
158 print('WiFi Radar version {VERSION}'.format(VERSION=misc.WIFI_RADAR_VERSION))
159 print("For help, check man pages for wifi-radar and wifi-radar.conf,")
160 print("or visit http://wifi-radar.tuxfamily.org/")
161 else:
162 wifiradar.Main(config)