Esempio n. 1
0
    def test_dict_simple(self):
        d = DictRepl()
        d.add("key1","v1")
        d.add("key1","v2")
        d.add("key2","v2")

        self.assertEqual( d.get("key1"), "v1|v2" )
        self.assertTrue(d.is_value("v1"))
        self.assertTrue(d.is_value("v2"))
        self.assertTrue(d.is_value_of("key1","v1"))
        self.assertTrue(d.is_value_of("key1","v2"))
        self.assertFalse(d.is_value("v1|v2"))
        self.assertTrue(d.is_value_of("key2","v2"))
        self.assertFalse(d.is_value_of("key2","v1"))
Esempio n. 2
0
class Mapping(object):
    """
    @authors: Brigitte Bigi
    @contact: [email protected]
    @license: GPL, v3
    @summary: Mapping of any string.


    """

    def __init__(self, dictname=None):
        """
        Create a new Mapping instance.

        @param dictname (string) is the file name with the mapping table (2 columns),

        """

        try:
            self.repl = DictRepl(dictname, nodump=True)
        except Exception:
            self.repl = DictRepl()

        self.keepmiss = True  # remove or not missing values
        self.reverse  = False # replace value by key instead of replacing key by value

    # End __init__
    # ------------------------------------------------------------------


    # -----------------------------------------------------------------------
    # Methods to fix options
    # -----------------------------------------------------------------------


    def fix_options(self, options):
        """
        Fix all options.

        Available options are:
            - keepmiss

        @param options (option)

        """

        for opt in options:

            key = opt.get_key()

            if key == "getmiss":
                self.set_keepmiss(opt.get_value())

            elif key == "reverse":
                self.set_reverse(opt.get_value())

            else:
                raise Exception('Unknown key option: %s'%key)

    # End fix_options
    # -----------------------------------------------------------------------


    def set_keepmiss(self, keepmiss):
        """
        Fix the keepmiss option.
        If keepmiss is set to True, each missing entry is kept without change.
        If keepmiss is set to False, each missing entry is deleted.

        @param keepmiss (Boolean)

        """
        self.keepmiss = keepmiss

    # End set_keepmiss
    # ----------------------------------------------------------------------


    def set_reverse(self, reverse):
        """
        Fix the reverse option.
        If replace is set to True, mapping will replace value by key
        instead of replacing key by value.

        @param reverse (Boolean)

        """
        self.reverse = reverse

    # End set_reverse
    # ----------------------------------------------------------------------


    def map_entry(self, entry):
        """
        Map an entry (a key or a value).

        @param entry is the input string to map
        @return a string

        """
        if self.repl.get_dictsize() == 0:
            return entry

        if self.reverse is False:
            if self.repl.is_key( entry ):
                return self.repl.replace( entry )
        else:
            if self.repl.is_value( entry ):
                return self.repl.replace_reversed( entry )

        if self.keepmiss is False:
            return ''
        return entry

    # End map_entry
    # ----------------------------------------------------------------------


    def map( self, str ):
        """
        Run the Mapping process on an input string.

        @param str is the input string to map
        @return a string

        """
        if self.repl.get_dictsize() == 0:
            return str

        # suppose that some punctuation are like a separator
        # and we have to replace all strings between them
        tab = re.split(r'(;|,|\s|\.|\|)\s*', str)

        for i,v in enumerate(tab):
            if v in [';', ',', '\s', '.', '|']:
                continue
            tab[i] = self.map_entry(v)

        return ''.join(tab)