[livecoding] r131 committed - - Added a way to get callbacks when classes are created....

1 view
Skip to first unread message

livec...@googlecode.com

unread,
Jan 29, 2010, 3:54:51 AM1/29/10
to py-livecod...@googlegroups.com
Revision: 131
Author: richard.m.tew
Date: Fri Jan 29 00:54:33 2010
Log: - Added a way to get callbacks when classes are created.
- Fixed method to register class update callbacks.
http://code.google.com/p/livecoding/source/detail?r=131

Modified:
/trunk/namespace.py
/trunk/reloader.py

=======================================
--- /trunk/namespace.py Wed Jan 20 18:00:25 2010
+++ /trunk/namespace.py Fri Jan 29 00:54:33 2010
@@ -167,6 +167,8 @@

# Personal references to created namespaces.
self.namespaces = {}
+
+ self.classCreationCallback = None

self.SetBaseDirectory(baseDirPath)
self.SetBaseNamespaceName(baseNamespace)
@@ -174,6 +176,9 @@
def __del__(self):
self.Unload()

+ def SetClassCreationCallback(self, ob):
+ self.classCreationCallback = ob
+
def SetBaseDirectory(self, baseDirPath):
self.baseDirPath = baseDirPath

@@ -385,9 +390,25 @@
contributedAttributes.add(k)

logger.info("Added '%s.%s'", moduleName, k)
+
+ if type(v) in (types.TypeType, types.ClassType):
+ self.BroadcastClassCreationEvent(namespace, k, v)
+ # print namespace, k, type(v)

scriptFile.SetContributedAttributes(contributedAttributes)

+ def BroadcastClassCreationEvent(self, *args):
+ if self.classCreationCallback:
+ try:
+ if type(self.classCreationCallback) is tuple:
+ getattr(self.classCreationCallback[0],
self.classCreationCallback[1])(*args)
+ else:
+ self.classCreationCallback(*args)
+ except ReferenceError:
+ self.classCreationCallback = None
+ except Exception:
+ logger.exception("Error broadcasting class creation")
+
def RemoveModuleAttributes(self, scriptFile, namespace):
logger.debug("RemoveModuleAttributes %s", scriptFile.filePath)
if scriptFile.contributedAttributes is None:
=======================================
--- /trunk/reloader.py Wed Jan 20 18:00:25 2010
+++ /trunk/reloader.py Fri Jan 29 00:54:33 2010
@@ -34,6 +34,7 @@

self.directoriesByPath = {}
self.leakedAttributes = {}
+ self.classCreationCallback = None
self.classUpdateCallback = None

if monitorFileChanges:
@@ -56,12 +57,32 @@
self.classUpdateCallback = (weakref.proxy(ob.im_self),
ob.func_name)
elif type(ob) is types.FunctionType:
self.classUpdateCallback = weakref.proxy(ob)
+ elif ob is None:
+ self.classUpdateCallback = ob
+ else:
+ raise Exception("Bad callback")
+
+ def SetClassCreationCallback(self, ob):
+ if type(ob) is types.MethodType:
+ self.classCreationCallback = (weakref.proxy(ob.im_self),
ob.func_name)
+ elif type(ob) is types.FunctionType:
+ self.classCreationCallback = weakref.proxy(ob)
+ elif ob is None:
+ self.classCreationCallback = ob
+ else:
+ raise Exception("Bad callback")
+
+ for handler in self.directoriesByPath.itervalues():
+ handler.SetClassCreationCallback(self.classCreationCallback)

#
------------------------------------------------------------------------
# Directory registration support.

def AddDirectory(self, baseNamespace, baseDirPath):
handler = self.scriptDirectoryClass(baseDirPath, baseNamespace)
+ if self.classCreationCallback:
+ handler.SetClassCreationCallback(self.classCreationCallback)
+
if handler.Load():
self.directoriesByPath[baseDirPath] = handler
logger.info("Added '%s' into '%s'", baseDirPath, baseNamespace)

Reply all
Reply to author
Forward
0 new messages