#!/usr/bin/env python
import os, sys, string, copy
from log import *
## ----------------------------------------------------------------------
## BaseObject class [ Steve Dekorte, Spring 1999 ]
## ----------------------------------------------------------------------
class BaseObject:
def __init__(self, cx=None):
self._cx = cx # use only when really needed
self._methods = []
self._showLog = 0
def context(self): return self._cx
def context_(self, o): self._cx = o; return self
def cx(self): return self._cx
def cx_(self, o): self._cx = o; return self
def showLog(self): return self._showLog
def showLog_(self, o): self._showLog = o; return self
#--- Utilities ------------------------------------------------
def respondsTo_(self, methodName):
# if it's a method
if hasattr(self, methodName):
if type(getattr(self, methodName)) == type(getattr(self, "respondsTo_")):
return 1
# if it's a auto-getter
if hasattr(self, "_" + methodName):
return 1
# if it's a auto-setter
if len(methodName):
if methodName[-1] == "_" and hasattr(self, methodName[0:-1]):
return 1
return 0
def perform_(self, methodName):
try:
aMethod = getattr(self, methodName)
except AttributeError:
self.attributeErrorFor_(methodName)
return
return aMethod()
def perform_with_(self, methodName, anObject):
try:
aMethod = getattr(self, methodName)
except AttributeError:
self.attributeErrorFor_(methodName)
return
return aMethod(anObject)
#--- Copy --------------------------------------
def shallowCopy(self):
#import who_calls
#log(who_calls.pretty_who_calls())
return copy.copy(self)
#--- Attributes --------------------------------------
def attributeNames(self):
names = []
for name in dir(self.__class__) + dir(self):
if name[0:1] == '_' and name[0:2] != '__':
names.append(name[1:len(name)])
#log("name = "+ name[1:len(name)])
#for name in self.externalAttributeNames():
# names.append(name)
return names
#--- Auto Getter/Setters --------------------------------------
def autoGetterSetter(self, *args):
name = self.forwardMethodName()
#---- Check for special attributes -----------------
if name[:2] == '__' and name[-2:] == '__':
raise AttributeError
log("is special :" + repr(name) + " = " + repr(getattr(self, name)))
self.doneWithMethodCall()
if name == '__nonzero__': return 1
return getattr(self, name)
#self._showLog = 0
#if string.find(name, "headerBoxColor") != -1:
# self._showLog = 1
#log("name = " + name)
#---- Auto Setter ----------------------------------
#log("setter name = " + name)
if name[-1] == '_':
vName = "_" + name[0:-1]
if hasattr(self, vName ) and len(args) == 1:
if self._showLog: log("<<<<<<<<< setattr(%s, %s)" % (vName, repr(args[0])) )
setattr(self, vName, args[0] )
self.doneWithMethodCall()
return self
#---- Auto Getter ----------------------------------
if hasattr(self, "_"+ name):
#log("Auto Getter = " + name)
if len(args):
log("******* Error: attempt to call getter '%s' arguments: %s" % (name, repr(args)))
raise AttemptToCallGetterWithArguments
value = getattr(self, "_"+ name)
if self._showLog: log(">>>>>>>>>> %s = getattr(%s)" % (repr(value), "_"+ name) )
if value != self.forward:
self.doneWithMethodCall()
return value
self.attributeErrorFor_(name)
self.doneWithMethodCall()
return
def forward(self, *args):
self.attributeErrorFor_(self.forwardMethodName())
return
def attributeErrorFor_(self, name):
errorString = "[%s %s]" % (self.className(), repr(name))
raise AttributeError, errorString
def className(self):
name = repr(self)[1:]
name = string.split(name, " ")[0]
return string.split(name, ".")[1]
def moduleName(self):
name = repr(self)[1:]
name = string.split(name, " ")[0]
return string.split(name, ".")[0]
def slotNames(self):
return dir(self.__class__) + dir(self)
def methodNames(self):
methodNames = []
for slotName in self.slotNames():
value = self.valueOfSlotNamed_(slotName)
if type(value) == 'function' and slotName[:2] != "__":
methodNames.append(methodName)
return methodNames
def variableNames(self):
methodNames = []
for slotName in self.slotNames():
value = self.valueOfSlotNamed_(slotName)
if type(value) != 'function' and slotName[:2] != "__":
methodNames.append(slotName)
return methodNames
def valueOfSlotNamed_(self, slotName):
return getattr(self, slotName)
def printVariables(self):
for name in self.variableNames():
log(" " + name + " : " + repr(self.valueOfSlotNamed_(name)))
def forwardMethodName_(self, o):
#self._forwardMethodName = o
#return self
self._methods.append(o)
log(repr(self._methods))
#if len(self._methods) > 1:
#import handleexception
#handleexception.HandleException()
def forwardMethodName(self):
#return self._forwardMethodName
o = self._methods[-1]
#o = self._methods[1]
return o
def doneWithMethodCall(self):
del self._methods[-1]
return
def instanceForClassName_(self, className, packageName=""):
import ihooks
ml = ihooks.ModuleImporter()
if packageName:
mod = ml.import_module(packageName + "." + className)
return getattr(mod, className)()
else:
mod = ml.import_module(className)
return getattr(mod, className)()
def clone(self):
import copy
clone = self.instanceForClassName_(self.className(), "HtmlKit")
dict = copy.copy(self.__dict__)
return clone
mapDict = {}
for key in dict.keys():
if type(key) == type([]):
pass
clone.__dict__ = dict
return clone
#----------------------------------------------------------------------
class AutoGetterSetterObject(BaseObject):
def __init__(self, cx=None):
BaseObject.__init__(self, cx=None)
def __getattr__(self, name, interalCall=0):
#log("__getattr__: " + repr(name))
if name[:2] == '__' and name[-2:] == '__':
raise AttributeError
return
#if len(self._methods):
# log("++++ " + repr(self._methods) )
# return
#"called upon failure to find attribute"
#---- So forward method knows which method was being called ----
if self._showLog: log("++++++ __getattr__ (%s)" % name)
if not interalCall:
self.forwardMethodName_(name)
return self.autoGetterSetter
#----------------------------------------------------------------------
def instanceForClassName_(self, className):
import ihooks
ml = ihooks.ModuleImporter()
mod = ml.import_module(className)
return getattr(mod, className)()