<filename>cui/serviceset_screen.py
# Core modules - Developers only
import py_cui
import os
import shutil

import sys
sys.path.insert(1, '..')

import i18n
i18n.load_path.append('./locales/')
i18n.set('filename_format', '{namespace}.{format}')

# Local modules - Developers only
import cui.screen_manager
import tasks.logr as LOG
import tasks.dev as DEV
import tasks.config as CONFIG
import tasks.serviceset as TASKS
import tasks.ansible as ANSIBLE
import tasks.choice as CHOICE


# Functions - Developers only

class ServiceSettings_Screen(cui.screen_manager.ScreenManager):
# ServiceSettings Screen Class
    def initialize_screen_elements(self):
    # ServiceSettings Screen widget
        serviceset_widget_set = self.manager.root.create_new_widget_set(8, 8)

        self.page_heading = serviceset_widget_set.add_label(i18n.t('serviceset.screen.page_heading'), 0, 2, column_span=4)
        self.page_heading.set_selectable(False)

        self.option_list = serviceset_widget_set.add_scroll_menu(i18n.t('serviceset.screen.option_list_title'), 1, 1, row_span=5, column_span=2)
        self.option_list.add_key_command(py_cui.keys.KEY_E_UPPER, self.servicesetting_change)
        self.option_list.add_key_command(py_cui.keys.KEY_E_LOWER, self.servicesetting_change)
        self.option_list.add_key_command(py_cui.keys.KEY_ENTER, self.get_servicesettings_desc)
        self.option_list.add_text_color_rule('?', py_cui.YELLOW_ON_BLACK,'startswith', selected_color=py_cui.YELLOW_ON_WHITE)
        self.option_list.add_text_color_rule('', py_cui.WHITE_ON_BLACK,'startswith', selected_color=py_cui.BLACK_ON_WHITE)
        self.option_list.set_focus_border_color(color=py_cui.GREEN_ON_BLACK)
        self.option_list.set_focus_text(i18n.t('serviceset.screen.option_list_keys'))

        self.option_desc = serviceset_widget_set.add_block_label(i18n.t('serviceset.screen.option_desc_placeholder'), 1, 3, row_span=4, column_span=3, padx=5, center=0)

        self.main_menu_popup = serviceset_widget_set.add_button(i18n.t('base.screen.main_menu'), 7, 1, row_span=1, column_span=1, command=self.main_menu_popup)
        self.main_menu_popup.add_text_color_rule('', py_cui.WHITE_ON_BLACK,'startswith')

        self.accept_parameters = serviceset_widget_set.add_button(i18n.t('serviceset.screen.accept_parameters'), 7, 3, row_span=1, column_span=2, command=self.accept_changes)
        self.accept_parameters.set_color(color=py_cui.CYAN_ON_BLACK)
        self.accept_parameters.add_text_color_rule('', py_cui.WHITE_ON_BLACK,'startswith')

        return serviceset_widget_set


    def main_menu_choice(self, menu_item):
    # ServiceSet Screen Main Menu Choices
        if menu_item == i18n.t('base.main_menu.service_update').format(DEV.service_selection):
        # Updates the specified service
            self.service_update()
        elif menu_item == i18n.t('base.main_menu.service_restart').format(DEV.service_selection):
        # Restarts the specified service
            self.service_restart()
        elif menu_item == i18n.t('base.main_menu.service_customize').format(DEV.service_selection):
        # Restarts the specified service
            self.service_customize()
        elif menu_item == i18n.t('base.main_menu.service_stop').format(DEV.service_selection):
        # Restarts the specified service
            self.service_stop()
        elif menu_item == i18n.t('base.main_menu.service_remove').format(DEV.service_selection):
        # Restarts the specified service
            self.service_remove()
        elif menu_item == i18n.t('base.main_menu.service_reset').format(DEV.service_selection):
        # Restarts the specified service
            self.service_reset()
        elif menu_item == i18n.t('base.main_menu.service_info'):
        # Gets the Service Info Screen
            DEV.prev_screen = 'serviceset'
            self.manager.get_serviceinfo_screen()
        elif menu_item == i18n.t('base.main_menu.back_to_start'):
        # Goes back to start, closes and re-encrypts config
            LOG.write(i18n.t('log.choicesc.move_to_start').format(DEV.current_configfile))
            CONFIG.encrypt_config()
            CHOICE.get_configs()
            self.manager.get_choice_screen()
        elif menu_item == i18n.t('base.main_menu.get_about_screen'):
        # Gets the About Screen
            DEV.prev_screen = 'serviceset'
            self.manager.get_about_screen()
        else:
            pass


    def main_menu_popup(self):
    # Main Menu Popup
        menu_choices = [i18n.t('base.main_menu.service_update').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_restart').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_customize').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_stop').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_remove').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_reset').format(DEV.service_selection),
                        "---",
                        i18n.t('base.main_menu.service_info'),
                        i18n.t('base.main_menu.back_to_start'),
                        i18n.t('base.main_menu.get_about_screen')
                        ]
        self.manager.root.show_menu_popup(i18n.t('base.screen.main_menu'), menu_choices, self.main_menu_choice)


    def accept_changes(self):
    # Return to operations screen with settings changed
        self.option_list.clear()

        self.manager.get_operations_screen()


    def service_customize(self):
    # Create a temporary dockerfile for specified service
        if not os.path.isdir(DEV.override_path + DEV.service_key):
            os.mkdir(DEV.override_path + DEV.service_key)
            os.chmod(DEV.override_path + DEV.service_key, 0o755)

        if os.path.exists(DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2'):
            os.remove(DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2')
        shutil.copy(DEV.rolespath + DEV.service_key + '/templates/docker-compose.' + DEV.service_key + '.yml.j2',
                    DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2')
        os.chmod(DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2', 0o755) # Sets new permissions

        self.manager.root.show_message_popup('', i18n.t('serviceset.popup.override_create')
                                             .format(DEV.service_selection, DEV.override_path + DEV.service_key))
        LOG.write(i18n.t('log.servicesetsc.override_create').format(DEV.service_selection))


    def service_update(self):
    # Update specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_update()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_restart(self):
    # Restart specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_restart()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_stop(self):
    # Stop specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_stop()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_remove(self):
    # Remove specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_remove()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_reset(self):
    # Reset specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_reset()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def set_servicesettingkey(self):
    # Sets the settings key, related to the service
        k_pos = self.option_list.get()
        settingkey = str(k_pos)
        return settingkey


    def get_servicesettings_desc(self):
    # List all editable settings for selected service
        DEV.servicesetting_key = self.set_servicesettingkey()

        self.option_desc.set_title('\n' + i18n.t('serviceset.screen.current_value') \
                                   + ' ' + str(TASKS.get_prev_value()) + '\n\n\n' \
                                   + i18n.t('serviceset.screen.option_title') + '\n' \
                                   + i18n.t('serviceset_tooltips.' + DEV.servicesetting_key[1:]) \
                                   + '\n\n' + i18n.t('serviceset_tooltips.see_tooltip')
                               )
        LOG.write(i18n.t('log.servicesetsc.desc_given')
                  .format(DEV.service_key, DEV.servicesetting_key[1:]))


    def servicesetting_change(self):
    # Popup for settings change
        DEV.servicesetting_key = self.set_servicesettingkey()

        pop_up = TASKS.which_popup(DEV.servicesetting_key[1:])

        if pop_up == 'yn':
            self.manager.root.show_yes_no_popup(i18n.t('serviceset.popup.yesno')
                        .format(' /' + DEV.service_key + DEV.servicesetting_key),
                                                self.set_usersettings_yesno
                                                )
        else:
            self.manager.root.show_text_box_popup(i18n.t('serviceset.popup.textbox')
                        .format(' /' + DEV.service_key + DEV.servicesetting_key),
                                                  self.set_usersettings_text
                                                  )


    def set_usersettings_yesno(self, yn_set):
    # Writes new setting to config file, if changed
        if yn_set:
            LOG.write(i18n.t('log.servicesetsc.set_to')
                      .format(DEV.servicesetting_key[1:], str('true')))
            TASKS.set_newvalue(True)
            self.manager.root.close_popup()
            self.manager.root.move_focus(self.option_list)
        else:
            LOG.write(i18n.t('log.servicesetsc.set_to')
                      .format(DEV.servicesetting_key[1:], str('false')))
            TASKS.set_newvalue(False)
            self.manager.root.close_popup()
            self.manager.root.move_focus(self.option_list)


    def set_usersettings_text(self, text_set):
    # Writes new setting to config file, if changed
        if text_set:
            if DEV.check_specialchars(text_set) == False:
                LOG.write(i18n.t('log.servicesetsc.set_to')
                          .format(DEV.servicesetting_key[1:], str(text_set)))
                TASKS.set_newvalue(text_set)
            else:
                LOG.write(i18n.t('log.servicesetsc.changes_non'))
                self.manager.root.move_focus(self.option_list)
        else:
            LOG.write(i18n.t('log.servicesetsc.changes_non'))
            self.manager.root.move_focus(self.option_list)

<filename>cui/serviceset_screen.py
# Core modules - Developers only
import py_cui
import os
import shutil

import sys
sys.path.insert(1, '..')

import i18n
i18n.load_path.append('./locales/')
i18n.set('filename_format', '{namespace}.{format}')

# Local modules - Developers only
import cui.screen_manager
import tasks.logr as LOG
import tasks.dev as DEV
import tasks.config as CONFIG
import tasks.serviceset as TASKS
import tasks.ansible as ANSIBLE
import tasks.choice as CHOICE


# Functions - Developers only

class ServiceSettings_Screen(cui.screen_manager.ScreenManager):
# ServiceSettings Screen Class
    def initialize_screen_elements(self):
    # ServiceSettings Screen widget
        serviceset_widget_set = self.manager.root.create_new_widget_set(8, 8)

        self.page_heading = serviceset_widget_set.add_label(i18n.t('serviceset.screen.page_heading'), 0, 2, column_span=4)
        self.page_heading.set_selectable(False)

        self.option_list = serviceset_widget_set.add_scroll_menu(i18n.t('serviceset.screen.option_list_title'), 1, 1, row_span=5, column_span=2)
        self.option_list.add_key_command(py_cui.keys.KEY_E_UPPER, self.servicesetting_change)
        self.option_list.add_key_command(py_cui.keys.KEY_E_LOWER, self.servicesetting_change)
        self.option_list.add_key_command(py_cui.keys.KEY_ENTER, self.get_servicesettings_desc)
        self.option_list.add_text_color_rule('?', py_cui.YELLOW_ON_BLACK,'startswith', selected_color=py_cui.YELLOW_ON_WHITE)
        self.option_list.add_text_color_rule('', py_cui.WHITE_ON_BLACK,'startswith', selected_color=py_cui.BLACK_ON_WHITE)
        self.option_list.set_focus_border_color(color=py_cui.GREEN_ON_BLACK)
        self.option_list.set_focus_text(i18n.t('serviceset.screen.option_list_keys'))

        self.option_desc = serviceset_widget_set.add_block_label(i18n.t('serviceset.screen.option_desc_placeholder'), 1, 3, row_span=4, column_span=3, padx=5, center=0)

        self.main_menu_popup = serviceset_widget_set.add_button(i18n.t('base.screen.main_menu'), 7, 1, row_span=1, column_span=1, command=self.main_menu_popup)
        self.main_menu_popup.add_text_color_rule('', py_cui.WHITE_ON_BLACK,'startswith')

        self.accept_parameters = serviceset_widget_set.add_button(i18n.t('serviceset.screen.accept_parameters'), 7, 3, row_span=1, column_span=2, command=self.accept_changes)
        self.accept_parameters.set_color(color=py_cui.CYAN_ON_BLACK)
        self.accept_parameters.add_text_color_rule('', py_cui.WHITE_ON_BLACK,'startswith')

        return serviceset_widget_set


    def main_menu_choice(self, menu_item):
    # ServiceSet Screen Main Menu Choices
        if menu_item == i18n.t('base.main_menu.service_update').format(DEV.service_selection):
        # Updates the specified service
            self.service_update()
        elif menu_item == i18n.t('base.main_menu.service_restart').format(DEV.service_selection):
        # Restarts the specified service
            self.service_restart()
        elif menu_item == i18n.t('base.main_menu.service_customize').format(DEV.service_selection):
        # Restarts the specified service
            self.service_customize()
        elif menu_item == i18n.t('base.main_menu.service_stop').format(DEV.service_selection):
        # Restarts the specified service
            self.service_stop()
        elif menu_item == i18n.t('base.main_menu.service_remove').format(DEV.service_selection):
        # Restarts the specified service
            self.service_remove()
        elif menu_item == i18n.t('base.main_menu.service_reset').format(DEV.service_selection):
        # Restarts the specified service
            self.service_reset()
        elif menu_item == i18n.t('base.main_menu.service_info'):
        # Gets the Service Info Screen
            DEV.prev_screen = 'serviceset'
            self.manager.get_serviceinfo_screen()
        elif menu_item == i18n.t('base.main_menu.back_to_start'):
        # Goes back to start, closes and re-encrypts config
            LOG.write(i18n.t('log.choicesc.move_to_start').format(DEV.current_configfile))
            CONFIG.encrypt_config()
            CHOICE.get_configs()
            self.manager.get_choice_screen()
        elif menu_item == i18n.t('base.main_menu.get_about_screen'):
        # Gets the About Screen
            DEV.prev_screen = 'serviceset'
            self.manager.get_about_screen()
        else:
            pass


    def main_menu_popup(self):
    # Main Menu Popup
        menu_choices = [i18n.t('base.main_menu.service_update').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_restart').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_customize').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_stop').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_remove').format(DEV.service_selection),
                        i18n.t('base.main_menu.service_reset').format(DEV.service_selection),
                        "---",
                        i18n.t('base.main_menu.service_info'),
                        i18n.t('base.main_menu.back_to_start'),
                        i18n.t('base.main_menu.get_about_screen')
                        ]
        self.manager.root.show_menu_popup(i18n.t('base.screen.main_menu'), menu_choices, self.main_menu_choice)


    def accept_changes(self):
    # Return to operations screen with settings changed
        self.option_list.clear()

        self.manager.get_operations_screen()


    def service_customize(self):
    # Create a temporary dockerfile for specified service
        if not os.path.isdir(DEV.override_path + DEV.service_key):
            os.mkdir(DEV.override_path + DEV.service_key)
            os.chmod(DEV.override_path + DEV.service_key, 0o755)

        if os.path.exists(DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2'):
            os.remove(DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2')
        shutil.copy(DEV.rolespath + DEV.service_key + '/templates/docker-compose.' + DEV.service_key + '.yml.j2',
                    DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2')
        os.chmod(DEV.override_path + DEV.service_key + '/docker-compose.override.yml.j2', 0o755) # Sets new permissions

        self.manager.root.show_message_popup('', i18n.t('serviceset.popup.override_create')
                                             .format(DEV.service_selection, DEV.override_path + DEV.service_key))
        LOG.write(i18n.t('log.servicesetsc.override_create').format(DEV.service_selection))


    def service_update(self):
    # Update specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_update()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_restart(self):
    # Restart specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_restart()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_stop(self):
    # Stop specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_stop()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_remove(self):
    # Remove specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_remove()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def service_reset(self):
    # Reset specified service
        self.option_list.clear()
        self.manager.root.stop()
        ANSIBLE.run_reset()

        self.manager.get_operations_screen()
        self.manager.root.start()


    def set_servicesettingkey(self):
    # Sets the settings key, related to the service
        k_pos = self.option_list.get()
        settingkey = str(k_pos)
        return settingkey


    def get_servicesettings_desc(self):
    # List all editable settings for selected service
        DEV.servicesetting_key = self.set_servicesettingkey()

        self.option_desc.set_title('\n' + i18n.t('serviceset.screen.current_value') \
                                   + ' ' + str(TASKS.get_prev_value()) + '\n\n\n' \
                                   + i18n.t('serviceset.screen.option_title') + '\n' \
                                   + i18n.t('serviceset_tooltips.' + DEV.servicesetting_key[1:]) \
                                   + '\n\n' + i18n.t('serviceset_tooltips.see_tooltip')
                               )
        LOG.write(i18n.t('log.servicesetsc.desc_given')
                  .format(DEV.service_key, DEV.servicesetting_key[1:]))


    def servicesetting_change(self):
    # Popup for settings change
        DEV.servicesetting_key = self.set_servicesettingkey()

        pop_up = TASKS.which_popup(DEV.servicesetting_key[1:])

        if pop_up == 'yn':
            self.manager.root.show_yes_no_popup(i18n.t('serviceset.popup.yesno')
                        .format(' /' + DEV.service_key + DEV.servicesetting_key),
                                                self.set_usersettings_yesno
                                                )
        else:
            self.manager.root.show_text_box_popup(i18n.t('serviceset.popup.textbox')
                        .format(' /' + DEV.service_key + DEV.servicesetting_key),
                                                  self.set_usersettings_text
                                                  )


    def set_usersettings_yesno(self, yn_set):
    # Writes new setting to config file, if changed
        if yn_set:
            LOG.write(i18n.t('log.servicesetsc.set_to')
                      .format(DEV.servicesetting_key[1:], str('true')))
            TASKS.set_newvalue(True)
            self.manager.root.close_popup()
            self.manager.root.move_focus(self.option_list)
        else:
            LOG.write(i18n.t('log.servicesetsc.set_to')
                      .format(DEV.servicesetting_key[1:], str('false')))
            TASKS.set_newvalue(False)
            self.manager.root.close_popup()
            self.manager.root.move_focus(self.option_list)


    def set_usersettings_text(self, text_set):
    # Writes new setting to config file, if changed
        if text_set:
            if DEV.check_specialchars(text_set) == False:
                LOG.write(i18n.t('log.servicesetsc.set_to')
                          .format(DEV.servicesetting_key[1:], str(text_set)))
                TASKS.set_newvalue(text_set)
            else:
                LOG.write(i18n.t('log.servicesetsc.changes_non'))
                self.manager.root.move_focus(self.option_list)
        else:
            LOG.write(i18n.t('log.servicesetsc.changes_non'))
            self.manager.root.move_focus(self.option_list)

# Core modules - Developers only # Local modules - Developers only # Functions - Developers only # ServiceSettings Screen Class # ServiceSettings Screen widget # ServiceSet Screen Main Menu Choices # Updates the specified service # Restarts the specified service # Restarts the specified service # Restarts the specified service # Restarts the specified service # Restarts the specified service # Gets the Service Info Screen # Goes back to start, closes and re-encrypts config # Gets the About Screen # Main Menu Popup # Return to operations screen with settings changed # Create a temporary dockerfile for specified service # Sets new permissions # Update specified service # Restart specified service # Stop specified service # Remove specified service # Reset specified service # Sets the settings key, related to the service # List all editable settings for selected service # Popup for settings change # Writes new setting to config file, if changed # Writes new setting to config file, if changed