Example #1
0
 def has_user_cfg(self):
     """
     Tells you if the element has user data saved.
     :rtype: bool
     """
     name = a2util.get_cfg_default_name(self.cfg)
     return self.mod.is_in_user_cfg(name)
Example #2
0
    def __init__(self, main, cfg, mod, user_cfg):
        self.is_expandable_widget = False
        super(Draw, self).__init__(parent=main)
        DrawCtrlMixin.__init__(self, main, cfg, mod, user_cfg)

        self.setTitle(self.cfg.get('label', ''))
        self.setCheckable(self.cfg.get('disablable', True))
        self.setChecked(self.get_user_value(bool, 'enabled'))
        self.clicked[bool].connect(self.check)

        self.a2_group_layout = QtWidgets.QVBoxLayout(self)

        # for some reason items in this GroupBox are 0px close to the
        # group box title. It works in settings view tho. So far I'm unable
        # to fix this via CSS. Enlighten me!
        self.a2_group_marging_top = QtWidgets.QWidget()
        self.a2_group_marging_top.setMaximumHeight(
            self.main.style.get('margin_h'))
        self.a2_group_layout.addWidget(self.a2_group_marging_top)

        expandable = False
        module_user_cfg = self.mod.get_user_cfg()
        for child in self.cfg.get('children', []):
            name = a2util.get_cfg_default_name(child)
            ctrl = a2ctrl.draw(self.main, child, self.mod,
                               module_user_cfg.get(name))
            if ctrl is not None and ctrl.is_expandable_widget:
                expandable = True
            if ctrl:
                self.a2_group_layout.addWidget(ctrl)
        if expandable:
            self.is_expandable_widget = True
Example #3
0
def assemble_settings(module_key, cfg_list, db_dict, module_path=None):
    a2obj = a2core.A2Obj.inst()
    module_user_cfg = a2obj.db.get('user_cfg', module_key) or {}
    for element_cfg in cfg_list:
        # get configs named db entry of module or None
        cfg_name = a2util.get_cfg_default_name(element_cfg)
        user_cfg = module_user_cfg.get(cfg_name)
        # pass if there is an 'enabled' entry and it's False
        if not get_cfg_value(element_cfg, user_cfg, 'enabled', default=True):
            continue

        element_get_settings_func = get_a2element_object(
            'get_settings', element_cfg, module_path)

        # no result try again with getting the module path:
        if element_get_settings_func is None and module_path is None:
            source_name, mod_name = module_key.split('|')
            module_path = a2obj.module_sources[source_name].mods[mod_name].path
            element_get_settings_func = get_a2element_object(
                'get_settings', element_cfg, module_path)

        if element_get_settings_func is not None:
            try:
                element_get_settings_func(module_key, element_cfg, db_dict, user_cfg)
            except Exception:
                log.error(traceback.format_exc().strip())
                log.error('Error calling get_settings function '
                          'for module: "%s"' % module_key)
Example #4
0
def assemble_settings(module_key, cfg_list, db_dict, module_path=None):
    a2obj = a2core.A2Obj.inst()
    module_user_cfg = a2obj.db.get('user_cfg', module_key) or {}
    for element_cfg in cfg_list:
        # get configs named db entry of module or None
        cfg_name = a2util.get_cfg_default_name(element_cfg)
        user_cfg = module_user_cfg.get(cfg_name)
        # pass if there is an 'enabled' entry and it's False
        if not get_cfg_value(element_cfg, user_cfg, 'enabled', default=True):
            continue

        element_get_settings_func = get_a2element_object(
            'get_settings', element_cfg, module_path)

        # no result try again with getting the module path:
        if element_get_settings_func is None and module_path is None:
            source_name, mod_name = module_key.split('|')
            module_path = a2obj.module_sources[source_name].mods[mod_name].path
            element_get_settings_func = get_a2element_object(
                'get_settings', element_cfg, module_path)

        if element_get_settings_func is not None:
            try:
                element_get_settings_func(module_key, element_cfg, db_dict,
                                          user_cfg)
            except Exception:
                log.error(traceback.format_exc().strip())
                log.error('Error calling get_settings function '
                          'for module: "%s"' % module_key)
Example #5
0
 def has_user_cfg(self):
     """
     Tells you if the element has user data saved.
     :rtype: bool
     """
     name = a2util.get_cfg_default_name(self.cfg)
     return self.mod.is_in_user_cfg(name)
Example #6
0
    def draw_mod(self):
        """
        from the modules config creates the usual display controls and
        fills them with the saved settings from the database.
        On change they trigger writing to the db, collect all include info
        and restart a2.
        """
        self.toggle_edit(False)

        if self.main.mod is None:
            if not self.main.num_selected:
                self.controls = [a2settings_view.A2Settings(self.main)]
                self.update_header()
                self.draw_ui()
                return

            else:
                config = [{'typ': 'nfo', 'author': '', 'version': '',
                           'description': 'Multiple modules selected. Here goes some '
                                          'useful info in the future...'}]
            module_user_cfg = {}
        else:
            config = self.main.mod.config
            module_user_cfg = self.main.mod.get_user_cfg()
            self.main.temp_config = None

        if len(config):
            if config[0].get('typ') != 'nfo':
                log.error('First element of config is not typ nfo! %s' % self.main.mod.name)
        else:
            config = [{'typ': 'nfo', 'author': '', 'version': '',
                       'description': 'Module Config is currently empty! imagine awesome layout here ...'}]

        nfo = config[0]
        self.update_header(nfo.get('author', ''), nfo.get('version', ''))

        self.controls = []
        self.menu_items = []

        for element_cfg in config:
            cfg_name = a2util.get_cfg_default_name(element_cfg)
            user_cfg = module_user_cfg.get(cfg_name, {})
            element_widget = a2ctrl.draw(self.main, element_cfg,
                                         self.main.mod, user_cfg)

            if isinstance(element_widget, QtWidgets.QWidget):
                self.controls.append(element_widget)
            elif isinstance(element_widget, QtWidgets.QAction):
                self.menu_items.append(element_widget)
            elif element_widget is not None:
                log.error('What is element "%s"?!' % element_widget)

        self.ui.head_widget.setVisible(True)
        self.toggle_edit(False)
        self.draw_ui()
Example #7
0
    def set_user_cfg(self, element_cfg, value, attr_name=None):
        """
        Sets an elements user value.

        Helps to keep the user config as small as possible.
        For instance if there is a value 'enabled' True by default
        only setting it to False will be saved. User setting it to True
        would delete it from user settings, so it's taking the default again.

        user sets True AND default is True:
            delete from user_cfg
        user sets True AND default it False:
            set to user_cfg

        :param dict element_cfg: The modules original config for the element.
        :param any value: Variable value o
        :param str attr_name: If given sets the value to the name inside a dict.
            Otherwise the value is regarded as a whole.
        """
        cfg_name = a2util.get_cfg_default_name(element_cfg)
        module_user_cfg = self.get_user_cfg()

        if attr_name is None:
            current_cfg = module_user_cfg.get(cfg_name)
            if value == current_cfg:
                return
            current_cfg = value

        else:
            current_cfg = module_user_cfg.get(cfg_name, {})
            if attr_name in current_cfg:
                # value to set equals CURRENT value: done
                if value == current_cfg.get(attr_name):
                    return
                # in any other case: delete to make changes
                current_cfg.pop(attr_name)

            # value to set equals CONFIG value: done. otherwise: save it:
            if value != element_cfg.get(attr_name):
                current_cfg[attr_name] = value

        # delete the value from module_user_cfg if needed
        if current_cfg is None:
            try:
                del module_user_cfg[cfg_name]
            except KeyError:
                pass
        else:
            module_user_cfg[cfg_name] = current_cfg

        # delete module_user_cfg alltogether if needed
        if module_user_cfg:
            self.a2.db.set(USER_CFG_KEY, module_user_cfg, self.key)
        else:
            self.clear_user_cfg()
Example #8
0
    def set_user_cfg(self, element_cfg, value, attr_name=None):
        """
        Sets an elements user value.

        Helps to keep the user config as small as possible.
        For instance if there is a value 'enabled' True by default
        only setting it to False will be saved. User setting it to True
        would delete it from user settings, so it's taking the default again.

        user sets True AND default is True:
            delete from user_cfg
        user sets True AND default it False:
            set to user_cfg

        :param dict element_cfg: The modules original config for the element.
        :param any value: Variable value o
        :param str attr_name: If given sets the value to the name inside a dict.
            Otherwise the value is regarded as a whole.
        """
        cfg_name = a2util.get_cfg_default_name(element_cfg)
        module_user_cfg = self.get_user_cfg()

        if attr_name is None:
            current_cfg = module_user_cfg.get(cfg_name)
            if value == current_cfg:
                return
            current_cfg = value

        else:
            current_cfg = module_user_cfg.get(cfg_name, {})
            if attr_name in current_cfg:
                # value to set equals CURRENT value: done
                if value == current_cfg.get(attr_name):
                    return
                # in any other case: delete to make changes
                current_cfg.pop(attr_name)

            # value to set equals CONFIG value: done. otherwise: save it:
            if value != element_cfg.get(attr_name):
                current_cfg[attr_name] = value

        # delete the value from module_user_cfg if needed
        if current_cfg is None:
            try:
                del module_user_cfg[cfg_name]
            except KeyError:
                pass
        else:
            module_user_cfg[cfg_name] = current_cfg

        # delete module_user_cfg alltogether if needed
        if module_user_cfg:
            self.a2.db.set(USER_CFG_KEY, module_user_cfg, self.key)
        else:
            self.clear_user_cfg()
Example #9
0
 def clear_user_cfg(self):
     name = a2util.get_cfg_default_name(self.cfg)
     self.mod.clear_user_cfg_name(name)
     self.main.load_runtime_and_ui()
Example #10
0
    def draw_mod(self):
        """
        from the modules config creates the usual display controls and
        fills them with the saved settings from the database.
        On change they trigger writing to the db, collect all include info
        and restart a2.
        """
        self.toggle_edit(False)

        if self.main.mod is None:
            if not self.main.num_selected:
                self.controls = [a2settings_view.A2Settings(self.main)]
                self.update_header()
                self.draw_ui()
                return

            else:
                config = [{
                    'typ':
                    'nfo',
                    'author':
                    '',
                    'version':
                    '',
                    'description':
                    'Multiple modules selected. Here goes some '
                    'useful info in the future...'
                }]
            module_user_cfg = {}
        else:
            config = self.main.mod.config
            module_user_cfg = self.main.mod.get_user_cfg()
            self.main.temp_config = None

        if len(config):
            if config[0].get('typ') != 'nfo':
                log.error('First element of config is not typ nfo! %s' %
                          self.main.mod.name)
        else:
            config = [{
                'typ':
                'nfo',
                'author':
                '',
                'version':
                '',
                'description':
                'Module Config is currently empty! imagine awesome layout here ...'
            }]

        nfo = config[0]
        self.update_header(nfo.get('author', ''), nfo.get('version', ''))

        self.controls = []
        self.menu_items = []

        for element_cfg in config:
            cfg_name = a2util.get_cfg_default_name(element_cfg)
            user_cfg = module_user_cfg.get(cfg_name, {})
            element_widget = a2ctrl.draw(self.main, element_cfg, self.main.mod,
                                         user_cfg)

            if isinstance(element_widget, QtWidgets.QWidget):
                self.controls.append(element_widget)
            elif isinstance(element_widget, QtWidgets.QAction):
                self.menu_items.append(element_widget)
            elif element_widget is not None:
                log.error('What is element "%s"?!' % element_widget)

        self.ui.head_widget.setVisible(True)
        self.toggle_edit(False)
        self.draw_ui()
Example #11
0
 def clear_user_cfg(self):
     name = a2util.get_cfg_default_name(self.cfg)
     self.mod.clear_user_cfg_name(name)
     self.main.load_runtime_and_ui()