From 40b1065073f0e10b916678dfe2b201a1a7f91a87 Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 17 Aug 2020 19:23:49 +0200 Subject: [PATCH] Make use of config module --- wicd/curses/wicd_curses.py | 11 ++++--- wicd/misc.py | 16 +++++----- wicd/networking.py | 16 +++++----- wicd/translations.py | 7 +++-- wicd/wicd_daemon.py | 61 +++++++++++++++++++------------------- wicd/wnettools.py | 13 ++++---- 6 files changed, 64 insertions(+), 60 deletions(-) diff --git a/wicd/curses/wicd_curses.py b/wicd/curses/wicd_curses.py index ad6de39..805ceb5 100644 --- a/wicd/curses/wicd_curses.py +++ b/wicd/curses/wicd_curses.py @@ -39,18 +39,20 @@ from optparse import OptionParser import signal import sys import warnings +import os import urwid from dbus import DBusException # It took me a while to figure out that I have to use this. from gi.repository import GLib as gobject -from wicd import wpath +from wicd.config import CFG from wicd import misc from wicd import dbusmanager from wicd.translations import _ # Curses UIs for other stuff +import wicd.curses from wicd.curses.curses_misc import OptCols from wicd.curses.curses_misc import SelText from wicd.curses.curses_misc import NSelListBox @@ -66,6 +68,7 @@ from wicd.curses import netentry_curses from wicd.curses.netentry_curses import WirelessSettingsDialog from wicd.curses.netentry_curses import WiredSettingsDialog + # Stuff about getting the script configurer running # from grp import getgrgid # from os import getgroups, system @@ -73,7 +76,7 @@ from wicd.curses.netentry_curses import WiredSettingsDialog # import logging # import logging.handler -CURSES_REV = wpath.curses_revision +CURSES_REV = wicd.curses.__version__ # Fix strings in wicd-curses # from wicd.translations import language @@ -298,7 +301,7 @@ def help_dialog(body): def run_configscript(parent, netname, nettype): """Run configuration script.""" - configfile = wpath.etc + netname + '-settings.conf' + configfile = os.path.join(CFG.etc, netname + '-settings.conf') if nettype != 'wired': header = 'profile' else: @@ -1291,7 +1294,7 @@ def main(): if "DBus.Error.AccessDenied" in e.get_dbus_name(): print(_('ERROR: wicd-curses was denied access to the wicd daemon: ' 'please check that your user is in the "$A" group.') - .replace('$A', '\033[1;34m' + wpath.wicd_group + '\033[0m')) + .replace('$A', '\033[1;34m' + CFG.wicd_group + '\033[0m')) sys.exit(1) else: raise diff --git a/wicd/misc.py b/wicd/misc.py index fdd8b3a..d6c850d 100644 --- a/wicd/misc.py +++ b/wicd/misc.py @@ -40,7 +40,7 @@ from gi.repository import GLib as gobject from wicd.translations import _ # wicd imports -from wicd import wpath +from wicd.config import CFG # Connection state constants NOT_CONNECTED = 0 @@ -212,7 +212,7 @@ def IsValidIPv6(ip): def PromptToStartDaemon(): """Prompt the user to start the daemon""" - daemonloc = wpath.sbin + 'wicd' + daemonloc = os.path.join(CFG.sbin, 'wicd') sudo_prog = choose_sudo_prog() if not sudo_prog: return False @@ -307,8 +307,9 @@ def ParseEncryption(network): and creating a config file for it """ - enctemplate = open(wpath.encryption + network["enctype"]) - template = enctemplate.readlines() + with open(os.path.join(CFG.encryption, network["enctype"])) as fobj: + template = fobj.readlines() + if network.get('essid'): config_file = "ap_scan=1\n" else: @@ -348,7 +349,7 @@ def ParseEncryption(network): file_name = network['bssid'].replace(":", "").lower() else: file_name = 'wired' - file_loc = os.path.join(wpath.networks, file_name) + file_loc = os.path.join(CFG.networks, file_name) f = open(file_loc, "w") os.chmod(file_loc, 0o600) os.chown(file_loc, 0, 0) @@ -371,7 +372,8 @@ def LoadEncryptionMethods(wired=False): else: active_fname = "active" try: - enctypes = open(wpath.encryption + active_fname, "r").readlines() + with open(os.path.join(CFG.encryption, active_fname), "r") as fobj: + enctypes = fobj.readlines() except IOError as e: print("Fatal Error: template index file is missing.") raise IOError(e) @@ -405,7 +407,7 @@ def _parse_enc_template(enctype): return line.replace(key, "").replace("=", "").strip() try: - f = open(os.path.join(wpath.encryption, enctype), "r") + f = open(os.path.join(CFG.encryption, enctype), "r") except IOError: print(("Failed to open template file %s" % enctype)) return None diff --git a/wicd/networking.py b/wicd/networking.py index c8f1eb2..f1372dd 100644 --- a/wicd/networking.py +++ b/wicd/networking.py @@ -48,12 +48,10 @@ from signal import SIGTERM from functools import cmp_to_key # wicd imports +from wicd.config import CFG from wicd import misc -from wicd import wpath from wicd.backend import BackendManager -if __name__ == '__main__': - wpath.chdir(__file__) BACKEND = None BACKEND_MGR = BackendManager() @@ -227,7 +225,7 @@ class Controller(object): mac = 'X' if name in (None, ''): name = 'X' - misc.ExecuteScripts(wpath.predisconnectscripts, self.debug, + misc.ExecuteScripts(CFG.predisconnectscripts, self.debug, extra_parameters=(nettype, name, mac)) if self.pre_disconnect_script: print('Running pre-disconnect script') @@ -241,7 +239,7 @@ class Controller(object): iface.FlushDNS() iface.Down() iface.Up() - misc.ExecuteScripts(wpath.postdisconnectscripts, self.debug, + misc.ExecuteScripts(CFG.postdisconnectscripts, self.debug, extra_parameters=(nettype, name, mac)) if self.post_disconnect_script: print('Running post-disconnect script') @@ -965,7 +963,7 @@ class WirelessConnectThread(ConnectThread): self.is_connecting = True # Run pre-connection script. - self.run_scripts(wpath.preconnectscripts, + self.run_scripts(CFG.preconnectscripts, extra_parameters=('wireless', self.network['essid'], self.network['bssid'])) self.run_script_if_needed(self.before_script, 'pre-connection', @@ -1013,7 +1011,7 @@ class WirelessConnectThread(ConnectThread): self.verify_association(wiface) # Run post-connection script. - self.run_scripts(wpath.postconnectscripts, + self.run_scripts(CFG.postconnectscripts, extra_parameters=('wireless', self.network['essid'], self.network['bssid'])) self.run_script_if_needed(self.after_script, 'post-connection', @@ -1235,7 +1233,7 @@ class WiredConnectThread(ConnectThread): self.is_connecting = True # Run pre-connection script. - self.run_scripts(wpath.preconnectscripts, + self.run_scripts(CFG.preconnectscripts, extra_parameters=('wired', 'wired', self.network['profilename'])) self.run_script_if_needed(self.before_script, 'pre-connection', @@ -1262,7 +1260,7 @@ class WiredConnectThread(ConnectThread): self.set_dns_addresses(liface) # Run post-connection script. - self.run_scripts(wpath.postconnectscripts, + self.run_scripts(CFG.postconnectscripts, extra_parameters=('wired', 'wired', self.network['profilename'])) self.run_script_if_needed(self.after_script, 'post-connection', diff --git a/wicd/translations.py b/wicd/translations.py index 63262f7..953e11d 100644 --- a/wicd/translations.py +++ b/wicd/translations.py @@ -19,17 +19,18 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # +import gettext import locale import os -from wicd import wpath -import gettext + +from wicd.config import CFG def get_gettext(): """Set up gettext for translations.""" # Borrowed from an excellent post on how to do this at # http://www.learningpython.com/2006/12/03/translating-your-pythonpygtk-application/ - local_path = wpath.translations + local_path = CFG.translations langs = [] osLanguage = os.environ.get('LANGUAGE', None) if osLanguage: diff --git a/wicd/wicd_daemon.py b/wicd/wicd_daemon.py index fd0e354..2f7fbff 100644 --- a/wicd/wicd_daemon.py +++ b/wicd/wicd_daemon.py @@ -54,7 +54,8 @@ else: DBusGMainLoop(set_as_default=True) # wicd specific libraries -from wicd import wpath +import wicd +from wicd.config import CFG from wicd import networking from wicd import misc from wicd import wnettools @@ -62,14 +63,12 @@ from wicd.misc import noneToBlankString, _status_dict from wicd.logfile import ManagedStdio from wicd.configmanager import ConfigManager -if __name__ == '__main__': - wpath.chdir(__file__) misc.RenameProcess("wicd") -wireless_conf = os.path.join(wpath.etc, "wireless-settings.conf") -wired_conf = os.path.join(wpath.etc, "wired-settings.conf") -dhclient_conf = os.path.join(wpath.etc, "dhclient.conf.template") +wireless_conf = os.path.join(CFG.etc, "wireless-settings.conf") +wired_conf = os.path.join(CFG.etc, "wired-settings.conf") +dhclient_conf = os.path.join(CFG.etc, "dhclient.conf.template") class WicdDaemon(dbus.service.Object, object): @@ -85,7 +84,7 @@ class WicdDaemon(dbus.service.Object, object): """Initializes the daemon DBus object.""" dbus.service.Object.__init__(self, bus_name=bus_name, object_path=object_path) - self.config = ConfigManager(os.path.join(wpath.etc, + self.config = ConfigManager(os.path.join(CFG.etc, "manager-settings.conf")) self._debug_mode = bool(self.config.get("Settings", "debug_mode")) self.wifi = networking.Wireless(debug=self._debug_mode) @@ -158,7 +157,7 @@ class WicdDaemon(dbus.service.Object, object): anything >= 0. This number is effective starting wicd v1.2.0. """ - return wpath.version + return wicd.__version__ @dbus.service.method('org.wicd.daemon') def SetWiredInterface(self, interface): @@ -1720,7 +1719,7 @@ Arguments: \t-n\t--no-poll\tDon't monitor network status. \t-o\t--no-stdout\tDon't redirect stdout. \t-h\t--help\t\tPrint this help. -""" % (wpath.version + ' (bzr-r%s)' % wpath.revision)) +""" % (wicd.__version__ + ' (git-r%s)' % CFG.revision)) def daemonize(): @@ -1753,10 +1752,10 @@ def daemonize(): try: pid = os.fork() if pid > 0: - dirname = os.path.dirname(wpath.pidfile) + dirname = os.path.dirname(CFG.pidfile) if not os.path.exists(dirname): os.makedirs(dirname) - pidfile = open(wpath.pidfile, 'w') + pidfile = open(CFG.pidfile, 'w') pidfile.write(str(pid) + '\n') pidfile.close() sys.exit(0) @@ -1799,7 +1798,7 @@ def run(argv): """ # back up resolv.conf before we do anything else try: - backup_location = wpath.varlib + 'resolv.conf.orig' + backup_location = os.path.join(CFG.varlib, 'resolv.conf.orig') # Don't back up if the backup already exists, either as a regular file # or a symlink # The backup file should have been cleaned up by wicd, so perhaps it @@ -1854,14 +1853,14 @@ def run(argv): if kill: try: - f = open(wpath.pidfile) + f = open(CFG.pidfile) except IOError: # print >> sys.stderr, "No wicd instance active, aborting." sys.exit(1) # restore resolv.conf on quit try: - backup_location = wpath.varlib + 'resolv.conf.orig' + backup_location = os.path.join(CFG.varlib, 'resolv.conf.orig') if os.path.islink(backup_location): dest = os.readlink(backup_location) os.remove('/etc/resolv.conf') @@ -1884,33 +1883,33 @@ def run(argv): # quit, this should be the only option specified sys.exit(0) - if os.path.exists(wpath.pidfile): + if os.path.exists(CFG.pidfile): print('It seems like the daemon is already running.') - print('If it is not, please remove %s and try again.' % wpath.pidfile) + print('If it is not, please remove %s and try again.' % CFG.pidfile) sys.exit(1) - if not os.path.exists(wpath.networks): - os.makedirs(wpath.networks) + if not os.path.exists(CFG.networks): + os.makedirs(CFG.networks) if do_daemonize: daemonize() if redirect_stderr or redirect_stdout: - logpath = os.path.join(wpath.log, 'wicd.log') - if not os.path.exists(wpath.log): - os.makedirs(wpath.log) - os.chmod(wpath.log, 0o755) + logpath = os.path.join(CFG.log, 'wicd.log') + if not os.path.exists(CFG.log): + os.makedirs(CFG.log) + os.chmod(CFG.log, 0o755) output = ManagedStdio(logpath) if os.path.exists(logpath): try: - os.chmod(logpath, int(wpath.log_perms, 8)) + os.chmod(logpath, int(CFG.log_perms, 8)) except OSError: - print('unable to chmod log file to %s' % wpath.log_perms) + print('unable to chmod log file to %s' % CFG.log_perms) try: - if wpath.log_group: + if CFG.log_group: import grp - group = grp.getgrnam(wpath.log_group) + group = grp.getgrnam(CFG.log_group) os.chown(logpath, 0, group[2]) except OSError: print('unable to chown log file to %s' % group[2]) @@ -1924,7 +1923,7 @@ def run(argv): print('wicd initializing...') print('---------------------------') - print('wicd is version', wpath.version, wpath.revision) + print('wicd is version', wicd.__version__, CFG.revision) # Open the DBUS session bus = dbus.SystemBus() @@ -1933,8 +1932,8 @@ def run(argv): keep_connection=keep_connection) child_pid = None if not no_poll: - child_pid = Popen([wpath.python, "-O", - os.path.join(wpath.daemon, "monitor.py")], + child_pid = Popen([CFG.python, "-O", + os.path.join(CFG.daemon, "monitor.py")], shell=False, close_fds=True).pid atexit.register(on_exit, child_pid) @@ -1956,8 +1955,8 @@ def on_exit(child_pid): except OSError: pass print('Removing PID file...') - if os.path.exists(wpath.pidfile): - os.remove(wpath.pidfile) + if os.path.exists(CFG.pidfile): + os.remove(CFG.pidfile) print('Shutting down...') sys.exit(0) diff --git a/wicd/wnettools.py b/wicd/wnettools.py index 6bef898..250980b 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -41,10 +41,11 @@ import shutil import socket import time -from wicd import wpath +from wicd.config import CFG from wicd import misc from wicd.misc import find_path + # Regular expressions. _re_mode = (re.I | re.M | re.S) essid_pattern = re.compile('.*ESSID:"?(.*?)".*\n', _re_mode) @@ -342,7 +343,7 @@ class BaseInterface(object): return (client, cmd) # probably /var/lib/wicd/dhclient.conf with defaults - dhclient_conf_path = os.path.join(wpath.varlib, 'dhclient.conf') + dhclient_conf_path = os.path.join(CFG.varlib, 'dhclient.conf') client_dict = { "dhclient": @@ -382,7 +383,7 @@ class BaseInterface(object): 'better') dhclient_template = open(os.path. - join(wpath.etc, + join(CFG.etc, 'dhclient.conf.template'), 'r') output_conf = open(dhclient_conf_path, 'w') @@ -394,7 +395,7 @@ class BaseInterface(object): output_conf.close() dhclient_template.close() else: - shutil.copy(os.path.join(wpath.etc, + shutil.copy(os.path.join(CFG.etc, 'dhclient.conf.template'), dhclient_conf_path) @@ -1022,7 +1023,7 @@ class BaseWiredInterface(BaseInterface): """Authenticate with wpa_supplicant.""" misc.ParseEncryption(network) cmd = ['wpa_supplicant', '-B', '-i', self.iface, '-c', - os.path.join(wpath.networks, 'wired'), + os.path.join(CFG.networks, 'wired'), '-Dwired'] if self.verbose: print(cmd) @@ -1316,7 +1317,7 @@ class BaseWirelessInterface(BaseInterface): else: driver = '-D' + self.wpa_driver cmd = ['wpa_supplicant', '-B', '-i', self.iface, '-c', - os.path.join(wpath.networks, + os.path.join(CFG.networks, network['bssid'].replace(':', '').lower()), driver] if self.verbose: