""" *==LICENSE==*

CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.

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 3 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, see <http://www.gnu.org/licenses/>.

Additional permissions under GNU GPL version 3 section 7

If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.

You can contact Cyan Worlds, Inc. by email legal@cyan.com
 or by snail mail at:
      Cyan Worlds, Inc.
      14617 N Newport Hwy
      Mead, WA   99021

 *==LICENSE==* """
"""
This module is the word filter to be used...
"""
import string
import re

# Rating levels
xRatedG = 0
xRatedPG = 1
xRatedPG13 = 2
xRatedR = 3
xRatedX = 4

class LanguageFilter:
    def __init__(self):
        pass
    def test(self,sentence):
        "returns censored sentence"
        return xRatedG
    def censor(self,sentence,censorLevel):
        "returns censored sentence"
        return sentence

class ExactMatchListFilter(LanguageFilter):
    def __init__(self,wordlist):
        self.wordlist = wordlist
    def test(self,sentence):
        "return the rating of sentence in question"
        rated = xRatedG     # assume rated lowest level
        startidx = 0
        for endidx in range(len(sentence)):
            if sentence[endidx] in string.whitespace or sentence[endidx] in string.punctuation:
                if startidx != endidx:
                    try:
                        # find and get rating and substitute
                        rating = self.wordlist[string.lower(sentence[startidx:endidx])]
                    except LookupError:
                        # couldn't find word
                        rating = None
                    if rating != None and rating.rating > rated:
                        # substitute into string
                        rated = rating.rating
                startidx = endidx + 1
        if startidx < len(sentence):
            try:
                # find and get rating and substitute
                rating = self.wordlist[string.lower(sentence[startidx:])]
            except LookupError:
                # couldn't find word
                rating = None
            if rating != None and rating.rating > rated:
                # substitute into string
                rated = rating.rating
        return rated
        
    def censor(self,sentence,censorLevel):
        "censors a sentence to a rating"
        # break into words, but perserve original punctuation
        censored = ""
        startidx = 0
        for endidx in range(len(sentence)):
            if sentence[endidx] in string.whitespace or sentence[endidx] in string.punctuation:
                if startidx != endidx:
                    try:
                        # find and get rating and substitute
                        rating = self.wordlist[string.lower(sentence[startidx:endidx])]
                    except LookupError:
                        # couldn't find word
                        rating = None
                    if rating != None and rating.rating > censorLevel:
                        # substitute into string
                        censored += rating.substitute + sentence[endidx]
                    else:
                        censored += sentence[startidx:endidx] + sentence[endidx]
                else:
                    censored += sentence[startidx]
                startidx = endidx + 1
        if startidx < len(sentence):
            # Special after loop processing!
            try:
                # find and get rating and substitute
                rating = self.wordlist[string.lower(sentence[startidx:])]
            except LookupError:
                # couldn't find word
                rating = None
            if rating != None and rating.rating > censorLevel:
                # substitute into string
                censored += rating.substitute
            else:
                censored += sentence[startidx:]
        return censored

class REFilter(LanguageFilter):
    def __init__(self,regexp,rating):
        self.compiledRE = re.compile(regexp, re.IGNORECASE | re.MULTILINE )
        if not isinstance(rating,Rating):
            PtDebugPrint("ptWordFilter: rating for %s not of type Rating" % (regexp))
        self.rating = rating
    def test(self,sentence):
        "return the rating of sentence in question"
        if self.compiledRE.search(sentence) != None:
            return self.rating.rating
        return xRatedG
    def censor(self,sentence,censorLevel):
        "censors a sentence to a rating"
        if self.rating.rating > censorLevel:
            if self.compiledRE.search(sentence) != None:
                return self.compiledRE.sub(self.rating.substitute,sentence)
        return sentence

class Rating:
    "substitute can be string for exact substitute or number of splat replacement"
    def __init__(self,rating,subtitute="*****"):
        self.rating = rating
        self.substitute = subtitute
