# Copyright (C) 2010 Holger Berndt <hb@gnome.org>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""Preview-like objects need to implement the "preview" function and have
a "short_description" class member. If it also has a "skip" class member
which evaluates to True, the class is skipped.
The constructor needs to take a refresh func as argument, which must not
be called during preview (or an endless loop might occur)."""

import string


class PreviewNoop(object):
    """Source name is identical to target name."""

    short_description = "No change"
    skip = False

    def __init__(self, refresh_func):
        self._refresh_func = refresh_func
        
    def preview(self, model):
        for row in model:
            row[1] = row[0]


class PreviewTranslate(object):
    """General character translation"""

    short_description = "Character translation"
    skip = True

    def __init__(self, refresh_func):
        self._refresh_func = refresh_func
        self._translation_table = None

    def set_source_and_target(self, source, target):
        self._translation_table = string.maketrans(source, target)
    
    def preview(self, model):
        if self._translation_table:
            for row in model:
                row[1] = row[0].translate(self._translation_table)



class PreviewReplaceSpacesWithUnderscores(PreviewTranslate):

    short_description =  "Replace spaces with underscores"
    skip = False

    def __init__(self, refresh_func):
        PreviewTranslate.__init__(self, refresh_func)
        self.set_source_and_target(" ", "_")


class PreviewReplaceEverySecondWithFixedString(object):
    """Just for testing"""
    
    short_description = "Replace with fixed string"
    skip = False
    
    def __init__(self, refresh_func):
        pass
    
    def preview(self, model):
        for ii,row in enumerate(model):
            if (ii % 2) == 0:
                row[1] = "foobar"
            else:
                row[1] = row[0]

@@ -2615,28 +2615,399 @@
             row%5B1%5D = row%5B0%5D%0A
+%0A%0Aclass PreviewCircleNames(object):%0A    %22%22%22Just for testing%22%22%22%0A%0A    short_description = %22Circle names%22%0A    skip = False%0A%0A    def __init__(self, refresh_func):%0A        pass%0A%0A    def preview(self, model):%0A        if not model:%0A            return%0A        for ii in range(1, len(model)):%0A            model%5Bii-1%5D%5B1%5D = model%5Bii%5D%5B1%5D%0A        model%5Blen(model)-1%5D%5B1%5D = model%5B0%5D%5B0%5D%0A

Add circle preview test class