def on_action_connect_activate(self, action): """Establish the connection for the destination""" selected_row = get_treeview_selected_row(self.ui.tvw_connections) if selected_row and not self.is_selected_row_host(): host = self.hosts[self.model_hosts.get_key( self.ui.store_hosts.iter_parent(selected_row))] destination_name = self.model_hosts.get_key(selected_row) destination = host.destinations[destination_name] description = self.model_hosts.get_association(selected_row) service_name = self.model_hosts.get_service(selected_row) service_arguments = self.model_hosts.get_arguments(selected_row) arguments = json.loads(service_arguments) association = host.find_association(description=description, destination=destination.name, service=service_name, arguments=arguments) if service_name in model_services.services: service = model_services.services[service_name] command = service.command # Prepares the arguments arguments_map = {} arguments_map['address'] = destination.value for key in association.service_arguments: arguments_map[key] = association.service_arguments[key] # Execute command try: command = command.format(**arguments_map) processes.processes.add_process(host, destination, service, command) except KeyError as error: # An error occurred processing the command error_msg1 = _('Connection open failed') error_msg2 = _('An error occurred processing the ' 'service command.') show_message_dialog(class_=UIMessageDialogClose, parent=self.ui.win_main, message_type=Gtk.MessageType.ERROR, title=None, msg1=error_msg1, msg2=error_msg2, is_response_id=None) debug.add_error(error_msg2) debug.add_error('Host: "%s"' % host.name) debug.add_error('Destination name: "%s"' % destination.name) debug.add_error('Destination value: "%s"' % destination.value) debug.add_error('Service: %s' % service.name), debug.add_error('Command: "%s"' % command) else: debug.add_warning('service %s not found' % service_name)
def on_action_copy_activate(self, action): """Copy the selected host to another""" selected_row = get_treeview_selected_row(self.ui.tvw_connections) if selected_row: if self.is_selected_row_host(): # First level (host) name = self.model_hosts.get_key(selected_row) description = self.model_hosts.get_description(selected_row) selected_iter = self.model_hosts.get_iter(name) expanded = self.ui.tvw_connections.row_expanded( self.model_hosts.get_path(selected_iter)) dialog = UIHost(parent=self.ui.win_main, hosts=self.model_hosts) # Restore the destinations for the selected host destinations = self.hosts[name].destinations for destination_name in destinations: destination = destinations[destination_name] dialog.model_destinations.add_data(destination) # Restore the associations for the selected host for association in self.hosts[name].associations: service_name = association.service_name if service_name in model_services.services: dialog.model_associations.add_data( index=dialog.model_associations.count(), name=association.destination_name, description=association.description, service=model_services.services[service_name], arguments=association.service_arguments) else: debug.add_warning('service %s not found' % service_name) # Show the edit host dialog response = dialog.show(default_name=_('Copy of %s') % name, default_description='', title=_('Copy host'), treeiter=None) if response == Gtk.ResponseType.OK: destinations = dialog.model_destinations.dump() associations = dialog.model_associations.dump() host = HostInfo(dialog.name, dialog.description) # Set the associations for values in associations: (destination_name, description, service_name, service_arguments) = associations[values] destination = destinations[destination_name] arguments = json.loads(service_arguments) host.add_association(description=description, destination_name=destination_name, service_name=service_name, arguments=arguments) self.add_host(host=host, destinations=destinations, update_settings=True) # Get the path of the host tree_path = self.model_hosts.get_path_by_name(dialog.name) # Automatically select again the previously selected host self.ui.tvw_connections.set_cursor(path=tree_path, column=None, start_editing=False) # Automatically expand the row if it was expanded before if expanded: self.ui.tvw_connections.expand_row(tree_path, False) # Collapse the duplicated row self.ui.tvw_connections.collapse_row( self.model_hosts.get_path(selected_iter))
def add_host(self, host, destinations, 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) # Add the destinations to the data for destination_name in destinations: destination = destinations[destination_name] host.add_destination(item=destination) # Add service associations to the model for association in host.associations: description = association.description service_name = association.service_name service_arguments = json.dumps(association.service_arguments) destination = destinations[association.destination_name] if service_name in model_services.services: service = model_services.services[service_name] self.model_hosts.add_association(treeiter=treeiter, description=description, destination=destination, service=service, arguments=service_arguments) else: debug.add_warning('service %s not found' % service_name) # 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) # Add destinations for key in host.destinations: destination = host.destinations[key] settings_host.set(section=SECTION_DESTINATIONS, option=destination.name, value=destination.value) association_index = 0 for association in host.associations: arguments = json.dumps(association.service_arguments) # Add associations to the settings association_index += 1 section = '%s %d' % (SECTION_ASSOCIATION, association_index) settings_host.set(section=section, option=OPTION_ASSOCIATION_DESCRIPTION, value=association.description) settings_host.set(section=section, option=OPTION_ASSOCIATION_DESTINATION, value=association.destination_name) settings_host.set(section=section, option=OPTION_ASSOCIATION_SERVICE, value=association.service_name) settings_host.set(section=section, option=OPTION_ASSOCIATION_ARGUMENTS, value=arguments) settings_host.set_int(section=SECTION_HOST, option=OPTION_HOST_ASSOCIATIONS, value=association_index) # Save the settings to the file settings_host.save()