class InterfaceManager(object): def __init__(self, status_change_callback=print_data): ''' Creates a new InterfaceManager object. ''' self._interfaces = {} path = wpath.etc + "interfaces.conf" self.config_manager = ConfigManager(path) self.backend_manager = BackendManager() self.backend_manager.load_all_available_backends() self.status_change_callback = status_change_callback def add(self, interface): ''' Adds interface to the dictionary of interfaces. ''' self._interfaces[interface.interface_name] = interface def create(self, type, interface_name): ''' Creates the interface, if possible. If interface exists, returns. ''' if not self.exists(interface_name): type_class = self.backend_manager.get_backend_by_type(type) new_interface = type_class(interface_name, self.status_change_callback) self.add(new_interface) print 'new interface: %s' % new_interface.interface_name else: print 'interface already exists: %s' % interface_name def get(self, interface_name): ''' Gets a single interface object. ''' return self._interfaces.get(interface_name) def exists(self, interface_name): ''' Returns True if the specified interface exists, otherwise False. ''' return bool(self._interfaces.get(interface_name)) def load(self): """ Loads the saved interface configuration. """ sections = self.config_manager.sections() for section in sections: interface_name = section type = self.config_manager.get_option(section, 'type') try: self.create(type, interface_name) except baseinterface.BaseInterface.CannotCreateInterfaceException: print 'error creating interface %s' % interface_name print 'skipping interface %s' % interface_name else: interface = self.get(interface_name) for k, v in self.config_manager.items(section): setattr(interface, k, v) def save(self): """ Saves the current interface configuration. """ self.config_manager.clear_all() for interface_name in self.get_all(): interface = self.get(interface_name) interface.do_save() settings = interface.dump_settings_to_dictionary() for k, v in settings.iteritems(): self.config_manager.set_option(interface_name, k, v) self.config_manager.write() def get_all(self): ''' Returns the interface dictionary. ''' return self._interfaces def get_all_names(self): ''' Returns the names of all the interfaces. ''' interfaces = self.get_all() names = [ value.interface_name for key, value in interfaces.iteritems() ] return names
class PluginManager(object): def __init__(self, daemon): self.daemon = daemon self.config_manager = ConfigManager('/etc/wicd/plugins.conf') self.loaded_plugins = [] self.load_all_plugins() def set_daemon(self, daemon): self.daemon = daemon def find_available_plugins(self): available_plugins = [] for item in os.listdir(os.path.join( os.path.dirname(os.path.realpath(__file__)), 'plugins')): if item.endswith('.py') and \ not item == 'baseplugin.py' and \ not item == '__init__.py': available_plugins.append(item) return available_plugins def load_all_plugins(self): classes = [] for plugin in self.find_available_plugins(): module = self.load_plugin(plugin) for item in dir(module): if item.endswith("Plugin") \ and not item == 'BasePlugin' \ and self.config_manager.get_option(item, 'enabled', True): the_class = getattr(module, item) classes.append(the_class) import operator classes.sort(key=operator.attrgetter('PRIORITY')) for the_class in classes: logging.debug( 'loading plugin %s', the_class) self.loaded_plugins.append(the_class(self.daemon)) def load_plugin(self, name): if name in self.find_available_plugins(): if name.endswith('.py'): name = name[:-3] module = __import__('plugins.' + str(name)) module = getattr(module, name) return module else: raise WicdError('Plugin %s was not found' % name) def action(self, action_name, values=()): for plugin in self.loaded_plugins: if hasattr(plugin, "do_%s" % action_name): getattr(plugin, "do_%s" % action_name)(*values)