#!/usr/bin/python # -*- coding: utf-8 -*- # Written for Python 2.x # Tested in Python 2.5.2 # Idea and implementation of the original Kenny Translator idea by Kohan Ikin # http://www.namesuppressed.com/kenny/ # # Reimplementation in Python by Stefan Bilsing # # Copyright (c) 2010 Stefan Bilsing # # This is free software. # Feel free to copy, modify, distribute it. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import sys # System Arguments import re # Regular Expressions import string # Advanced String Functions class KennyTranslator: def kennify(self, text): """ Translates the text into Kenny speak """ encoded = '' codemap = string.maketrans('012', 'mpf') for i in range(len(text)): if re.match('[A-Za-z]', text[i]): # 'V' -> 21 num = ord(text[i].lower()) - ord('a') # Base 10 -> Base 3 num = (num/9)*100 + ((num%9)/3)*10 + (num%9)%3 # 210 -> 'Fpm' triple = ("%03d"%num).translate(codemap) if text[i].isupper(): triple = triple.capitalize() encoded = encoded + triple else: encoded = encoded + text[i] return encoded def unkennify(self, text): """ Translates the Kenny speak back to normal """ decoded = '' codemap = string.maketrans('MmPpFf', '001122') nLen = len(text) i = 0 while i+3 <= nLen: if re.match('[MmPpFf]{3,}', text[i:i+3]): # 'mpf' -> '012' -> 5 num = int( text[i:i+3].translate(codemap), 3) # 5 -> 'f' cypher = chr(num + ord('a')) if text[i].isupper(): cypher = cypher.upper() decoded = decoded + cypher i = i + 3 else: decoded = decoded + text[i] i = i + 1 if i < nLen: decoded = decoded + text[i:] return decoded def iskennified(self, text): """ Is there any alphanumeric character besides F, M and P ? """ return not re.search('[A-EG-LNOQ-Za-eg-lnoq-z]', text) if __name__ == '__main__': usage = """ Kenny Translator usage: python Kenny.py [-k|-u] -k kennify text -u unkennify text examples: python Kenny.py -k 'Hello World' python Kenny.py -u 'Mfpmpppmfpmfppf Fppppfpffpmfmpm' """ nLen = len(sys.argv) text = '' if nLen < 2: print usage elif nLen < 3 and (sys.argv[1] == '-k' or sys.argv[1] == '-u'): print usage else: if sys.argv[1] == '-k' or sys.argv[1] == '-u': iStart = 2 else: iStart = 1 if iStart == nLen - 1: text = sys.argv[iStart] else: for i in range( iStart, nLen): text = text + sys.argv[i] + ' ' if sys.argv[1] == '-k': bDoKenny = True elif sys.argv[1] == '-u': bDoKenny = False else: bDoKenny = not KennyTranslator().iskennified(text) if bDoKenny: print KennyTranslator().kennify(text) else: print KennyTranslator().unkennify(text)