Exemplo n.º 1
0
    def generic_save_config(self,
                            config_name,
                            dict_key=None,
                            data_dict=None,
                            delete=False):
        """Save email message to config.

        If data_dict is None, delete message with key==dict_key
        If data_dict is not None, dict_key is ignored
        If data_dict does not have a key named "key", it's a new config element, so a new key is created.

        config_name must be either of the dictionaries in the main config (See DEFAULT_PREFS):
        """
        if data_dict is not None and type(data_dict) != dict:
            raise ValueError(
                "generic_save_config: data_dict must be a dictionary: '%s'" %
                str(data_dict))

        if config_name not in default_prefs():
            raise ValueError("Invalid config key:" + str(config_name))

        config = self.config[config_name]

        # Means delete
        if data_dict is None:
            if dict_key is None:
                raise ValueError(
                    "generic_save_config: key and value cannot both be None")
            if not delete:
                raise ValueError(
                    "generic_save_config: deleting item requires 'delete' to be True"
                )
            # Value is None, means delete entry with key dict_key
            if dict_key in config:
                del config[dict_key]
                # Save main config to file
                self.config.save()
                return self.config.config
            else:
                raise ValueError("generic_save_config: Invalid key - "
                                 "Item with key %s doesn't exist" % dict_key)
        else:  # Save config
            # The entry to save does not have an item 'key'. This means it's a new entry in the config
            if "key" not in data_dict:
                dict_key = common.get_new_dict_key(config)
                data_dict["key"] = dict_key
            else:
                dict_key = data_dict["key"]

        config[dict_key] = data_dict
        self.config.save()
        return self.config.config
    def generic_save_config(self, config_name, dict_key=None, data_dict=None, delete=False):
        """Save email message to config.

        If data_dict is None, delete message with key==dict_key
        If data_dict is not None, dict_key is ignored
        If data_dict does not have a key named "key", it's a new config element, so a new key is created.

        config_name must be either of the dictionaries in the main config (See DEFAULT_PREFS):
        """
        if data_dict is not None and type(data_dict) != dict:
            raise ValueError("generic_save_config: data_dict must be a dictionary: '%s'" % str(data_dict))

        if not default_prefs().has_key(config_name):
            raise ValueError("Invalid config key:" + str(config_name))

        config = self.config[config_name]

        # Means delete
        if data_dict is None:
            if dict_key is None:
                raise ValueError("generic_save_config: key and value cannot both be None")
            if not delete:
                raise ValueError("generic_save_config: deleting item requires 'delete' to be True")
           # Value is None, means delete entry with key dict_key
            if config.has_key(dict_key):
                del config[dict_key]
                # Save main config to file
                self.config.save()
                return self.config.config
            else:
                raise ValueError("generic_save_config: Invalid key - "\
                                     "Item with key %s doesn't exist" % dict_key)
        else: # Save config
            # The entry to save does not have an item 'key'. This means it's a new entry in the config
            if not data_dict.has_key("key"):
                dict_key = common.get_new_dict_key(config)
                data_dict["key"] = dict_key
            else:
                dict_key = data_dict["key"]

        config[dict_key] = data_dict
        self.config.save()
        return self.config.config
    def update_rssfeeds_dict_matching(self, rssfeed_parsed, options):
        """rssfeed_parsed: Dictionary returned by get_rssfeed_parsed_dict
        options, a dictionary with the following keys:
        * "regex_include": str
        * "regex_exclude": str
        * "regex_include_ignorecase": bool
        * "regex_exclude_ignorecase": bool

        Updates the items in rssfeed_parsed
        Return: a dictionary of the matching items only.
        """
        # regex and title are converted from utf-8 unicode to ascii strings before matching
        # This is because the indexes returned by span must be the byte index of the text,
        # because Pango attributes takes the byte index, and not character index.

        matching_items = {}
        p_include = p_exclude = None
        message = None

        # Remove old custom lines
        for key in rssfeed_parsed.keys():
            if rssfeed_parsed[key]["link"] is None:
                del rssfeed_parsed[key]

        if options.has_key("custom_text_lines") and options["custom_text_lines"]:
            if not type(options["custom_text_lines"]) is list:
                self.log.warn("type of custom_text_lines' must be list")
            else:
                for l in options["custom_text_lines"]:
                    key = common.get_new_dict_key(rssfeed_parsed, string_key=False)
                    rssfeed_parsed[key] = self._new_rssfeeds_dict_item(l, key=key)

        if options["regex_include"] is not None and options["regex_include"] != "":
            flags = re.IGNORECASE if options["regex_include_ignorecase"] else 0
            try:
                regex = common.string_to_unicode(options["regex_include"]).encode("utf-8")
                p_include = re.compile(regex, flags)
            except Exception, e:
                #traceback.print_exc(e)
                self.log.warn("Regex compile error:" + str(e))
                message = "Regex: %s" % e
                p_include = None
Exemplo n.º 4
0
    def update_rssfeeds_dict_matching(self, rssfeed_parsed, options):
        """rssfeed_parsed: Dictionary returned by get_rssfeed_parsed_dict
        options, a dictionary with the following keys:
        * "regex_include": str
        * "regex_exclude": str
        * "regex_include_ignorecase": bool
        * "regex_exclude_ignorecase": bool

        Updates the items in rssfeed_parsed
        Return: a dictionary of the matching items only.
        """
        # regex and title are converted from utf-8 unicode to ascii strings before matching
        # This is because the indexes returned by span must be the byte index of the text,
        # because Pango attributes takes the byte index, and not character index.

        matching_items = {}
        p_include = p_exclude = None
        message = None

        # Remove old custom lines
        for key in rssfeed_parsed.keys():
            if rssfeed_parsed[key]["link"] is None:
                del rssfeed_parsed[key]

        if "custom_text_lines" in options and options["custom_text_lines"]:
            if not type(options["custom_text_lines"]) is list:
                self.log.warn("type of custom_text_lines' must be list")
            else:
                for l in options["custom_text_lines"]:
                    key = common.get_new_dict_key(rssfeed_parsed, string_key=False)
                    rssfeed_parsed[key] = self._new_rssfeeds_dict_item(l, key=key)

        if options["regex_include"] is not None and options["regex_include"] != "":
            flags = re.IGNORECASE if options["regex_include_ignorecase"] else 0
            try:
                regex = common.string_to_unicode(options["regex_include"]).encode("utf-8")
                p_include = re.compile(regex, flags)
            except Exception, e:
                self.log.warn("Regex compile error:" + str(e))
                message = "Regex: %s" % e
                p_include = None