Allow empty defaults when creating ConfigFileManager
[wifi-radar.git] / wifiradar / misc.py
blobe91819695c128338a8eaf6ed0074f2e91bfcb25c
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 # misc.py - collection of miscellaneous functions and classes
6 # Part of WiFi Radar: A utility for managing WiFi profiles on GNU/Linux.
8 # Copyright (C) 2004-2005 Ahmad Baitalmal <ahmad@baitalmal.com>
9 # Copyright (C) 2005 Nicolas Brouard <nicolas.brouard@mandrake.org>
10 # Copyright (C) 2005-2009 Brian Elliott Finley <brian@thefinleys.com>
11 # Copyright (C) 2006 David Decotigny <com.d2@free.fr>
12 # Copyright (C) 2006 Simon Gerber <gesimu@gmail.com>
13 # Copyright (C) 2006-2007 Joey Hurst <jhurst@lucubrate.org>
14 # Copyright (C) 2006, 2009 Ante Karamatic <ivoks@ubuntu.com>
15 # Copyright (C) 2009-2010,2014 Sean Robinson <robinson@tuxfamily.org>
16 # Copyright (C) 2010 Prokhor Shuchalov <p@shuchalov.ru>
18 # This program is free software; you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation; version 2 of the License.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License in LICENSE.GPL for more details.
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write to:
29 # Free Software Foundation, Inc.
30 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
34 from __future__ import unicode_literals
36 import errno
37 import gettext
38 import logging
39 import os
40 import sys
42 from subprocess import check_call, Popen, PIPE, STDOUT
44 WIFI_RADAR_VERSION = '0.0.0'
46 # create a logger
47 logger = logging.getLogger(__name__)
49 __all__ = ['_', 'PYVERSION', 'WIFI_RADAR_VERSION', 'DeviceError',
50 'NoDeviceError', 'PipeError', 'get_new_profile', 'shellcmd']
52 PYVERSION = sys.version_info[0]
54 # Look for string translations in the default location, then in the po
55 # directory under the current directory. This second location is useful
56 # during development.
57 for locale_dir in [gettext.bindtextdomain(None), os.path.realpath('./po/')]:
58 try:
59 trans = gettext.translation('wifi-radar', locale_dir)
60 except IOError as e:
61 logger.warning("{EXC} in '{DIR}'".format(EXC=e, DIR=locale_dir))
62 else:
63 logger.info("Translation file found in '{DIR}'".format(DIR=locale_dir))
64 break
65 else:
66 logger.warning('Translation disabled. Please report this '
67 'at http://wifi-radar.tuxfamily.org/')
68 trans = gettext.NullTranslations()
69 # Return a reference to the unicode version of gettext, no matter
70 # the Python version.
71 if PYVERSION < 3:
72 _ = trans.ugettext
73 else:
74 _ = trans.gettext
76 class DeviceError(EnvironmentError):
77 """ DeviceError will be raised when a problem is found while changing
78 a wireless device's configuration (e.g. when 'ifconfig wlan0 up'
79 fails).
80 """
81 pass
84 class NoDeviceError(EnvironmentError):
85 """ NoDeviceError will be raised when a wireless device cannot be
86 found.
87 """
88 pass
91 class PipeError(IOError):
92 """ PipeError is raised when a communication Pipe closes unexpectedly.
93 """
94 pass
96 def get_new_profile():
97 """ Return a blank profile.
99 Parameters:
101 none
103 Returns:
105 dictionary -- An AP profile with defaults set.
107 return { 'known': False,
108 'available': False,
109 'encrypted': False,
110 'essid': '',
111 'bssid': '',
112 'roaming': False,
113 'protocol': 'g',
114 'signal': -193,
115 'channel': 'auto',
116 'con_prescript': '',
117 'con_postscript': '',
118 'dis_prescript': '',
119 'dis_postscript': '',
120 'key': '',
121 'mode': 'auto',
122 'security': 'none',
123 'wep_mode': '',
124 'wpa_psk': '',
125 'use_dhcp': True,
126 'ip': '',
127 'netmask': '',
128 'gateway': '',
129 'domain': '',
130 'dns1': '',
131 'dns2': ''
134 def shellcmd(command, environment=None):
135 """ Run 'command' through the shell, where 'command' is a tuple of
136 the program to execute and its arguments. 'environment' is a
137 dictionary of shell environment variables (as keys) and their
138 values. Returns 0 on success, otherwise, it raises
139 CalledProcessError.
141 env_tmp = os.environ
142 env_tmp.update(environment)
143 return check_call(command, shell=True, env=env_tmp)
146 # Make so we can be imported
147 if __name__ == '__main__':
148 pass