#!/usr/bin/python2.5
# coding: utf-8
''' cocomo.py

Version 0.1ξ

The amazing API that lets cocoa be used sensibly by people from Maclab.

To use, simply type

from cocomo import GypsyMagic


This code released under the terms of the WTFPL, which can be found at http://sam.zoy.org/wtfpl/

'''

import AppKit

REAL_ATTRS = ["__u__","no_really"]

class CocomoProxy(object):
	''' CocomoProxy objects are used to proxy method calls to PyObjC objects '''
	def __init__(self, underlay):
		self.__u__ = underlay
		
	def no_really(self):
		return self.__u__
	
	def __getattribute__(self,name):
		#''' Perform method/attribute proxying on ''' + repr(self.__u__)
		if name in REAL_ATTRS:
			return object.__getattribute__(self,name)
		else:
			new_objectname = "self.__u__.%s" % translate(name)
			new_object = eval(new_objectname)
			return CocomoProxy(new_object)
	def __call__(self,*a, **k):
		new_a = [i.__u__ if type(i) == CocomoProxy else i for i in a]
		new_k = dict( (translate(i), k[i].__u__ if type(k[i]) == CocomoProxy else k[i]) for i in k)
		return CocomoProxy(self.__u__(*new_a,**new_k))
	
			
	def __repr__(self):
		return "<CocomoProxy for %s>" % self.__u__
		
	def __iter__(self):
		for i in self.__u__:
			yield CocomoProxy(i)

# GypsyMagic is the proxy for AppKit
GypsyMagic = CocomoProxy(AppKit)


def translate(inp):
	''' Translates an input string from key language to value language '''
	for i in LANGUAGE:
		if i[0].islower():
			# Try both capital case and lowercase
			inp = inp.replace(i, LANGUAGE[i])
			inp = inp.replace(rtitle(i), rtitle(LANGUAGE[i]))
		else:
			inp = inp.replace(i, LANGUAGE[i])
	return inp


def rtitle(i):
	return i[0].upper() + i[1:]


''' And now, the language definition.  The keys are the maclabbian variant of the values  '''

LANGUAGE = {
	"OG" 		: "NS",
	"forker" 	: "object",
	"forkable" 	: "mutable",
	"thrusterate"	: "insert",
	"mouthWords": "string",
	"makeGogo"	: "init",
	"subsume"	: "alloc",
	"thingy"	: "value",
	"trinketHolder" : "array",
	"thrustinglyThrust" : "add",
}

