def add_host(self, host, update_settings): """Add a new host along as with its destinations""" # Add the host to the data and to the model self.hosts[host.name] = host treeiter = self.model_hosts.add_data(host) # Update settings file if requested if update_settings: hosts_path = self.get_current_group_path() settings_host = settings.Settings(filename=os.path.join( hosts_path, '%s.conf' % host.name), case_sensitive=True) # Add host information settings_host.set(SECTION_HOST, OPTION_HOST_NAME, host.name) settings_host.set(SECTION_HOST, OPTION_HOST_DESCRIPTION, host.description) settings_host.set(SECTION_HOST, OPTION_HOST_PROTOCOL, host.protocol) settings_host.set(SECTION_HOST, OPTION_HOST_ADDRESS, host.address) settings_host.set_int(SECTION_HOST, OPTION_HOST_PORT, host.port_number) settings_host.set_int(SECTION_HOST, OPTION_HOST_VERSION, host.version) settings_host.set(SECTION_HOST, OPTION_HOST_COMMUNITY, host.community) settings_host.set(SECTION_HOST, OPTION_HOST_DEVICE, host.device) # Save the settings to the file settings_host.save()
def reload_hosts(self): """Load hosts from the settings files""" self.model_hosts.clear() self.hosts.clear() hosts_path = self.get_current_group_path() # Fix bug where the groups model isn't yet emptied, resulting in # being still used after a clear, then an invalid path if not os.path.isdir(hosts_path): return for filename in os.listdir(hosts_path): # Skip folders, used for groups if os.path.isdir(os.path.join(hosts_path, filename)): continue settings_host = settings.Settings(filename=os.path.join( hosts_path, filename), case_sensitive=True) host = HostInfo( name=settings_host.get(SECTION_HOST, OPTION_HOST_NAME), description=settings_host.get(SECTION_HOST, OPTION_HOST_DESCRIPTION), protocol=settings_host.get(SECTION_HOST, OPTION_HOST_PROTOCOL), address=settings_host.get(SECTION_HOST, OPTION_HOST_ADDRESS), port_number=settings_host.get_int(SECTION_HOST, OPTION_HOST_PORT), version=settings_host.get_int(SECTION_HOST, OPTION_HOST_VERSION), community=settings_host.get(SECTION_HOST, OPTION_HOST_COMMUNITY), device=settings_host.get(SECTION_HOST, OPTION_HOST_DEVICE)) self.add_host(host, False)
def __init__(self, application): self.application = application snmp.snmp = snmp.SNMP() # Load settings settings.settings = settings.Settings(FILE_SETTINGS, False) settings.positions = settings.Settings(FILE_WINDOWS_POSITION, False) settings.services = settings.Settings(FILE_SERVICES, False) settings.devices = settings.Settings(FILE_DEVICES, False) preferences.preferences = preferences.Preferences() # Load services for key in settings.services.get_sections(): model_services.services[key] = ServiceInfo( name=key, description=settings.services.get(key, OPTION_SERVICE_DESCRIPTION), numeric_oid=snmp.snmp.translate( settings.services.get(key, OPTION_SERVICE_DESCRIPTION))) # Load devices for key in settings.devices.get_sections(): model_devices.devices[key] = DeviceInfo( name=key, description=settings.devices.get(key, OPTION_DEVICE_DESCRIPTION), services=settings.devices.get_list(key, OPTION_DEVICE_SERVICES)) self.loadUI() self.model_hosts = ModelHosts(self.ui.store_hosts) self.model_groups = ModelGroups(self.ui.store_groups) # Load the groups and hosts list self.hosts = {} self.reload_groups() # Sort the data in the models self.model_groups.model.set_sort_column_id( self.ui.column_group.get_sort_column_id(), Gtk.SortType.ASCENDING) self.model_hosts.model.set_sort_column_id( self.ui.column_name.get_sort_column_id(), Gtk.SortType.ASCENDING) # Automatically select the first host if any self.ui.tvw_groups.set_cursor(0) if self.model_hosts.count() > 0: self.ui.tvw_connections.set_cursor(0) # Restore the saved size and position settings.positions.restore_window_position(self.ui.win_main, SECTION_WINDOW_NAME)