1
0
mirror of https://github.com/gryf/wicd.git synced 2026-01-28 19:25:45 +01:00

Adjust backend module to use backends directly.

This commit is contained in:
2020-08-17 19:26:15 +02:00
parent 28d07f826b
commit 9ca790dd49
2 changed files with 11 additions and 30 deletions

View File

@@ -22,12 +22,12 @@ Manages and loads the pluggable backends for wicd.
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
import sys
import os import os
import wicd.wpath as wpath import wicd.backends as bck
BACKENDS = {x.split('_')[1]: x for x in dir(bck) if not x.startswith('__')}
def fail(backend_name, reason): def fail(backend_name, reason):
@@ -40,7 +40,6 @@ class BackendManager(object):
"""Manages, validates, and loads wicd backends.""" """Manages, validates, and loads wicd backends."""
def __init__(self): def __init__(self):
"""Initialize the backend manager.""" """Initialize the backend manager."""
self.backend_dir = wpath.backends
self.__loaded_backend = None self.__loaded_backend = None
def _valid_backend_file(self, be_file): def _valid_backend_file(self, be_file):
@@ -58,11 +57,7 @@ class BackendManager(object):
def get_available_backends(self): def get_available_backends(self):
"""Returns a list of all valid backends in the backend directory.""" """Returns a list of all valid backends in the backend directory."""
be_list = [] return list(BACKENDS)
for f in os.listdir(self.backend_dir):
if self._valid_backend_file(os.path.join(self.backend_dir, f)):
be_list.append(f[3:-3])
return be_list or [""]
def get_update_interval(self): def get_update_interval(self):
"""Returns how often in seconds the wicd monitor should update.""" """Returns how often in seconds the wicd monitor should update."""
@@ -73,25 +68,11 @@ class BackendManager(object):
def get_backend_description(self, backend_name): def get_backend_description(self, backend_name):
"""Loads a backend and returns its description.""" """Loads a backend and returns its description."""
backend = self._load_backend(backend_name) try:
if backend and backend.DESCRIPTION: return BACKENDS[backend_name].DESCRIPTION
return backend.DESCRIPTION except KeyError:
else:
return "No backend data available" return "No backend data available"
def _load_backend(self, backend_name):
"""Imports a backend and returns the loaded module."""
print(('trying to load backend %s' % backend_name))
backend_path = os.path.join(self.backend_dir,
'be-' + backend_name + '.py')
if self._valid_backend_file(backend_path):
sys.path.insert(0, self.backend_dir)
backend = __import__('be-' + backend_name)
return backend
else:
fail(backend_name, 'Invalid backend file.')
return None
def _validate_backend(self, backend, backend_name): def _validate_backend(self, backend, backend_name):
"""Ensures that a backend module is valid.""" """Ensures that a backend module is valid."""
failed = False failed = False
@@ -116,8 +97,9 @@ class BackendManager(object):
valid. valid.
""" """
backend = self._load_backend(backend_name) try:
if not backend: backend = BACKENDS[backend_name]
except KeyError:
return None return None
failed = self._validate_backend(backend, backend_name) failed = self._validate_backend(backend, backend_name)

View File

@@ -38,7 +38,6 @@ import struct
import time import time
from wicd import misc from wicd import misc
from wicd import wpath
from wicd.wnettools import BaseInterface from wicd.wnettools import BaseInterface
from wicd.wnettools import BaseWiredInterface from wicd.wnettools import BaseWiredInterface
from wicd.wnettools import BaseWirelessInterface from wicd.wnettools import BaseWirelessInterface